Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get touch position while dragging

I have some views which I like to drag around. The views are within a LinearLayout, which itself is within a scrollview.

I want to get the position of the current finger (touch), to make a smooth scroll on my scrollview, depending of the height of the current drag.

I start my draggin after a long click with the View build-in listener startDrag

view.startDrag(null, shadowBuilder, view, 0);

I am also able to get the relativ position of the drag on the view that is currently hovered by

view.setOnDragListener(new OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            //get event positions
        }
    });

But this only works for the view where the drag shadow is currently on and DragEvent only provides relative positions, not raw positions.

What I need is the position of the finger, while the drag happens. Unfortunately all onTouchEvents get consumed while dragging.

Does anybody know how I get this to work?

Ps: One way that works (kind of) which I currently use, is to calculate the position of the touch via the dragEvent.relativePosition in combination with the view position. But isn't there a better way to go?

like image 809
JacksOnF1re Avatar asked May 29 '15 14:05

JacksOnF1re


1 Answers

OK, since there seems not to be an easier answer, I will provide my own relativly easy solution for further readers, with no need of gesture detection.

At first you need the view that receives the drag event and at second the drag event ifself (or at least the x and y coordinates). With fetching of the view position and some simple addition you get the raw touch position.

This method is tested with the show pointer position developer options to provide the correct data.

The method for the calculation looks like this:

/**
 * @param item  the view that received the drag event
 * @param event the event from {@link android.view.View.OnDragListener#onDrag(View, DragEvent)}
 * @return the coordinates of the touch on x and y axis relative to the screen
 */
public static Point getTouchPositionFromDragEvent(View item, DragEvent event) {
    Rect rItem = new Rect();
    item.getGlobalVisibleRect(rItem);
    return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY()));
}

Call this method inside of your onDragListener implementation:

@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //etc etc. do some stuff with the drag event
            break;
        case DragEvent.ACTION_DRAG_LOCATION:
            Point touchPosition = getTouchPositionFromDragEvent(v, event);
            //do something with the position (a scroll i.e);
            break;
         default: 
   }
   return true;
}

Addtional: If you want to determinate if the touch is inside of a specific view, you can do something like this:

 public static boolean isTouchInsideOfView(View view, Point touchPosition) {
    Rect rScroll = new Rect();
    view.getGlobalVisibleRect(rScroll);
    return isTouchInsideOfRect(touchPosition, rScroll);
}

public static boolean isTouchInsideOfRect(Point touchPosition, Rect rScroll) {
    return touchPosition.x > rScroll.left && touchPosition.x < rScroll.right //within x axis / width
            && touchPosition.y > rScroll.top && touchPosition.y < rScroll.bottom; //withing y axis / height
}

It is also possible to implement a smooth scroll on a ListView based on this solution. So that the user can drag an item out of a list, and scroll the list via dragging the item either to the top or bottom of the list view.

Cheers.

like image 129
JacksOnF1re Avatar answered Sep 20 '22 08:09

JacksOnF1re