Reputation: 1097
I am trying to create a simple drawing app I which the user can press a point of my custom view and drag it to create a rectangle or a circle, I managed to do it quite easily.
The problem arose when I try showing the user the progress of move their shape as it grow bigger or smaller.
To explain it simply I want the same effect as on windows if you click and drag your mouse on the desktop.
This is my onTouchEvent
method, how do I do it?
if (chosenShape.equals("rectangle")) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // user start pressing
//starting point - one side of the rect
starting_X = touchX;
starting_Y = touchY;
break;
case MotionEvent.ACTION_MOVE: // user move while pressing
break;
case MotionEvent.ACTION_UP: // user release
//ending point - thie side of the circle
ending_X = touchX;
ending_Y = touchY;
// draw the rectangle - opposite side of the rect
drawCanvas.drawRect(starting_X, starting_Y, ending_X, ending_Y, drawPaint);
chosenShape = ""; // reseting the shape to keep drawing
break;
default:
return false;
}
} else {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
}
Upvotes: 0
Views: 95
Reputation: 4397
In your case MotionEvent.ACTION_MOVE:
you need to set the ending_X
and ending_Y
values.
Better yet would be to track intermediate_x
and intermediate_y
so you can draw a different effect, for example a dashed line instead of solid.
You're probably best storing all the shapes a user has drawn and refreshing the canvas each time a touch event comes in, otherwise you'll get the shapes drawn over each other as you drag ad they won't be cleared.
Upvotes: 1