2011-10-03 38 views
2

嗨,我想添加撤消功能,在API演示中给出的手指绘画示例。但我无法实现它。到目前为止,我已经添加了列表中绘制的所有路径,并将其重绘到除最后一个路径之外的画布。但它不起作用。任何线索,我错了。Android添加撤消功能,以手指绘画示例在API演示

编辑#1:我的Undo方法是在推进这一

private void undo(){    
     if (MyPathList.size()>0) { 
      mCanvas.drawColor(0xFFFFFFFF); 


      for (int i = 0; i < MyPathList.size()-1; i++) { 
       Path p=(Path)MyPathList.get(i);    
       mCanvas.drawLine(0, 0, 20, 20, mPaint); 
       mCanvas.drawLine(0, 0, 80, 20, mPaint); 
       mCanvas.drawPath(p, mPaint); 
       p.reset();    
      }    
      invalidate(); 
     }   
    } 

感谢。

回答

1

当你现在使用绘图等,使用它的手指现在存储这个点进去temp_array列表画线像当前进程。现在将这个temp_array列表添加到主数组列表中,这将在这里绘制所有的子数组列表点。

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

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

总是重新初始化到onTouch()在关闭事件的方法以获得在移动时所有的点得到的地步,现在高达事件存储到这个列表将这个数组列表存储到主列表中。

现在如果要撤消上次以上你可以从mainPoint数组列表

+0

1+为答案...它的工作正常...但在弧的情况下...如何去... ...和收集点时,它不是返回连续点,而是长线三点和短线两点... – Dinash

2
+0

删除最后添加temp_point名单我通过已经去了,他们已经使用了一些设计模式在那边。我的主要问题是为什么上面的代码正在工作......还有其他简单的方法来解决它。 – Dinash

+0

一旦你画在画布上,画就会卡在画布上。您需要在需要更新时清除画布,就像上面的教程一样。认为它像翻页漫画。你必须绘制每个页面相同的图像,并略有不同。在Android或其他编程语言中,您可以获得1页,并且您只需清除画布并重绘即可。你可能想知道canvas.save和canvas.restore是做什么的。 http://stackoverflow.com/questions/3051981/why-do-we-use-canvas-save-or-canvas-restore在这里你会得到这个想法。 –

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