【问题标题】:Half screen swipe up and down for volume and half for brightness半屏上下滑动调节音量,一半调节亮度
【发布时间】:2019-09-12 12:32:29
【问题描述】:

我想了解用户是在屏幕右侧还是左侧滑动。我想提高音量是用户在屏幕右侧向上滑动并在用户在屏幕左侧滑动时增加亮度。我能够阅读上、下、右、左滑动,但我想阅读 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;
        }
    }

【问题讨论】:

  • 你能不能有两个视图,一个在左边,一个在右边,并为每个视图使用一个单独的 GestureDetector?
  • @LarsH 不,因为它是直播电视频道流,所以我不能

标签: java android swipe gesture brightness


【解决方案1】:

您是否尝试过以下方法:

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

【讨论】:

  • 这行得通。我刚刚通过 Resources.getSystem().getDisplayMetrics().widthPixels 更改了 getWindow().getDecorView().getWidth(),我得到了我想要的!谢谢大佬!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 2013-07-20
  • 2012-07-24
  • 1970-01-01
相关资源
最近更新 更多