Detecting up-down, left-right swipe on Android.

This probably is the simplest way to detect the exact swipe direction on Android. We split the screen into four quadrants and then use the MotionEvents to calculate the angle of swipe. The code is pretty self explanatory.

class SwipeGestureDetector extends SimpleOnGestureListener {

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, 
        float velocityX, float velocityY) {

        switch (getSlope(e1.getX(), e1.getY(), e2.getX(), e2.getY())) {
        case 1:
            Log.d(LOGTAG, "top");
            return true;
        case 2:
            Log.d(LOGTAG, "left");
            return true;
        case 3:
            Log.d(LOGTAG, "down");
            return true;
        case 4:
            Log.d(LOGTAG, "right");
            return true;
        }
        return false;
    }

    private int getSlope(float x1, float y1, float x2, float y2) {
        Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1));
        if (angle > 45 && angle <= 135)
        // top
        return 1;
        if (angle >= 135 && angle < 180 || angle < -135 && angle > -180)
        // left
        return 2;
        if (angle < -45 && angle>= -135)
        // down
        return 3;
        if (angle > -45 && angle <= 45)
        // right
        return 4;
        return 0;
    }

To check out how to use this SwipeGestureDetector class with the relevant view, check out this post : https://vshivam.wordpress.com/2013/09/02/privly-reading-app-integration-and-detecting-swipe-gesture/

Advertisements

One thought on “Detecting up-down, left-right swipe on Android.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s