Android add undo feature to finger paint example in api demo

后端 未结 3 1059
时光取名叫无心
时光取名叫无心 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:27

    When you drawing line for the current process like at now use drawing the like using it finger now store this points into temp_array list. now add this temp_array list into the main array list which will draw all sub array list point like wise here.

    List<List<Point> main_Points = new ArrayList<List<Point>>();
    List<Point> temp_point; // this one is for the current drawing
    

    like onTouch()

    onTouch(MotionEvent event){
         int x = (int) event.getX();
         int y = (int) event.getY();
    
         if(event.getAction==MotionEvent.DOWN){
               temp_point = new ArrayList<Point>();
               temp_point.add(new Point(x,y);
         }else if(event.getAction==MotionEvent.MOVE){
               if(temp_point!=null)
                  temp_point.add(new Point(x,y);
         }else if(event.getAction==MotionEvent.UP){
               mainPoint.add(temp_point);
               temp_point = null;
         }
         return true;
    }
    

    always re-initialize into onTouch() method in down event and get all the point at move time get the point and store into this list now at up event time store this array list into main list.

    now if you want to undo last or more you can just remove last add temp_point list from the mainPoint array List

    0 讨论(0)
  • 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 ArrayList<Path>unDonePathArray= new ArrayList<Path>();
        Public ArrayList<Path>patharray= new ArrayList<Path>();
        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);
      }
      }
    
    0 讨论(0)
  • 2020-12-16 09:32

    Maybe this tutorial is what your looking for: http://www.tutorialforandroid.com/p/drawing-with-canvas-series.html

    0 讨论(0)
提交回复
热议问题