How to draw an overlay on a SurfaceView used by Camera on Android? How to draw an overlay on a SurfaceView used by Camera on Android? android android

How to draw an overlay on a SurfaceView used by Camera on Android?


SurfaceView probably does not work like a regular View in this regard.

Instead, do the following:

  1. Put your SurfaceView inside of aFrameLayout or RelativeLayout inyour layout XML file, since both ofthose allow stacking of widgets onthe Z-axis
  2. Move your drawing logicinto a separate custom View class
  3. Add an instance of the custom Viewclass to the layout XML file as achild of the FrameLayout orRelativeLayout, but have it appearafter the SurfaceView

This will cause your custom View class to appear to float above the SurfaceView.


Try calling setWillNotDraw(false) from surfaceCreated:

public void surfaceCreated(SurfaceHolder holder) {    try {        setWillNotDraw(false);         mycam.setPreviewDisplay(holder);        mycam.startPreview();    } catch (Exception e) {        e.printStackTrace();        Log.d(TAG,"Surface not created");    }}@Overrideprotected void onDraw(Canvas canvas) {    canvas.drawRect(area, rectanglePaint);    Log.w(this.getClass().getName(), "On Draw Called");}

and calling invalidate from onTouchEvent:

public boolean onTouch(View v, MotionEvent event) {    invalidate();    return true;}


I think you should call the super.draw() method first before you do anything in surfaceView's draw method.