public class SurfaceViewTempalte extends SurfaceView implements Callback, Runnable { private SurfaceHolder mHolder; private Canvas mCanvas; // 线程 Thread t; // 线程开关 private boolean isRunning; public SurfaceViewTempalte(Context context) { this(context, null); } public SurfaceViewTempalte(Context context, AttributeSet attrs) { super(context, attrs, 0); mHolder = getHolder(); mHolder.addCallback(this); //设置可获得焦点 setFocusable(true); setFocusableInTouchMode(true); //设置常亮 setKeepScreenOn(true); } @Override public void surfaceCreated(SurfaceHolder holder) { isRunning = true; t = new Thread(this); t.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { isRunning = false; } @Override public void run() { //不断进行绘制 while(isRunning) { draw(); } } private void draw() { //try防止back无释放 try { mCanvas = mHolder.lockCanvas(); if(mCanvas != null) { //绘制 } } catch (Exception e) { } finally { //释放mCanvas if(mCanvas != null) { mHolder.unlockCanvasAndPost(mCanvas); } } } }