您好我正在尝试将撤消功能添加到API演示中给出的手指画示例中。但我无法实现它。到目前为止,我添加了列表中绘制的所有路径,并将其重绘到除最后一个路径之外的画布。但它没有用。我出错的任何线索。
编辑#1:我的撤消方法就是这个
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();
}
}
先谢谢。
答案 0 :(得分:2)
答案 1 :(得分: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;
}
总是在down事件中重新初始化为onTouch()方法,并在移动时获取所有点,并在up事件时将此存储到此列表中,并将此数组列表存储到主列表中。
现在如果要撤消上一个或多个,可以从mainPoint数组列表中删除最后一个添加temp_point列表
答案 2 :(得分: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);
}
}