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
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With