zoukankan      html  css  js  c++  java
  • 双缓冲机制简介

    一.理解双缓冲机制

    1.创建一个画板,和一个图片()

    2.将图片设置为画板

    3.之后画板将会在该图片上作画

    4.之后再将图片放到View提供的画板上显示

    二、实例(利用双缓冲机制的画板)

    public class DrawView extends View {
        private int view_width = 0;
        private int view_height = 0;
        private float prevX = 0;
        private float prevY = 0;
        private Path mPath;
        private Bitmap mCacheBitmap;
        private Canvas mCacheCanvas;
        private Paint mPaint;
        public DrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
            //获取屏幕的宽高
            WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            view_width = display.getWidth();
            view_height = display.getHeight();
            //创建双缓冲板
            mCacheBitmap = Bitmap.createBitmap(view_width, view_height, Bitmap.Config.ARGB_8888);//创建图片缓冲区
            mCacheCanvas = new Canvas(mCacheBitmap);//将该Canvas绘制到缓冲区中
            //创建Path
            mPath = new Path();
            //创建Paint
            mPaint = new Paint();
            mPaint.setStrokeWidth(3);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float currentX = event.getX();
            float currentY = event.getY();
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    prevX = currentX;
                    prevY = currentY;
                    mPath.moveTo(event.getX(), event.getY());
                    break;
                case MotionEvent.ACTION_MOVE:
                    prevX = currentX;
                    prevY = currentY;
                    mPath.quadTo(prevX,prevY,currentX,currentY);
                    break;
                case MotionEvent.ACTION_UP:
                    mPath.reset();
                    break;
            }
            mCacheCanvas.drawPath(mPath, mPaint);
            invalidate();
            //要将 return 改为 true 这样onTouchEvent()方法才会被执行
            return true;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            canvas.drawBitmap(mCacheBitmap,0,0,paint);
        }
    }
    View Code

    图片:

  • 相关阅读:
    简述智障版本搜索引擎架构
    kaggle PredictingRedHatBusinessValue 简单的xgboost的交叉验证
    机器学习速查表
    World final 2017 题解
    微博爬虫
    喵哈哈村的魔法考试 Round #21 (Div.2) 题解
    喵哈哈村的魔法考试 Round #20 (Div.2) 题解
    Tinkoff Challenge
    常用的机器学习&数据挖掘知识(点)总结
    喵哈哈村的魔法考试 Round #19 (Div.2) 题解
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5323364.html
Copyright © 2011-2022 走看看