双缓冲机制简介
Posted 技术丶从积累开始
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双缓冲机制简介相关的知识,希望对你有一定的参考价值。
一.理解双缓冲机制
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); } }
图片:
以上是关于双缓冲机制简介的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础
Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段