Android控件之SurfaceView初步了解

1.SurfaceView 说明:
1)使用了双缓存技术,显示效率非常快,对内存与cpu的开销非常大
A.加载数据 显示界面
B.显示界面 加载数据
2)特点:
a.可以绘制
b.可以在多个多个线程同时修改界面
线程要求:在surfaceCreated时创建,在surfaceDestroyed是销毁

2.绘制:
1)必须获得surface, 通过holder.getSurface();
2)surface.lockCanvas(); 锁定画布
canvas.draw() ;绘制界面
surface.unLockCanvasAndPost();解锁并提交

public class PlantView extends SurfaceView implements SurfaceHolder.Callback{
    private boolean isRuning = false;
    private SurfaceThread mThread;
    private SurfaceHolder mHolder;

    public PlantView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public PlantView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public PlantView(Context context) {
        super(context);

        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    @Override/**创建是调用*/
    public void surfaceCreated(SurfaceHolder holder) {
        mThread = new SurfaceThread();
        isRuning = true;
        mThread.start();
    }

    @Override/**改变时是调用*/
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override/**销毁时调用*/
    public void surfaceDestroyed(SurfaceHolder holder) {
        //停止线程
        isRuning = false;

    }


    private class SurfaceThread extends Thread{
        @Override
        public void run() {
            while(isRuning){
                try {
                    drawUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /** * 绘制矩形 */
    public void drawUI() {
        //锁定界面
        Canvas canvas = mHolder.lockCanvas();
        //画矩形
        canvas.drawRect(0, 0, 100, 100, new Paint());
        //解锁并提交
        mHolder.unlockCanvasAndPost(canvas);
    }


}

你可能感兴趣的:(android,控件,surfaceVie,surfaceHol)