Andrew delgadillo
Andrew delgadillo

Reputation: 714

Detecting start and end position of a 'drag' in Android, and drawing a line between them

I want 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?

Upvotes: 6

Views: 5545

Answers (1)

dbyrne
dbyrne

Reputation: 61011

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.

Upvotes: 11

Related Questions