javaandroidswipegesturebrightness

Half screen swipe up and down for volume and half for brightness


I want to read if the user is swiping on the right side of the screen or left side. I want to raise volume is user swipes up on right side of the screen and increase brightness if user swipes on the left side of the screen. I was able to read up,down and right,left swipe but i want to read swipeUpRight, swipeDownRight and 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;
        }
    }

Solution

  • have you tried something along these lines:

    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
        }
    }