上下滑动一半屏幕以获得音量,一半滑动以获得亮度

时间:2019-09-12 12:32:29

标签: java android swipe gesture brightness

我想阅读用户是否在屏幕右侧或左侧滑动。我要提高音量是用户在屏幕右侧向上滑动,而在用户在屏幕左侧滑动时增加亮度。我能够读取上下左右滑动,但是我想读取swipeUpRight,swipeDownRight和swipeUpLeft,swipeDownLeft

private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

        @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();
                /*Log.e("TAG", "Y: " + e1.getY() + " , " + e2.getY());
                Log.e("TAG", "diffY: " + diffY);
                Log.e("TAG", "X: " + e1.getX() + " , " + e2.getX());*/
                Log.e("TAG", "X: " + Math.abs(diffX));
                Log.e("TAG", "Y: " + Math.abs(diffY));
                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();
                        }
                        result = true;
                    }
                } else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottomRight();
                    } else {
                        onSwipeTopRight();
                    }
                    result = true;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

1 个答案:

答案 0 :(得分:2)

您是否尝试过以下方法:

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
   int halfWidth = getWindow().getDecorView().getWidth() / 2;
    if (e1.getX() < halfWidth) {
      //leftSide code
    else{
      //rightSide code
    }
}