SurfaceView小结

2019-03-19  本文已影响0人  Dane_404

1、使用SurfaceView的原因

View刷新重新绘制的时间间隔是16ms,如果可以在16ms以内绘制完, 则没有什么问题,如果绘制逻辑复杂,更新又很频繁,这样界面就会卡顿,就必须使用SurfaceView了。
区别:

2、使用SurfaceView

做个小Demo,一个涂鸦Demo

涂鸦:
public class DoodleSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    
        private SurfaceHolder mHolder;
        private Paint mPaint;
        private Canvas mCanvas;
        private Path mPath;
    
        public DoodleSurfaceView (Context context) {
            this(context, null);
        }
    
        public DoodleSurfaceView (Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public DoodleSurfaceView (Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
            initPaintAndPath();
        }
    
        private void initPaintAndPath() {
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(5);
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.BLACK);
            mPath = new Path();
        }
    
        private void init() {
            mHolder = getHolder();
            mHolder.addCallback(this);
            setFocusable(true);
            setKeepScreenOn(true);  //保持屏幕常亮
            setFocusableInTouchMode(true);
        }
    
        //创建时调用
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mCanvas = mHolder.lockCanvas();
            mCanvas.drawColor(Color.WHITE);
            mHolder.unlockCanvasAndPost(mCanvas);
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    
        private void draw() {
            mCanvas = mHolder.lockCanvas();
            mCanvas.drawColor(Color.WHITE);
            mCanvas.drawPath(mPath, mPaint);
            mHolder.unlockCanvasAndPost(mCanvas);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPath.moveTo(x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mPath.lineTo(x, y);
                    draw();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        }
 }
上一篇下一篇

猜你喜欢

热点阅读