Skip to content Skip to sidebar Skip to footer

How To Implement Touch Smooth Image Eraser In Android?

I have already seen fingurePaint.java from API demos. I want to implement touch smooth eraser to erase parts of the image by touch move in android. fingurePaint told me to impleme

Solution 1:

First Declar your paint with all property in your constructor.

Write this code in your onDraw() method

@Override
protected void onDraw(Canvas canvas) 
   {
    System.out.println("come in on draw......");
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(mBitmap, 0, 0, mPaint);
            if(eraser==true)
               mPaint.setColor(Color.TRANSPARENT):
            else
               mPaint.setColor(Color.RED):
    canvas.drawPath(mPath, mPaint);

    super.dispatchDraw(canvas);
}

second solution:

call below method in your touch_move() method

mBitmap.setPixel(x, y, Color.TRANSPARENT); This method will change your bitmap you have to pass only X,Y & COLOR

public void changeBitmap(int x, int y, Bitmap mBitmap)
{
 Bitmap tempBitmap = Bitmap.createBitmap(mBitmap); //Edited code
 tempBitmap.setPixel(x, y, Color.TRANSPARENT);
}



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);
            mX = x;
            mY = y;
            changeBitmap(x, y, your_bitmap)
        }
    }

Solution 2:

Isn't it better to invert this case ? Just paint on your image with background color and after all when saving, merge those layers if needed.


Solution 3:

Define a temporary canvas and bitmap then draw your path or line, circle, image anything on touch events and then pass this temporary bitmap to canvas in onDraw your work will done properly, and also if you want to do erasing then do your erasing on that temporary canvas.

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        TemporaryBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
        TemporaryCanvas = new Canvas(TemporaryBitmap);
}
TemporaryCanvas.drawColor(0, PorterDuff.Mode.clear);
public void onDraw(Canvas canv){
canv.drawBitmap(TemporaryBitmap, matrix, paint);

Post a Comment for "How To Implement Touch Smooth Image Eraser In Android?"