1

我创建了一个使用画布和铅笔工具等的应用程序。我现在需要的是一个擦除工具,它可以清除前面的行。我尝试了使用背景颜色作为铅笔的擦除。我不需要那个。我实现了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);

}

我将此方法用于应用程序中的橡皮擦按钮。但它不起作用。它不会清除之前绘制的线条。

编辑的完整代码这不是完整的代码,因为我不能透露我的代码。我发布了其中的一部分来详细解释。

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);
4

0 回答 0