29、SurfaceView

一、SurfaceView

1.1、SurfaceView和View

Android系统提供View进行绘图处理,View可以满足大部分的绘图需求,但是View有一个弊端,

Android通过发出VSYNC信号进行屏幕的重绘,刷新的间隔时间为16ms,如果在16ms内View完成

所需的操作,用户视觉上则不会产生卡顿的感觉。而如果执行的逻辑操作太多,特别是频繁刷新界面,

那么就会不断阻塞主线程,从而导致画面卡顿。

Skipped 1188 frames!  The application may be doing too much work on its main thread.

为避免该情况的发生,Android系统提供SurfaceView组件来解决该问题。它和View的区别如下:

  • View主要适用于主动更新的情况下,而SurfaceView主要适用于被动更新,例如频繁刷新。
  • View在主线程中对画面进行刷新,而SurfaceView通常在子线程进行页面刷新。
  • View在绘图时没有使用双缓冲机制,而SurfaceView在底层实现机制中已经实现双缓冲机制。

总结:如果自定义View需要频繁刷新,或者刷新数据量比较大则使用SurfaceView。

1.2、SurfaceView的使用

a)创建自定义的类继承SurfaceView,并实现两个接口--SurfaceHolder.Callback和Runnable,

b)在构造函数中初始化SurfaceView,通常定义三个成员变量。

c)在使用时,同SurfaceHolder对象的lockCanvas()方法可获得当前Canvas绘图对象。

d)绘制的时候,在surfaceCreate()方法中开启子线程进行绘制,子线程中使用while(mIsDrawing)来循环不停绘制。

e)绘制的具体逻辑中,通过lockCanvas()方法获取Canvas对象进行绘制,并通过unlockCanvasAndPost(mCanvas)方法对画布内容提交。

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    
    private SurfaceHolder mHolder;   // SurfaceHolder
    private Canvas mCanvas;          // 用于绘图的Canvas
    private boolean mIsDrawing;      // 子线程标志位
    public MySurfaceView(Context context) {
        this(context, null);
    }
    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        
    }
    private void initView() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
        //mHolder.setFormat(PixelFormat.OPAQUE);
    }
    /**下面三个方法分别对应SurfaceView的创建、改变、销毁过程*/
    @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) {  
    }
    
    @Override
    public void run() {
        while(mIsDrawing){
            draw();
        }
    }
    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();
            // draw something
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(mCanvas != null){
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }
}

以上代码基本上可以满足大部分的SurfaceView绘图需求,注意在finally中的代码,保住每次都能将内容提交。

1.3、绘制正弦曲线

要绘制正弦曲线,只需要不断修改横坐标的值,并让它们满足正弦函数即可。

因此使用Path对象来保存正弦函数上的坐标点,在子线程的while循环中,不断改变横纵坐标值。

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    private SurfaceHolder mHolder;
    private Canvas mCanvas;
    private boolean mIsDrawing;
    private int x = 0;
    private int y = 0;
    private Path mPath;
    private Paint mPaint;
    public MySurfaceView(Context context) {
        super(context);
        initView();
    }
    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }
    public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }
    private void initView() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
        mPath = new Path();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsDrawing = true;
        mPath.moveTo(0, 400);
        new Thread(this).start();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height) {
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsDrawing = false;
    }
    @Override
    public void run() {
        while (mIsDrawing) {
            draw();
            x += 1;
            y = (int) (Math.sin(x * 2 * Math.PI / 180 * 100) + 400);
            mPath.lineTo(x, y);
        }
    }
    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();
            // SurfaceView背景
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawPath(mPath, mPaint);
        } catch (Exception e) {
        } finally {
            if (mCanvas != null)
                mHolder.unlockCanvasAndPost(mCanvas);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/pengjingya/p/5510086.html
29