Is there any possibility to call SurfaceView.onDraw()
method immidately? The documentation says after calling the method invalidate()
, method onDraw()
will be called at some point in the future. I am working with Camera API and modifying preview frames. Here is my code:
Activity which implements Camera.PreviewCallback
:
@Override
public void onPreviewFrame(byte[] data, Camera camera {
previewLayout.removeView(drawView); //RelativeLayout, which actually represents preview frames
drawView = new CustomSurfaceView();
previewLayout.addView(drawView);
drawView.invalidate(); //onDraw not called
setRectangle(drawView.getRect()); //this object is created in onDraw method, which was not yet called
}
CustomSurfaceView:
@Override
public void onDraw(Canvas canvas) {
rect = new Rect(..,..,..,..);
canvas.drawRect(rect);
}
public Rect getRect() {
return Rect;
}
I don't think there is an answer to my question in that threads. I'm doing everything mentioned there:
- putting custom SurfaceView in another RelativeLayout, where I'm displaying preview frames
- onDraw method is implemented in another SurfaceView, which is then added above the preview frames SurfaceView
Actually, I had a mistake in my sample code, not previewLayout but variable drawView was the SurfaceView I had to invalidate. But it still not works, onDraw is called only after onPreviewFrame.
By the way, I'm making face detection application such as in one of the possible duplicates. The thing is I want to capture an image of cropped rectangle with the face detected, but sometimes there is not an instance of Rect in drawView, because method onDraw was not yet called, so my app crashes. My question is still the same: is it possible to invoke onDraw method of my sample view immediately in onPreviewFrames method?