Use and understanding of surface view

Drawing interface stuck has always been the pain of Android developers. Today, let's look at surface view, known as sub thread drawing, double buffering mechanism, no stuck.

SurfaceView has the following features:

    1 have independent drawing surface;

    2. We need to dig a hole in the host window to show ourselves;

    3. Its UI can be drawn in a separate thread, so that complex UI can be drawn.
    
    4. At the same time, it will not affect the main thread of the application program and respond to user input, that is, it will not jam.

Because View is drawn in the main UI thread, the main thread will be blocked when drawing. If there are more ontouch events processed, the interface card will be caused.

And surfaceView is another thread drawing, plus double buffering mechanism, so it should be efficient. No card.

Two interfaces for surfaceView:

1´╝îSurfaceHolder.CallBack

2´╝îRunnable

The methods to be implemented in the first interface correspond to the life cycle of SurfaceView, namely creation, change and destruction. The specific code is as follows:

//Life cycle of Surface
@Override
public void surfaceCreated(SurfaceHolder holder) {
 
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    
}

Insert the code slice here and the second interface needs to implement the run method for draw ing operation in the sub thread.

Note that surfaceView calls surfaceCreated in callback only when it is displayed. When it is displayed, it will not be called during initialization.
Surfacetestroyed in callback is called when it is hidden.

An example is as follows:

package com.bryant.view.surfaceview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by bryant on 2019/6/2
 * Test code.
 */

public class SurfaceViewCsCode extends SurfaceView implements SurfaceHolder.Callback,Runnable{
    
    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    private boolean mIsDrawing;
    
    //Construction method
    public SurfaceViewCsCode (Context context) {
        super(context);
        initView();
    }

    

    public SurfaceViewCsCode (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SurfaceViewCsCode (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void initView() {
        mHolder=getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsDrawing=true;
        new Thread(this).start();
        
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsDrawing=false;

    }

    @Override
    public void run() {
        while (mIsDrawing){
            draw();
            //Control refresh rate by thread sleep time
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    private void draw() {
        try {
            mCanvas=mHolder.lockCanvas();
            //Initialize the canvas and draw something on it
        }catch (Exception e){
            
        }finally {
            //Judge whether the canvas is empty to avoid black screen
            if(mCanvas!=null){
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }
}

Keywords: Mobile SurfaceView Android

Added by bob_dole on Thu, 31 Oct 2019 07:01:14 +0200