在Android中向左滑动手势不正确

时间:2019-09-16 13:52:32

标签: java android android-studio

嘿,我即时通讯使用android的滑动手势进行滑动查看,但这并不准确,就像我想要这样的触摸手势

    //         \ A  /
    //          \  /
    //       D   e1   B
    //          /  \
    //         / C  \

但是这只能像直线一样难以触及  加上帮助我如何处理

  public class OnSwipeTouchListener implements View.OnTouchListener {

        private GestureDetector gestureDetector;

        public OnSwipeTouchListener(Context c) {
            gestureDetector = new GestureDetector(c, new GestureListener());
        }


        public boolean onTouch(final View view, final MotionEvent motionEvent) {
            return gestureDetector.onTouchEvent(motionEvent);
        }

        public void onSwipeRight() {
        }

        public void onSwipeLeft() {
        }

        public void onSwipeUp() {
        }

        public void onSwipeDown() {
        }

        public void onClick() {
        }

        public void onDoubleClick() {
        }

        public void onLongClick() {
        }


        private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

            private static final int SWIPE_THRESHOLD = 50;
            private static final int SWIPE_VELOCITY_THRESHOLD = -50;

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                onClick();
                return super.onSingleTapUp(e);
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                onDoubleClick();
                return super.onDoubleTap(e);
            }

            @Override
            public void onLongPress(MotionEvent e) {
                onLongClick();
                super.onLongPress(e);
            }

            // Determines the fling velocity and then fires the appropriate swipe event accordingly
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                try {
                    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(); // Right swipe
                            } else {
                                onSwipeLeft();  // Left swipe
                            }
                        }
                    } else {
                        if (Math.abs(diffY) >= SWIPE_THRESHOLD && Math.abs(velocityY) >= SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 1) {
                                onSwipeDown(); // Down swipe
                            } else {
                                onSwipeUp(); // Up swipe
                            }
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }
    }

触摸很难工作

我要实现的目标是将Gesture侦听器设置为int。

0 个答案:

没有答案