Search code examples
androidpaint

How to implement an eraser for a drawing app in Android


I have been searching all evening for some way to implement an eraser function for my drawing app. The most common answer is to simply paint the background color or image in, but this solution will not work for my application because I am implementing multiple layers (Gimp/Photoshop style).

The user should be able to draw a line with the brush tools provided in as many layers as they like (the onDraw method of my drawingview draws layer0...layerX on top of each other). Then if they choose the eraser tool it should cause any area of the current layer that they trace over to become transparent.

I cannot seem to find an appropriate class/function built in and am unsure how I could write it myself. I tried to do something like

Paint paint = new Paint();
paint.setAlpha(0);

and then use that Paint object to draw with, but that only draws an 'invisible' line.

I also attempted to use

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

but that just seemed to draw as normal and without effect. I'm probably using it incorrectly, but the Android documentation does not contain a clear description of what it does. I just happened to see it in some examples about modifying bitmaps.

I can supply code as needed, I am just not sure what would be helpful to solve my problem. Being pointed in the right direction would be the biggest help as I have not been successful with Google.


Solution

  • You can find the implementation of eraser function, create new canvas, brush function and save function, on this link :

    create android drawing interface

    it's have very good tutorial for making drawing app on android using motion event.