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:
- Put your
SurfaceView
inside of aFrameLayout
orRelativeLayout
inyour layout XML file, since both ofthose allow stacking of widgets onthe Z-axis - Move your drawing logicinto a separate custom
View
class - Add an instance of the custom Viewclass to the layout XML file as achild of the
FrameLayout
orRelativeLayout
, but have it appearafter theSurfaceView
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.