Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:21:22+00:00 2026-06-11T02:21:22+00:00

I have already seen fingurePaint.java from API demos. I want to implement touch smooth

  • 0

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 implement this

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

But this is not working to erase the image. It is working to erase something which is drawn by touch.

public class SandboxView extends View implements OnTouchListener {
    public  final Bitmap bitmap;
    private final int width;
    private final int height;
    private Matrix transform = new Matrix();

    private Vector2D position = new Vector2D();
    private float scale = 1;
    private float angle = 0;
    public boolean isInitialized = false;
    private TouchManager touchManager = new TouchManager(2);
    final GestureDetector mGesDetect;


    // Debug helpers to draw lines between the two touch points
    private Vector2D vca = null;
    private Vector2D vcb = null;
    private Vector2D vpa = null;
    private Vector2D vpb = null;

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private Path    mPath;
    private Canvas  mCanvas;
    private Paint       mPaint;
    private Paint   mBitmapPaint;
    public SandboxView(Context context, Bitmap bitmap) {
        super(context);

        this.bitmap = bitmap;
        this.width = bitmap.getWidth();
        this.height = bitmap.getHeight();
        this.mGesDetect = new GestureDetector(context, new DoubleTapGestureDetector());

        setOnTouchListener(this);
    }


    private  float getDegreesFromRadians(float angle) {
        return (float)(angle * 360.0 / Math.PI);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (!isInitialized) {
            Bitmap mBitmap = bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPaint = new Paint();
            mPath = new Path();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFFFF0000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(12);
            mPaint.setAlpha(0);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mPaint.setAntiAlias(true);

            mBitmapPaint = new Paint(Paint.DITHER_FLAG);


            int w = getWidth();
            int h = getHeight();
            position.set(w / 2, h / 2);
            isInitialized = true;
        }
        if(isEraser==1){
            canvas.drawColor(80000000);


            canvas.drawBitmap(bitmap, transform, mBitmapPaint);

            canvas.drawPath(mPath, mPaint);
        }
        else{


        Paint paint = new Paint();

        transform.reset();
        transform.postTranslate(-width / 2.0f, -height / 2.0f);
        transform.postRotate(getDegreesFromRadians(angle));
        transform.postScale(scale, scale);
        transform.postTranslate(position.getX(), position.getY());

        canvas.drawBitmap(bitmap, transform, paint);

        try {
            /*paint.setColor(0xFF007F00);
            canvas.drawCircle(vca.getX(), vca.getY(), 64, paint);
            paint.setColor(0xFF7F0000);
            canvas.drawCircle(vcb.getX(), vcb.getY(), 64, paint);

            paint.setColor(0xFFFF0000);
            canvas.drawLine(vpa.getX(), vpa.getY(), vpb.getX(), vpb.getY(), paint);
            paint.setColor(0xFF00FF00);
            canvas.drawLine(vca.getX(), vca.getY(), vcb.getX(), vcb.getY(), paint);*/




        }
        catch(NullPointerException e) {
            // Just being lazy here...
        }
        }
    }

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    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;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }




    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if(isEraser ==1){
            float x = event.getX();
            float y = event.getY();

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


            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }
        else{
        vca = null;
        vcb = null;
        vpa = null;
        vpb = null;
        mGesDetect.onTouchEvent(event);

        try {
            touchManager.update(event);

            if (touchManager.getPressCount() == 1) {
                vca = touchManager.getPoint(0);
                vpa = touchManager.getPreviousPoint(0);
                position.add(touchManager.moveDelta(0));
            }
            else {
                if (touchManager.getPressCount() == 2) {
                    vca = touchManager.getPoint(0);
                    vpa = touchManager.getPreviousPoint(0);
                    vcb = touchManager.getPoint(1);
                    vpb = touchManager.getPreviousPoint(1);

                    Vector2D current = touchManager.getVector(0, 1);
                    Vector2D previous = touchManager.getPreviousVector(0, 1);
                    float currentDistance = current.getLength();
                    float previousDistance = previous.getLength();

                    if (previousDistance != 0) {
                        scale *= currentDistance / previousDistance;
                    }

                    angle -= Vector2D.getSignedAngleBetween(current, previous);
                }
            }

            invalidate();
        }
        catch(Throwable t) {
            // So lazy...
        }
        return true;
        }
    }
    class DoubleTapGestureDetector extends GestureDetector.SimpleOnGestureListener {


        @Override
        public boolean onDoubleTap(MotionEvent e) {
            colorseekbar.setVisibility(View.INVISIBLE);
            opacityseekbar.setVisibility(View.INVISIBLE);
            return true;
        }
    }

}

So please help me to erase the parts of image by using touch move.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:21:24+00:00Added an answer on June 11, 2026 at 2:21 am

    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)
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load activity with flip animation. I have already seen api demos
I want to combine AND OR mysql queries in CI. I have already seen
I googled,I binged,I already have seen the other duplicates here,but none of them work
I've seen that a few instances of this problem have been raised already. However,
I have seen the related question here: An object with the same key already
If you have seen my previous questions, you'd already know I am a big
im sure this is completely simple , infact i'm sure i have already seen
I already seen some question from here (stackoverflow) and THIS post, but I still
I'm taking some information from some variables I have already defined outside this function
I have already seen the method: GetRolesForUser But is there a way using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.