Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the correct X,Y coordinates of a DragEvent.ACTION_DROP

I'm having trouble getting the X,Y coordinates of a DragEvent ACTION_DROP.

I am attempting to animate a gridview cell drag and drop, animating the cell from the X,Y of the release point to the new X,Y.

Below is the code I am using to try and get the X,Y coordinates. However it does not return the values that I would expect.

 @Override
 public boolean onDrag(View v, DragEvent event) {
 
   switch (action) {
     case DragEvent.ACTION_DROP:
       float x = event.getX();
       float y = event.getY();
     break;
   }
}

Dragging the first gridview cell to the adjacent cell to the right gives me these values.

Drop X,Y: 176.0,127.0

SourceView X,Y: 0.0,0.0

TargetView X,Y 360.0,0.0

  • The Drop values are the values returned from the DragEvent. The
  • SourceView values are the from the cell where the drag was started.
  • The TargetView values are from the cell where the drag event ended.

Can anyone point me in the right direction of how to get the X,Y coordinates of the release point of the DragEvent? Or explain what the getX() and getY() of a DragEvent actually returns?

Thanks

like image 657
Jonathon Fry Avatar asked Mar 28 '14 13:03

Jonathon Fry


1 Answers

As documented in docs, the

event.getX(); event.get(y);

return the position of the drag action inside the view.

The getX() and getY() methods supply the X and Y position of of the drag point within the View object's bounding box.

So in order to get the absolute coordinates of the drop position you can override GridView onTouchEvent and get the values of event.getRawX(), event.getRawY() .

If every cell in GridView accept the dragEvent you can take the View that get the onDrop event, and do something like that to retrieve coordinates:

@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DROP:
        int[] oldFolderCellPosition = new int[2];
        v.getLocationOnScreen(oldFolderCellPosition);
        return true;
}
like image 109
yshahak Avatar answered Oct 19 '22 12:10

yshahak