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
?