SurfaceCreated () not called SurfaceView Components

Sometimes we need to do rendering in the context of the surfaceview in the native layer. At this time, we just provide a separate do nothing Surfaceview.

The xml file is as follows:

 

< span style="white-space:pre">

The surfaceview file is as follows:

public class MirrorSurfaceView extends SurfaceView impl ements Runnable, SurfaceHolder.Callback {public MirrorSurfaceView(Context context, AttributeSet attrs) {super(context, attrs);} @Override public void run() {while(true){ if(getHolder().getSurface().isValid( )){ //Canvas canvas = getHolder().lockCanvas(); renderFrame(this.getHolder().getSurface()); //getHolder().unlockCanvasAndPost(canvas);}}} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {// TODO Auto-generated method stub} @Override public void surfaceCreated(SurfaceHolder holder) {initRender(this.getWidth(), this.getHeight()); new Thread(this ).start();} @Override public void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub} private native void initRender(int render_width, int render_height); private native void renderFrame(Surface surface);}  

is because the java layer thinks that the surfaceview has not done anything, so it will not do any initialization operation, that is, surfaceCreated() will not be called. How to solve this time?

There are two solutions:

< /span>1. Manually add a child control in surfaceview to trigger surfaceview to do lazy initialization. But doing this is sometimes because all of my rendering logic is native, and some are muddy and dirty.

2. Adding this sentence in the surfaceview constructor can also be solved:

getHolder().addCallback(this);

Leave a Comment

Your email address will not be published.