Android Canvas Redo and Undo Operation

At first glance I see the following problems:

  • By adding your empty Path to paths as soon as you make it, you’re going to have a problem as soon as you undo: you’re popping that empty Path first, making the first undo not seem to work. Then if you draw into that Path, it’s not added to paths. The solution is to add the completed Path to paths in touch_up() before creating a new one.

That is, remove

paths.add(mPath);

from the constructor, and in touch_up(), change

mPath = new Path();
paths.add(mPath);

to

paths.add(mPath);
mPath = new Path();

You’ll also want to add

canvas.drawPath(mPath, mPaint);

after your for loop in onDraw() in order to draw the in-progress Path.

  • You’re not emptying undonePaths when the user starts drawing again.

Leave a Comment