hi i am trying to add undo feature to finger paint example given in API demo. but i am not able to achieve it. So far i have added all the path that are drawn in a list and
There is another way of achieving undo , you can use porterduff mode.clear to draw over the particular path which need to be undone
1. Store your paths to array of Paths during onTouch event.
2. check the size of stored path array, and get the last drawn path using size() method. i.e patharray.size()-1;
3. Store that to a separate path.
4. Call invalidate() method
5. Set your paint xfermode property to Mode.clear
6. Draw on canvas using the retrieved path and the newly set paint.
Public Path unDonePath,drawpath;
Public Boolean undo=false;
Paint paintForUndo= new Paint();
Public ArrayListunDonePathArray= new ArrayList();
Public ArrayListpatharray= new ArrayList();
public boolean onTouchEvent(MotionEvent event)
{
Switch(MotionEven.getAction())
{
case MotionEvent.ACTION_UP:
pathArray.add(drawpath);
pathToDraw = new Path();
//drawpath contains touchx and touch y co-ordinates.
}
}
Protected void undo()
{
if(pathArray.size()>0) {
unDonePath=new Path();
unDonePathArray.add(pathArray.remove(pathArray.size()-1));
unDonePath=unDonePathArray.get(unDonePathArray.size()-1);
unDonePathArray.clear();
invalidate();
}
}
public void onDraw(Canvas canvas)
{
if(undo)
{
paintForUndo.setStrokeWidth(12.0f);
/*stroke width have to be set more than or equal to your painted
stroke width, if ignored border of the painted region will be left
uncleared. */
paintForUndo.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.CLEAR));
drawOnCanvas.drawPath(unDonePath,paintForUndo);
}
}