我创建了一个应用程序,其中我使用了画布和铅笔工具等。我现在需要的是一个擦除工具,它清除了之前的行。我尝试使用背景颜色作为铅笔的擦除。我不需要那个。我实施了PorterDuff.Mode
,但不知何故它没有用,所以请帮助我。
public void colorChanged22() {
// TODO Auto-generated method stub
canvasPathPaint.setXfermode(null);
canvasPathPaint.setColor(Color.TRANSPARENT);
canvasPathPaint.setAntiAlias(true);
canvasPathPaint.setDither(true);
canvasPathPaint.setAlpha(0);
canvasPathPaint.setStyle(Paint.Style.STROKE);
canvasPathPaint.setStrokeJoin(Paint.Join.ROUND);
canvasPathPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPathPaint.setStrokeWidth(12);
canvasPathPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
drawingBitmap = drawingBitmap.copy(Config.ARGB_8888, true);
}
我在应用程序中将此方法用于橡皮擦按钮。但它不起作用。它不会清除之前绘制的线条。
EDITED 完整的代码 这不是完整的代码,因为我无法透露我的代码。我发布了部分内容以详细解释。
PaintRunner.java
private final Xfermode eraseXfermode;
private final Canvas bitmapCanvas;
private final Paint bitmapPathPaint;
private final Paint canvasPathPaint;
public PaintRunner(PaintView paintView, Context context) {
surfaceHolder = paintView.getHolder();
commandManager = new CommandManager(context);
mContext = context;
super.setRunnable(new DrawLoop());
bitmapCanvas = new Canvas();
bitmapPathPaint = new Paint();
bitmapPathPaint.setColor(color);
bitmapPathPaint.setAntiAlias(true);
bitmapPathPaint.setDither(true);
bitmapPathPaint.setStyle(Paint.Style.STROKE);
bitmapPathPaint.setStrokeJoin(Paint.Join.ROUND);
bitmapPathPaint.setStrokeCap(Paint.Cap.ROUND);
bitmapPathPaint.setStrokeWidth(appContext.maxStrokeWidth() / 9);
canvasPathPaint = new Paint(bitmapPathPaint);
Bitmap checkerboard = BitmapFactory.decodeResource(appContext.getResources(), R.drawable.black);
DisplayMetrics metrics = (context).getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
checkerboard = Bitmap.createScaledBitmap(checkerboard, screenWidth, screenHeight, true);
BitmapShader shader = new BitmapShader(checkerboard, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
checkeredPattern = new Paint();
checkeredPattern.setShader(shader);
// eraseXfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
eraseXfermode = new Xfermode();
这是在单击橡皮擦按钮时调用的橡皮擦方法
canvasPathPaint.setXfermode(null);
canvasPathPaint.setMaskFilter(null);
canvasPathPaint.setColor(Color.TRANSPARENT);
canvasPathPaint.setAntiAlias(true);
canvasPathPaint.setDither(true);
canvasPathPaint.setAlpha(0);
System.out.println("color " + color);
canvasPathPaint.setStyle(Paint.Style.STROKE);
canvasPathPaint.setStrokeJoin(Paint.Join.ROUND);
canvasPathPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPathPaint.setStrokeWidth(12);
canvasPathPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// drawingBitmap.eraseColor(Color.TRANSPARENT);
drawingBitmap = drawingBitmap.copy(Config.ARGB_8888, true);