Android add undo feature to finger paint example in api demo

后端 未结 3 1096
时光取名叫无心
时光取名叫无心 2020-12-16 08:58

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

3条回答
  •  长情又很酷
    2020-12-16 09:28

    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);
      }
      }
    

提交回复
热议问题