Android Paint PorterDuff.Mode.CLEAR drawing black

2019-05-21 17:31发布

问题:

I want to implement eraser for my painting app . I am able to erase with the following code

 paint.setColor(0x00000000);
 paint.setAlpha(0x00);

But after erasing when you start painting again it does not paint properly so any idea to erase paint please suggest.

回答1:

Try the below code.

      paint.setAlpha(0xFF);//transperent color
      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//clear the draw

Also have a look at the sample FingerPaint.java in the api demos under the folder graphics.

 setAlpha(int a)

Helper to setColor(), that only assigns the color's alpha value, leaving its r,g,b values unchanged.

http://developer.android.com/reference/android/graphics/Paint.html. Have a look a the documentation.

Edit:

Also check this

https://code.google.com/p/android/issues/detail?id=54105#c1



回答2:

I had same issue.

Need to set view's setLayerType.

yourView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

You can set it in constructor or by view's object.

Its done.



回答3:

This is should resolve this issue

private void touch_move(float x, float y){
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
        mPath.moveTo(mX, mY);

        mX = x;
        mY = y;
    }
}

in touch_up() change it to:

private void touch_up() {

     mPath.reset();
}