public class DrawView extends View{ //定义记录前一个拖动时间发生点的坐标 float preX; float preY; private Path path; public Paint paint = null; //定义一个内存中的图片,该图片将作为缓存区 Bitmap cacheBbitmap = null; //定义cacheBbitmap上的Canvas对象 Canvas cacheCanvas = null; public DrawView(Context context,int width,int height) { super(context); cacheBbitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); cacheCanvas = new Canvas(); path = new Path(); //设置cacheCanvas将会会知道内存中的cacheBbitmap中 cacheCanvas.setBitmap(cacheBbitmap); //设置颜色 paint = new Paint(Paint.DITHER_FLAG); paint.setColor(Color.RED); //设置风格 paint.setStyle(Paint.Style.STROKE);//实心 paint.setStrokeWidth(1); paint.setAntiAlias(true); paint.setDither(true); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN : path.moveTo(x,y); preX = x; preY = y; break; case MotionEvent.ACTION_MOVE : path.quadTo(preX,preY,x,y); preX = x; preY = y; break; case MotionEvent.ACTION_UP : cacheCanvas.drawPath(path,paint); path.reset(); break; } invalidate(); return true; } @Override protected void onDraw(Canvas canvas) { Paint bmpPaint = new Paint(); //显示缓存 // cacheCanvas.drawPath(path,paint); canvas.drawBitmap(cacheBbitmap,0,0,bmpPaint); //显示绘制的过程 canvas.drawPath(path,paint); super.onDraw(canvas); } }
简单的手绘板 利用bitmap缓存
最新推荐文章于 2022-08-04 11:21:01 发布