Search code examples
androidcanvasbitmapandroid-custom-viewpaint

How to invert mask?


I am trying to invert image mask bitmap using below code

    static final PorterDuffXfermode eraseMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);

public void invertSelection() {
    Bitmap inverted = Bitmap.createBitmap(imageBitmap.getWidth(), imageBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    if (!annotationBitmap.sameAs(inverted)) {
        Canvas canvas = new Canvas(inverted);
        paint.setColor(Color.RED);
        canvas.drawPaint(paint);
        paint.setXfermode(eraseMode);
        canvas.drawBitmap(annotationBitmap, 0,0,paint);
        annotationBitmap = inverted;
        undoStack.push(annotationBitmap.copy(annotationBitmap.getConfig(), true));
        invalidate();
    }
}

after calling this function I am no longer able to draw on annotationBitmap.

What am I doing wrong here???


Solution

  • I think you should read this beautiful article at medium and i am not sure but i think if you changePorterDuff.Mode.CLEAR to PorterDuff.Mode.DST_OUT your problem will solve

    enter image description here