I am using a customized version of FingerPaint for Android with some other features, like inserting images and moving them. I decided to implement an Undo&Redo, since it will make life just easier. In order to implement it, I finally decided to use a Stack where I push the Drawing Cache of the view, and from where I push the content every time I want to go back to a previous state. So, using the FingerPaint as a basis, I have the following:
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// I enable the set drawing cache...
myView.setDrawingCacheEnabled(true);
// ... and I add the cache to the stack
undoStack.add(myView.getDrawingCache());
indexOfUndoRedo++;
// kill this so we don't double draw
mPath.reset();
}
The stack is being updated only after the touch up at the moment, since I am still figuring out how to solve this. When I want to apply redo, I do the following:
private void undo() {
myView = new MyView(getActivity());
myView.setBackgroundDrawable(new BitmapDrawable(undoStack.get(indexOfUndoRedo)));
indexOfUndoRedo--;
myView.invalidate();
}
So far, the application shows the original state of the screen with no change. I also tried to paint it with a white background in order to reset it, but this approach is also not working.
Any idea or suggestion on how to fix this? I would be really thankful 🙂
Regards
Try below code Draw View:
and Draw Activity layout code below:
and Draw Activity Class below code:
This is sample paint app with undo and redo operations in android,it work’s perfectly for me!