11

我有一个“简单”的问题。我试着在surfaceview上画画。布局-XML:

<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>

Activity 是一个 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();
    }
}

如果我打电话

_surfaceview.setBackground(Color.RED) 在 onDraw(...) 它工作。但

canvas.drawRGB(255, 0, 255) 不工作:(

4

3 回答 3

23

关注 Snippet 将对您有所帮助。

public class SurfaceDemo extends Activity implements SurfaceHolder.Callback {

    private static final String TAG = "Svetlin SurfaceView";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SurfaceView view = new SurfaceView(this);
        setContentView(view);
        view.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        tryDrawing(holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) { 
        tryDrawing(holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {}

    private void tryDrawing(SurfaceHolder holder) {
        Log.i(TAG, "Trying to draw...");

        Canvas canvas = holder.lockCanvas();
        if (canvas == null) {
            Log.e(TAG, "Cannot draw onto the canvas as it's null");
        } else {
            drawMyStuff(canvas);
            holder.unlockCanvasAndPost(canvas);
        }
    }

    private void drawMyStuff(final Canvas canvas) {
        Random random = new Random();
        Log.i(TAG, "Drawing...");
        canvas.drawRGB(255, 128, 128);
    }
}
于 2012-06-07T12:23:19.777 回答
3

SurfaceViewView当前布局中的 a 和布局下的表面组成。如果您为视图设置背景,您将看不到表面上发生的任何事情。

于 2016-08-26T11:57:14.820 回答
-2

你试过了吗

yourHolder.unlockCanvassAndPost(your_canvas);
于 2014-12-24T09:22:46.953 回答