SurfaceView--扩展SurfaceView的基础模版代码

来源:互联网 发布:mac win linux 编辑:程序博客网 时间:2024/04/29 19:00
/** * <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) {    }}
原创粉丝点击