android 橡皮擦 黑色,Android画板,橡皮擦为黑色痕迹的问题
本人小菜鸟一枚,最近想做画板(用的疯狂Android讲义中,双缓冲技术那种),遇到一个橡皮擦为黑色痕迹的问题,网上搜索资料,基础太差,实在看不懂,于是写下自己的解决办法,帮助跟我同样的小白。1.画线改用画圆问题2.改用画圆后出现,手松开后,橡皮擦效果才显示出来?解决:在onTouchEvent 的MotionEvent.ACTION_MOVE中,每次都将写到内存中区canvas.drawPath(
本人小菜鸟一枚,最近想做画板(用的疯狂Android讲义中,双缓冲技术那种),遇到一个橡皮擦为黑色痕迹的问题,网上搜索资料,基础太差,实在看不懂,于是写下自己的解决办法,帮助跟我同样的小白。
1.画线改用画圆
问题2.改用画圆后出现,手松开后,橡皮擦效果才显示出来?
解决:在onTouchEvent 的MotionEvent.ACTION_MOVE中,每次都将写到内存中区canvas.drawPath(path, paint); ----------- -----就是每次移动时,都加上与手松开时调用的方法
第一次发,废话有点多,好吧,上代码(代码太烂,轻喷)
public class DrawView extends View{
float preX;
float preY;
private Bitmap cacheBitmap = null;
//画布
Canvas canvas = null;
//画笔
public Paint paint = null;
private Path path = null;
//屏幕宽度
private int width;
//屏幕高度
private int height;
public boolean isRubber = false;
public DrawView(Context context, AttributeSet set) {
super(context,set);
WindowManager wm = (WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
cacheBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
canvas = new Canvas();
canvas.drawColor(Color.TRANSPARENT);
path = new Path();
//给画布设置bitmap
canvas.setBitmap(cacheBitmap);
//初始化画笔信息
setPaint(Color.BLACK,Params.initStrokeWidth);
}
public void setPaint(int color){
isRubber = false;
setPaint(color,5);
}
//初始化画笔信息
public void setPaint(int color,int strokeWidth){
System.out.println("===========setPaint==========");
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(color);
// 设置画笔风格
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
// 反锯齿
paint.setAntiAlias(true);
paint.setDither(true);
}
//初始化画笔信息
public void setRubber(){
isRubber = true;
paint = new Paint(Paint.DITHER_FLAG);
// 设置画笔风格
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(Params.initStrokeWidth + 5);
// 反锯齿
paint.setAntiAlias(true);
paint.setDither(true);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("====MotionEvent.ACTION_DOWN===DOWN");
path.moveTo(x, y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_MOVE:
System.out.println("====MotionEvent.ACTION_MOVE:===MOVE");
path.quadTo(preX, preY, x, y);
preX = x;
preY = y;
if(isRubber){
//这样性能可能会很差,自己想办法解决
canvas.drawPath(path, paint); // 橡皮擦关键点================== 看这里 问题2解决============
}
break;
case MotionEvent.ACTION_UP:
System.out.println("====MotionEvent.ACTION_UP===UP");
canvas.drawPath(path, paint); //用画笔和路径画一张图片
path.reset(); //重置路径
break;
}
invalidate();//重新绘制 onDraw
return true;
}
@Override
protected void onDraw(Canvas canvas) {
Paint bmpPaint = new Paint();
// 将cacheBitmap绘制到该View组件上
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
if (isRubber) { //橡皮擦走这边 ======================看这里 问题一解决======================
canvas.drawCircle(preX, preY, 5, paint);
} else {
canvas.drawPath(path, paint);
}
}
}
更多推荐
所有评论(0)