我有一個「簡單」問題。我嘗試繪製surfaceview。佈局XML:安卓在Surfaceview和canvas上繪圖
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<SurfaceView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imagesurface"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#00ff00">
</SurfaceView>
</LinearLayout>
該活動是SurfaceHolder.Callback:
public class OpenCvonAndroidGTDforHOGActivity extends Activity implements SurfaceHolder.Callback{
private SurfaceHolder _surfaceHolder;
private SurfaceView _surfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_surfaceView = (SurfaceView)findViewById(R.id.imagesurface);
_surfaceHolder = _surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceView.setWillNotDraw(false);
}
protected void onDraw(Canvas canvas) {
canvas.drawRGB(255, 0, 255);
}
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
synchronized(holder) {
onDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
public void showToast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
如果我稱之爲的onDraw
_surfaceview.setBackground(Color.RED)
(...),它的工作原理。但
canvas.drawRGB(255, 0, 255)
不工作:(
它的工作原理。但爲什麼不從xml-layout的surfaceview中工作? – ChHaupt
它因爲你已經提到android:background =「#00ff00」。如果你刪除它,那麼你的代碼將工作。儘管我不知道是什麼原因。 –
Thx。它的工作原理沒有android:background =「#00ff00」。無論如何。 – ChHaupt