Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 776
Andrew delgadillo Avatar asked Feb 24 '11 02:02

Andrew delgadillo


People also ask

What is the callback method for a view for drag and drop operation?

Drag event listeners and callback methods A View receives drag events with either a drag event listener that implements View. OnDragListener or with the view's onDragEvent() callback method.

What is the use of Action_drag_entered?

ACTION_DRAG_ENTERED. Action constant returned by getAction() : Signals to a View that the drag point has entered the bounding box of the View. If the View can accept a drop, it can react to ACTION_DRAG_ENTERED by changing its appearance in a way that tells the user that the View is the current drop target.

Which method is used to redraw on screen and results to a call of The View's onDraw () method?

The draw() method is called by the framework when the view need to be re-drawn and the draw() method then calls the onDraw() to draw the view's content.


1 Answers

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.

like image 156
dbyrne Avatar answered Oct 25 '22 08:10

dbyrne