我目前正在開發一個Android應用程序,使用個人SurfaceView
和雙緩衝。但是我的代碼遇到了一些問題。
一方面,我有一個基於LinearLayout
層次結構的xml視圖。當我實例化我的活動時,我在此xml上設置了我的contentView
。問題是,我的雙緩衝不再工作了。線程正在運行,但沒有顯示。
另一方面,我將我的contentView
設置爲新的個人SurfaveView
元素,並且顯示效果良好。但是,當然,我不能訪問我的LinearLayout
的其他元素。
其實,我想設置我的contentView
在我的xml視圖和保持我的顯示工作。
我希望我已經夠清楚了,謝謝你的回答!LinearLayout上使用個人SurfaceView
這裏是我的活動:
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>
編輯:
經過進一步的研究和分析,它顯得清晰RLY是:
setContentView(R.layout.main_controller_activity)
:在這種情況下,我得到的所有從我的活動元素,但MCustomDrawableView
就什麼都不顯示。setContentView(new MCustomDrawableView(getApplicationContext()))
:在這種情況下,MCustomDrawableView
運作良好(它顯示我想要的),但我不從我main_controller_activity
有其他View
在兩種情況下:
- 我線程正在運行並運行良好。
- 我的繪圖功能也被調用,使用
holder.lockCanvas()
和holder.unlockCanvasAndPost(bufferCanvas)
方法。
什麼是「個人」SurfaceView? SurfaceView已經是雙緩衝或三緩衝,爲什麼要添加更多的緩衝?如果問題出在緩衝區或繪圖上,則應顯示該代碼。 – fadden
謝謝你的回答。我所謂的「個人」SurfaceView就是我的MCustomDrawableView類,它擴展了SurfaceView。在我的情況下,雙緩衝允許我在緩衝畫布上執行繪圖動作。然後,我顯示一個(主)的畫布。實際上這部分工作正常,我唯一的問題是我的主要活動和擴展SurfaceView的這個類之間的聯繫。 – Sierra