1

I am working on swipe gesture. Actually I want to restrict swipe if touch coordinates > Tap on left and right edges of screen (100 px) like

If I swipe from left to right and touch coordinate > from screen left Top 100px I want to restrict swipe

If swipe from right to left and touch coordinate

And There is no Restriction for Top and Bottom swipe.

Here is my code what I tried :

    float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD
                        && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {

                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD
                        && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
Android_dev
  • 320
  • 1
  • 7
  • 26
  • Try overriding and returning true from either [onInterceptTouchEvent()][1] and/or [onTouchEvent()][2], which will consume touch events on the pager. [1]: http://developer.android.com/reference/android/support/v4/view/ViewPager.html#onInterceptTouchEvent%28android.view.MotionEvent%29 [2]: http://developer.android.com/reference/android/support/v4/view/ViewPager.html#onTouchEvent%28android.view.MotionEvent%29 – Maveňツ Nov 18 '14 at 09:00

3 Answers3

3

Check this might help you

https://github.com/daimajia/AndroidSwipeLayout

Its library predefined swipe implementation

geniushkg
  • 706
  • 9
  • 21
  • Like that Example already I am doing. I want Swipe on edges of screen not on entire screen. Do you know how to do? – Android_dev Nov 17 '14 at 09:00
  • I need to do the same thing as well. Basically tug and see a new view on the sides or top, but the view never takes over the screen and has a max display size (probably 1/5th of the available space for me). Then I need to rubberband it back when you release. I'll reply here if I figure out something I can share :) – Lo-Tan Mar 09 '15 at 22:25
0

You can calculate this using the onFling method in SimpleOnGestureListener. Please check the following question:

Android: How to handle right to left swipe gestures

Community
  • 1
  • 1
SubinM
  • 394
  • 3
  • 7
0

there is a great post for getting the siplay metrics: Get screen dimensions in pixels. i'm not sure that i quite understance the meaning of swipe along the side s of the screen (1 finger at the right and the other at the left maybe?).. but i guuess that in your case it should be something like:

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;

        PointF p1 = new PointF(e1.getX(), e1.getY());
        PointF p2 = new PointF(e2.getX(), e2.getY());

        if ((p1.x < 100 || p1.x > width - 100) && (p2.x < 100 || p2.x > width - 100))
        {

        float diffY = p2.y - p1.y;
        float diffX = p2.x - p1.x;

        int dirY = (diffY < 0) ? -1 : 1;
        int dirX = (diffX < 0) ? -1 : 1;

        diffY = Math.abs(diffY); diffX = Math.abs(diffX); 
        if (diffX > diffY) {
            if (diffX > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {

                if (dirX > 0) {
                    onSwipeRight();
                } else {
                    onSwipeLeft();
                }
            }
        } else {
            if (diffY > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (dirY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
            }
        }
        }
Community
  • 1
  • 1
ymz
  • 6,602
  • 1
  • 20
  • 39