Android Question

onDrag not called when not moving finger

I'm creating an Android app with drag and drop support inside a zoomed image. I need to pan the image when the user moves the finger close to the edge of the screen. I check this if the event's action is ACTION_DRAG_LOCATION.

The problem I'm facing is that the onDrag() method in my onDragListener does not get called when the user's finger reaches the edge of the screen and stops moving.

I tried overriding the onTouch() method of my Activity but the events are not getting trigger when dragging.

Does someone know a way to achieve this?
Thanks in advance.

Answer

I ended up creating a Runnable that I start in the onDrag Method:

Runnable

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        long millis = SystemClock.uptimeMillis();
        if (insideOfMe) {
            int[] locations = new int[2];
            mapAreaLayout.getLocationOnScreen(locations);
            int x = locations[0];
            int y = locations[1];
            float speed = 10;
            if (dragEvent.getX() - speed < x) {
                pinView.panTheImage(speed, 0);
            }
            if (dragEvent.getX() + speed > x + mapAreaLayout.getWidth()) {
                pinView.panTheImage(-speed, 0);
            }
            if (dragEvent.getY() - speed < y) {
                pinView.panTheImage(0, speed);
            }
            if (dragEvent.getY() + speed > y + mapAreaLayout.getHeight()) {
                pinView.panTheImage(0, -speed);
            }

        }


        handler.postAtTime(this, millis + 16);
    }
};

onDrag

if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
                    handler.removeCallbacks(runnable);
                    handler.post(runnable);
}

And then I remove the callback to the Runnable:

if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
                handler.removeCallbacks(runnable);
            }
Comments