Search code examples
androidandroid-canvassurfaceviewundo-redo

Redo and Undo for shapes using SurfaceView in android


I am trying to develop an app similar to paint app.

I want allow user to perform redo or undo operations over canvas. I searched a lot, but dint find any example that explains about redo and undo operations for Circle, Rectangle etc. most of the tutorials explain redo and undo for Line.

Any help will be appreciated.


Solution

  • Store every operation in a list, then if you want to undo something just remove the last thing you put into the list. A linkedlist or a stack would work.

    Pseudocode

    Stack<Action> operations=new Stack<Action>();
    Stack<Action> redos=new Stack<Action>();
    

    Every time user have done something do

    operations.push(new Action(actiontype,ccoordinates));
    

    for undo

    redoes.push(operations.pop());
    

    to redo

    operations.push(redos.pop());
    

    and in your onDraw() method you draw everything thath is in operations...