Drawing to a SurfaceView in Android

2019-02-07 15:38发布

问题:

I'm trying to do some very simple drawing into a SurfaceView but I can't get it working. There are no exceptions, but I don't see any result either.

More precisely, I'm trying to create a SurfaceView and fill it with a single colour.

Here goes my code:

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

        SurfaceRenderer renderer = new SurfaceRenderer(this);
        setContentView(renderer);
    }
}

class SurfaceRenderer extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "Svetlin Surface Renderer";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;

    public SurfaceRenderer(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {}

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

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, 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) {
        Log.i(TAG, "Drawing...");
        canvas.drawRGB(255, 128, 128);
    }
}

As expected, I'm getting Drawing... outputted twice.

Could anyone suggest why I might no be able to draw over the SurfaceView?

回答1:

I removed the proxy class SurfaceRenderer and moved it all into my activity. It's working now. Here goes the fixed code:

public class SvetlinSurfaceViewTestActivity
    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);
    }
}