我正在绘制一个绘图应用程序,onTouchEvents是标准的,并且想要添加Undo()函数来删除最后绘制的路径.
声明:
int thelastLineId=0;
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintScreen; // use to draw bitmap onto screen
private Paint paintLine; // used to draw lines onto bitmap
private HashMap pathMap; // current Paths being drawn
private HashMap reservedpathMap; // for saving the paths being undone
private HashMap previousPointMap; // current Points
构造函数:
pathMap = new HashMap();
reservedpathMap = new HashMap (); // for storing path being undone
previousPointMap = new HashMap();
onDraw有:
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap,paintScreen);
// for each path currently being drawn
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key),paintLine); // draw line
}
的onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getActionMasked(); // event type
int actionIndex = event.getActionIndex(); // pointer (i.e.,finger)
if (action == MotionEvent.ACTION_DOWN)
{
touchStarted(event.getX(actionIndex),event.getY(actionIndex),event.getPointerId(actionIndex));
}
else if (action == MotionEvent.ACTION_UP)
{
touchEnded(event.getPointerId(actionIndex));
}
else
{
touchMoved(event);
}
invalidate();
return true;
}
touchStarted:
private void touchStarted(float x,float y,int lineID) // lineID represents how many fingers,1 finger 1 line
{
Path path; // used to store the path for the given touch id
Point point; // used to store the last point in path
// if there is already a path for lineID
if (pathMap.containsKey(lineID))
{
path = pathMap.get(lineID); // get the Path
path.reset(); // reset the Path because a new touch has started
point = previousPointMap.get(lineID); // get Path's last point
}
else
{
path = new Path(); // create a new Path
pathMap.put(lineID,path); // add the Path to Map
point = new Point(); // create a new Point
previousPointMap.put(lineID,point); // add the Point to the Map
}
path.moveTo(x,y);
point.x = (int) x;
point.y = (int) y;
}
touchMoved:
private void touchMoved(MotionEvent event)
{
// for each of the pointers in the given MotionEvent
for (int i = 0; i < event.getPointerCount(); i++)
{
// get the pointer ID and pointer index
int pointerID = event.getPointerId(i);
int pointerIndex = event.findPointerIndex(pointerID);
// if there is a path associated with the pointer
if (pathMap.containsKey(pointerID))
{
float newX = event.getX(pointerIndex);
float newY = event.getY(pointerIndex);
// get the Path and previous Point associated with this pointer
Path path = pathMap.get(pointerID);
Point point = previousPointMap.get(pointerID);
float deltaX = Math.abs(newX - point.x);
float deltaY = Math.abs(newY - point.y);
if (deltaX >= TOUCH_TOLERANCE || deltaY >= TOUCH_TOLERANCE)
{
path.quadTo(point.x,point.y,((newX + point.x)/2),((newY + point.y)/2));
point.x = (int) newX ;
point.y = (int) newY ;
}
}
}
touchEnded:
private void touchEnded(int lineID)
{
Path path = pathMap.get(lineID); // get the corresponding Path
bitmapCanvas.drawPath(path,paintLine);
path.reset();
}
撤消:
public void undo()
{
Toast.makeText(getContext(),"undo button pressed" + thelastLineId,Toast.LENGTH_SHORT).show();
Path path = pathMap.get(thelastLineId);
reservedpathMap.put(thelastLineId,path); // add the Path to reservedpathMap for later redo
pathMap.remove(thelastLineId);
invalidate();
}
题:
我正在尝试使用如上所示的代码实现UNDO方法:尝试从HashMap路径图中删除thelastLindId键(并将其放到HashMap后面的稍后重做的路径映射),这样当invalidate()时它将调用OnDraw()并且重绘
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key),paintLine);
但是,按下撤消按钮可以启动“撤消被点击”的祝酒,但最后绘制的行无法消失.
有人可以给我一个Undo()和Redo()的线索吗?提前谢谢了!!