在android中处理onScroll滑动手势

问题描述

我想通过在屏幕上滑动来控制音量,但问题是我无法找到合适的滑动速度。有时只需轻轻滑动即可波动太大,反之亦然。我尝试了许多不同的滑动速度,但仍然无法顺利工作。如何将其更改为按预期工作?这是实现

public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) {
  int halfWidth = getwindow().getDecorView().getWidth() / 2;
  boolean result = false;
  AudioManager mAudioManager;
  int SWIPE_THRESHOLD = 100;
  int SWIPE_VELociTY_THRESHOLD = 100;
  if (e1.getX() > halfWidth) {
     int swipeVolume = (int) ((Math.abs(diffY) - SWIPE_THRESHOLD) / SWIPE_VELociTY_THRESHOLD);
     if (diffY > 0) {
        onSwipeBottom(swipeVolume);
     } else {
        onSwipetop(swipeVolume);
     }
     result = true;
}

  private void onSwipetop(int increaseVolume) {
    
                int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                int nextVolume = currentVolume + increaseVolume;
                
                if (nextVolume > maxVolume) {
                    nextVolume = maxVolume;
                }
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,nextVolume,AudioManager.ADJUST_RAISE);
}

  private void onSwipeBottom(int decreaseVolume) {
            int minVolume = mAudioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
            int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int nextVolume = currentVolume - decreaseVolume;
           
            if (nextVolume < minVolume) {
                nextVolume = 0;
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0);
            } else {
                nextVolume = nextVolume;
            }
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0);
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...