[英]Using personal SurfaceView on LinearLayout
我目前正在使用個人SurfaceView
和雙緩沖開發Android應用程序。 但是我的代碼面臨一些小問題。
一方面,我有一個基於LinearLayout
層次結構的xml視圖。 實例化活動時,我在此xml上設置了contentView
。 問題是我的雙緩沖不再起作用了。 線程正在運行,但未顯示任何內容。
另一方面,我使用新的個人SurfaveView
元素設置了contentView
,並且顯示效果很好。 但是,當然,我無法再訪問LinearLayout
的其他元素。
其實,我想設置我的contentView
我的XML視圖, 並讓我的工作展示。
我希望我足夠清楚,謝謝您的回答!
這是我的活動:
public class MainController extends Activity {
@Override
protected void onCreate(final Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
this.drawableView = (MCustomDrawableView) findViewById(R.id.drawingBox);
...
}
...
}
我的表面視圖:
public class MCustomDrawableView extends SurfaceView {
public MCustomDrawableView(Context p_context, AttributeSet p_attributes) {
super(p_context, p_attributes);
this.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(final SurfaceHolder p_holder) {
try {
// Instantiating a new thread
thread = new MBufferingThread(getInstance());
thread.start();
} catch (Exception exception) {
exception.printStackTrace();
}
}
@Override
public void surfaceChanged(final SurfaceHolder p_holder, int p_format, int p_width, int p_height) {...}
@Override
public void surfaceDestroyed(final SurfaceHolder p_holder) {...}
});
// Setup drawing options
setupDrawing();
}
...
}
而我的xml視圖:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/app_background"
tools:context=".MainController">
<com.iskn.calligraphy.models.draw.MCustomDrawableView
android:id="@+id/drawingBox"
style="@style/DrawingBox" />
<SeekBar
android:id="@+id/brushSize"
style="@style/SizeSeekBar" />
<SeekBar
android:id="@+id/brushOpacity"
style="@style/OpacitySeekBar" />
</LinearLayout>
編輯:
經過進一步的研究和分析,顯然:
setContentView(R.layout.main_controller_activity)
:在這種情況下,我從活動中獲取了所有元素,但MCustomDrawableView
顯示任何內容。 setContentView(new MCustomDrawableView(getApplicationContext()))
:在這種情況下, MCustomDrawableView
運行良好(顯示我想要的內容),但是我的main_controller_activity
沒有其他View
在兩種情況下:
holder.lockCanvas()
和holder.unlockCanvasAndPost(bufferCanvas)
方法。 好吧,我找到了一個功能性的解決方案:
我認為問題出在MCustomDrawableView
初始化。 我從onCreate
方法創建了該類的新實例,而不是從xml文件創建了。 然后,將其設置為contentView
:
protected void onCreate(Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
// Instantiate drawable object & set style properties
this.drawableView = new MCustomDrawableView(getApplicationContext());
FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// Add the drawable view to the main_activity
addContentView(this.drawableView, style);
}
但是這種方式提出了一個新的問題。 添加的視圖位於頂部,這意味着所有其他View
都被隱藏。 我用下面的方法bringToBack(this.drawableView)
解決了這個問題:
private void bringToBack(View p_view) {
// Get parent from the current view
ViewGroup viewGroup = ((ViewGroup) p_view.getParent());
int childrenCount = viewGroup.indexOfChild(p_view);
for(int cpt = 0; cpt < childrenCount; cpt++) {
// Move the child to the top
viewGroup.bringChildToFront(viewGroup.getChildAt(cpt));
}
}
它將提供的視圖返回到背景位置。 我還必須將LinearLayout的alpha設置為0。
我仍然願意接受其他解決方案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.