GestureDetector调节音量和亮度

0918更新

效果

手势调节音量和屏幕亮度
横屏屏幕左侧调节音量,右侧调节屏幕亮度

方法

继承GestureDetector.OnGestureListener,覆盖对应的手势方法

onFling|onScroll方法参数解释

e1:第1个ACTION_DOWN MotionEvent
e2:最后一个ACTION_MOVE MotionEvent
velocityX:X轴上的移动速度,像素/秒
velocityY:Y轴上的移动速度,像素/秒

onFling和onScroll的的区别

滑动距离上

二者没有(明显)区别,在检测效果的操作中,滑动距离无论长短都会触发。

滑动速率、触发顺序上

有区别:
onFling()为“滑动”的最后触发(即手指Up抬起时触发),需要较为快速的”滑动”操作(但在”滑动”过程中,也会不停的触发onScroll()),如果慢速滑动,通过日志可以看出,最后没有调用onFling()。

onScroll()为“拖动”或“滑动”的过程中不断触发,直到动作结束,无论快慢都会触发。

GestureDetector.OnGestureListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class GestureDetectorListener implements GestureDetector.OnGestureListener {
private float mDownY;
private Context mContext;
private static final float VELOCITY_SCROLL_MIN = 10;
public GestureDetectorListener(Context context) {
this.mContext = context;
}
@Override
public boolean onDown(MotionEvent e) {
mDownY = e.getY();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return true;
}
if (Math.abs(velocityX) > Math.abs(velocityY)) {
return false;
}
float moveY = e2.getY();
float distance = moveY - mDownY;
if (ScreenUtils.isInLeft(mContext, e1.getX())) {
if (distance < 0 && Math.abs(distance) > VELOCITY_SCROLL_MIN) {
increaseLight();
mDownY = moveY;
} else if (distance > 0 && distance > VELOCITY_SCROLL_MIN) {
decreaseLight();
mDownY = moveY;
}
} else {
if (distance < 0 && Math.abs(distance) > VELOCITY_SCROLL_MIN) {
increaseVoice(Math.abs(distance));
mDownY = moveY;
} else if (distance > 0 && distance > VELOCITY_SCROLL_MIN) {
decreaseVoice(distance);
mDownY = moveY;
}
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
private void increaseLight() {
applySystemPermission();
float mBrightness = ((Activity) mContext).getWindow().getAttributes().screenBrightness;
if (mBrightness <= 0.00f)
mBrightness = 0.50f;
WindowManager.LayoutParams lpa = ((Activity) mContext).getWindow().getAttributes();
lpa.screenBrightness = mBrightness + 0.01f;
if (lpa.screenBrightness > 1.0f) {
lpa.screenBrightness = 1.0f;
}
((Activity) mContext).getWindow().setAttributes(lpa);
}
private void decreaseLight() {
applySystemPermission();
float mBrightness = ((Activity) mContext).getWindow().getAttributes().screenBrightness;
if (mBrightness < 0.01f)
mBrightness = 0.01f;
WindowManager.LayoutParams lpa = ((Activity) mContext).getWindow().getAttributes();
lpa.screenBrightness = mBrightness - 0.01f;
if (lpa.screenBrightness < 0.01f)
lpa.screenBrightness = 0.01f;
((Activity) mContext).getWindow().setAttributes(lpa);
}
private void increaseVoice(float y) {
AudioManager audiomanager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
int maxVolume = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量
int currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
if (currentVolume < maxVolume) {
currentVolume += y / 10;
if (currentVolume >= maxVolume) {
currentVolume = maxVolume;
}
}
//第三个参数FLAG_SHOW_UI,显示系统的音量条
audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, AudioManager.FLAG_SHOW_UI);
}
private void decreaseVoice(float y) {
AudioManager audiomanager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
int currentVolume = audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
if (currentVolume > 0) {
currentVolume -= y / 10;
if (currentVolume < 0) {
currentVolume = 0;
}
}
audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, AudioManager.FLAG_SHOW_UI);
}
//增加Android6.0及以上需要进行权限判断
private void applySystemPermission() {
// Android 6.0以上机型必须先请求权限
if (Build.VERSION.SDK_INT >= 23 && !Settings.System.canWrite(mContext.getApplicationContext())) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
return;
}
}
}

判断屏幕方向的辅助类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ScreenUtils {
//获取屏幕的宽度(像素数)
public static int getWidth(Context mContext) {
DisplayMetrics dm = new DisplayMetrics();
((Activity) montext).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
return dm.widthPixels;
}
public static int getHeight(Context mContext) {
DisplayMetrics dm = new DisplayMetrics();
((Activity) mContext).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
int screenHeight = dm.heightPixels;
return screenHeight;
}
public static boolean isInRight(Context mContext, float xWeight) {
return (xWeight > getWidth(mContext) * 1 / 2);
}
//根据当前的位置来进行判断
public static boolean isInLeft(Context mContext, float xWeight) {
return (xWeight < getWidth(mContext) * 1 / 2);
}
}

Activity中引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class LiveActivity extends BaseActivity{
private static final String TAG = "LiveActivity";
private GestureDetector mGestureDetector;
@Override
protected void initView() {
mGestureDetector = new GestureDetector(this, new OnGestureListener(this));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
}