1

I have implement the Canvas and paint to draw on canvas. i am able to do the Paint on canvas with different colour.

Now, if there is white plain background then its ok. I am using white color for Eraserand its work fine. But if there is any image on the background and if i select eraser (as like white colour) then it is being paint of white color on the image.

Instead of that i want is, While i select eraser and if there is any image in background then the eraser should erase the paint and show the image. . So can anybudy help me how to implement it ?? Thanks.

Edited: I am using below code to Erase the doing paint. But still not able to erase it.

case R.id.eraserBtn:
            currentPaint = new Paint();
            currentPaint.setAlpha(0);         
            currentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));         
            currentPaint.setAntiAlias(true);

            //currentPaint.setColor(0x00000000);
            currentPaint.setDither(true);
            currentPaint.setStyle(Paint.Style.STROKE);
            currentPaint.setStrokeJoin(Paint.Join.ROUND);
            currentPaint.setStrokeCap(Paint.Cap.ROUND);
            currentPaint.setStrokeWidth(3);
            break;

New Edited:

 case R.id.eraserBtn:
            currentPaint = new Paint();
            currentPaint.setAlpha(0);         
            currentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));         
            currentPaint.setAntiAlias(true);

            //currentPaint.setColor(0x00000000);
            currentPaint.setDither(true);
            currentPaint.setStyle(Paint.Style.STROKE);
            currentPaint.setStrokeJoin(Paint.Join.ROUND);
            currentPaint.setStrokeCap(Paint.Cap.ROUND);
            currentPaint.setStrokeWidth(15);
            break;

Another Edit:

      while (_run){

                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }
                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    //canvas.drawColor(mColor);// Edited by Shreyash
                    c.drawColor(mColor);

//                    Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                      canvas.drawBitmap (kangoo, 0,  200,null);

//                  works for logo                  
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                  c.drawBitmap (kangoo, 0,  200,null);

                    if(!(DrawingActivity.imagePath==null)){
                        c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
                    }
                    commandManager.executeAll(c);
                    canvas.drawBitmap (mBitmap, 0,  0,null);

                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188

2 Answers2

2

You should set setXfermode for the paint object and look into the different PorterDuffModes.

This should prove useful to you: Erase bitmap parts using PorterDuff mode

Community
  • 1
  • 1
c05mic
  • 558
  • 5
  • 18
2

I am not exactly aware of your code. but setColor works for me. But you can try one of the following code though not an exact solution but for your reference...

  1. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
  2. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html
  3. Best one (as I suggest) : http://som-itsolutions.blogspot.com/2010/12/freeware-android-paint.html
DShah
  • 9,768
  • 11
  • 71
  • 127