I have done the code for drawing a line through finger on canvas & also achieved "Undo" kind of functionality. Undo works pretty fine for the lines which doesn't cross each other, but when the lines cross each other & I undo previous line, it affects the another line as well at the "crossed" point, please have a look at the pics
for drawing I used this code
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(16);
mPaint.setXfermode(null);
//In MotionEvent.ACTION_DOWN:
mPath.reset();
mPath.moveTo(x, y);
// In MotionEvent.ACTION_MOVE:
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
// In MotionEvent.ACTION_UP:
mPath.lineTo(mX, mY);
circlePath.reset();
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
Now from ACTION_DOWN to ACTION_UP i keep track of all the x,y coordinates to use them for undo feature & here's how i Undo
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
// This helps to have undo kind of effect
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
rest code for ACTION_UP, ACTION_DOWN & ACTION_MOVE
is same. so basically
i just draw another line on same x-y coordinates with
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
& that results same as marked in pic with red circle.
So how can I only erase the part of particular line only even though they are having same x-y coordinates, can I just convert the drawn lines to ImageView
/ Bitmap
after it's drawn so I can remove the ImageView
it-self & it don't affect the another line?
Or is there any better way to achieve this?