I am working on a graphical application, onTouchEvents are standard and would like to add a Undo() function to remove the last return path.
Ads:
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<Integer, Path> pathMap; // current Paths being drawn private HashMap<Integer, Path> reservedpathMap; // for saving the paths being undone private HashMap<Integer, Point> previousPointMap; // current Points
Constructor:
pathMap = new HashMap<Integer, Path>(); reservedpathMap = new HashMap <Integer,Path>(); // for storing path being undone previousPointMap = new HashMap<Integer, Point>();
Ondraw:
@Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bitmap, 0, 0, paintScreen);
onTouchEvent:
@Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); // event type int actionIndex = event.getActionIndex(); // pointer (ie, 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)
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);
UNDO:
public void undo() { Toast.makeText(getContext(), "undo button pressed" + thelastLineId, Toast.LENGTH_SHORT).show(); Path path = pathMap.get(thelastLineId); reservedpathMap.put(thelastLineId, path);
Question:
I am trying to implement the UNDO method using the code as shown above: trying to remove thelastLindId key from the HashMap pathmap (and put the HashMap reservedpathMap in the HashMap reservedpathMap for later retry) so that if invalidate() it will call OnDraw() and redraw to
for (Integer key : pathMap.keySet()) canvas.drawPath(pathMap.get(key), paintLine);
However, pressing the cancel button may initiate a “cancel click” toast, but the last line drawn does not disappear.
Can someone please give me the key to Undo () and Redo ()? Thank you very much in advance!