Detecting start and end position of a 'drag' in Android, and drawing a line between them
I wan开发者_开发知识库t to make a simple doodle application, for android, but I don't know how to get some data from android! I need:
The position of the mouse before and after the drag or if it is a simple touch how to get the position of the touch.
How should I go about drawing the lines?
Can somebody please help me?
You can override the onTouchEvent
method in your View
:
@Override
public boolean onTouchEvent (MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
start_x = event.getX();
start_y = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//create the line from start_x and start_y to the current location
//don't forget to invalidate the View otherwise your line won't get redrawn
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//might not need anything here
}
return true;
}
I am assuming you want to draw a straight line from the start of the drag to the endpoint and you don't want to "doodle", but its pretty simple to modify this code to handle either.
精彩评论