Rotating turntable __surfaceview

Brief description:

SufaceView extends View

View is drawn in the UI thread,

SurfaceView is drawn in a sub-thread . Games generally use SurfaceView. Advantage: Avoid blocking the UI thread.

SurfaceView contains a Surface dedicated to drawing, and Surface contains a Canvas.

How to get Canvas?

getHolder() -> SurfaceHolder (Surface holder)

The role of holder

Function 1: holder -> Canvas (holder gets Canvas, in Drawing on Canvas)

Function 2: When to draw? (Custom View has an onDraw method, and you can draw a custom View shape in it)

Manage the life cycle of the SurfaceView

Manage the life cycle of the SurfaceView: the child of the drawing life cycle: surfaceCreated Thread is created, and surface is drawn in the run method of sub-thread

surface

The writing template of surfaceView:

package com.example .surfaceviewdemo;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class SurfaceViewTempalte extends SurfaceView implements Callback, Runnable {private SurfaceHolder mHolder;// Manage SufaceView private Canvas mCanvas;// Drawing SurfaceView is obtained by holder /** Child thread for drawing*/ private Thread t; /** Thread control switch*/ private boolean isRunning; public SurfaceVi ewTempalte(Context context, AttributeSet attrs) {super(context, attrs); // TODO Auto-generated constructor stub mHolder = getHolder(); // Obtain Holder mHolder.addCallback(this); // Binding management life cycle function / / Perform simple settings // Get focus setFocusable(true); setFocusableInTouchMode(true); // Set the screen to always be on, depending on the situation setKeepScreenOn(true);} public SurfaceViewTempalte(Context context) {this(context, null) ; // TODO Auto-generated constructor stub} @Override public void surfaceCreated(SurfaceHolder holder) {// When SurfaceView is created, start the child thread isRunning = true; t = new Thread(this); t.start();} @ Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {// TODO Auto-generated method stub} /** * When Surface is destroyed, close the thread */ @Override public void surfaceDestroyed(SurfaceHolder holder) { isRunning = false;} /** * Method of drawing Surface by sub-thread */ @Override public void run() {// Continue drawing while (isRunning) {draw();// Drawing method}} /** Draw The method of */ private void draw() {try {// 1) Lock the canvas mCanvas = mHolder. lockCanvas(); //Why do you want to judge null? //Reason 1: When the Surface is in the main interface, clicking the home button or the back button will destroy the Surface. At this time, it may be drawing and the code is still executing here. The mCanvas is empty. To prevent the null pointer exception, //Reason 2: The surfaceView is destroyed, the Thread thread may not be closed, and the operation is still in progress, there will be a null pointer exception if (mCanvas != null) {// 2) draw something draw according to demand}} catch (Exception e) {e.printStackTrace();} finally {// 3) Release Canvas if (mCanvas != null) {mHolder.unlockCanvasAndPost(mCanvas);}} }) 

Leave a Comment

Your email address will not be published.