/** * <p> * 扩展SurfaceView的基础模版代码 * SurfaceViewTemplate */ public class SurfaceViewTemplate extends SurfaceView implements SurfaceHolder.Callback, Runnable { protected SurfaceHolder mHolder; protected Canvas mCanvas; protected volatile boolean mIsDrawing; // 用于控制子线程 protected Paint mPaint; public SurfaceViewTemplate(Context context) { this(context, null); } public SurfaceViewTemplate(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SurfaceViewTemplate(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } protected void init() { mHolder = getHolder(); mHolder.addCallback(this); setFocusable(true); setFocusableInTouchMode(true); 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; } public void stopDraw() { mIsDrawing = false; } @Override public void run() { while (mIsDrawing) { draw(); } } protected void draw() { try { mCanvas = mHolder.lockCanvas(); // TODO draw anything drawAnyThing(mCanvas); } catch (Exception e) { e.printStackTrace(); } finally { if (mCanvas != null) { mHolder.unlockCanvasAndPost(mCanvas); // 解锁并且提交绘制结果 } } } /** * 子类重写该方法 绘制自定义内容 * * @param mCanvas */ protected void drawAnyThing(Canvas mCanvas) { } }
SurfaceView--扩展SurfaceView的基础模版代码
最新推荐文章于 2018-05-03 16:17:08 发布