PlayerTouchView.java
9.19 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.cnlive.gundam.video.view;
/**
* Created by gujun on 2017/4/6.
*/
import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableInt;
import android.media.AudioManager;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import com.cnlive.gundam.R;
import com.cnlive.gundam.databinding.LayoutPlayerTouchBinding;
import com.cnlive.gundam.video.utils.DensityUtils;
import com.cnlive.gundam.video.utils.PlayerUtils;
/**
* Created by gujun on 2016/9/7.
*/
public class PlayerTouchView extends RelativeLayout {
private VideoTouchListener l;
//点下时的x坐标
private int downX;
//点下时的y坐标
private int downY;
//左右控制区域的宽度
private int controlAreaWidth;
//是在左边区域滑动
private ObservableBoolean leftArea = new ObservableBoolean();
//是在右边区域滑动
private ObservableBoolean rightArea = new ObservableBoolean();
//声音或亮度进度
private ObservableInt brightnessAndVolumeProgress = new ObservableInt();
//记录当前手势有没有滑动
private boolean isMove = false;
//判定是点击还是滑动的范围
private int boundary = 30;
//上下滑动
private boolean udMove = false;
//左右滑动
private boolean lrMove = false;
//声音manager
private AudioManager audioManager;
//开始改变前的音量
private int preVolume = -1;
//时时当前声音
private int curVolumeProgress = -1;
//开始改变前的亮度
private int preBrightness = -1;
//时时当前亮度
private int curBrightnessProgress = -1;
//时时当前播放进度百分比
private int curPlayProgressPercent = -1;
private LayoutPlayerTouchBinding binding;
public PlayerTouchView(Context context) {
this(context, null);
}
public PlayerTouchView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PlayerTouchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_player_touch, this, true);
binding.setLeftAreaMove(leftArea);
binding.setRightAreaMove(rightArea);
binding.setBrightnessAndVolumeProgress(brightnessAndVolumeProgress);
}
public void setTouchListener(VideoTouchListener listener) {
this.l = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (l != null && !l.canTouch()) return false;
int curX = (int) event.getX();
int curY = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (l != null) l.down();
controlAreaWidth = getWidth() / 4;
downX = (int) event.getX();
downY = (int) event.getY();
if (downX < controlAreaWidth) {
if (preVolume == -1)
preVolume = getSystemVolume();
brightnessAndVolumeProgress.set(preVolume * DensityUtils.dp2px(getContext(), 93) / getSystemMaxVolume());
} else if (downX > getWidth() - controlAreaWidth) {
if (preBrightness == -1)
preBrightness = getSystemBrightness();
brightnessAndVolumeProgress.set(preBrightness * DensityUtils.dp2px(getContext(), 93) / 255);
}
curBrightnessProgress = -1;
curVolumeProgress = -1;
curPlayProgressPercent = -1;
break;
case MotionEvent.ACTION_MOVE:
int moveX = downX - curX;
int moveY = downY - curY;
if (Math.abs(moveX) > boundary || Math.abs(moveY) > boundary) {
isMove = true;
}
if (!udMove && !lrMove) {
if (Math.abs(moveX) > boundary) {
lrMove = true;
} else if (Math.abs(moveY) > boundary) {
udMove = true;
}
}
if (binding.operationVolumeBrightness.getVisibility() != VISIBLE) {
leftArea.set(downX < controlAreaWidth && udMove);
rightArea.set(downX > getWidth() - controlAreaWidth && udMove);
}
if (lrMove) {
changeProgress(moveX);
return true;
}
if (udMove) {
if (rightArea.get()) {
changeBrightness(moveY);
} else if (leftArea.get()) {
changeVolume(moveY);
}
}
break;
case MotionEvent.ACTION_UP:
if (!isMove) {
if (l != null) l.click();
} else if (lrMove) {
if (l != null) l.change_value_progress(0, true);
}
lrMove = false;
udMove = false;
isMove = false;
leftArea.set(false);
rightArea.set(false);
preBrightness = curBrightnessProgress;
preVolume = curVolumeProgress;
break;
}
return true;
}
/**
* 改变播放进度
*
* @param moveX 移动距离
*/
private void changeProgress(int moveX) {
int curProgressPercent = -moveX * 100 / getWidth();
if (curPlayProgressPercent != curProgressPercent) {
if (l != null) l.change_value_progress(curProgressPercent, false);
curPlayProgressPercent = curProgressPercent;
}
}
/**
* 改变亮度
*
* @param moveY 移动距离
*/
private void changeBrightness(int moveY) {
int curBrightness = PlayerUtils.checkSize(preBrightness + moveY * 255 / getHeight(), 1, 255);
if (curBrightnessProgress == curBrightness) return;
int progress = curBrightness * DensityUtils.dp2px(getContext(), 93) / 255;
brightnessAndVolumeProgress.set(progress);
curBrightnessProgress = curBrightness;
changeAppBrightness(curBrightness);
}
/**
* 获取系统屏幕亮度
*
* @return 0-255之间的范围
*/
private int getSystemBrightness() {
int systemBrightness = 0;
try {
systemBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return systemBrightness;
}
/**
* 改变App当前Window亮度
*
* @param brightness 0-255之间的范围
*/
public void changeAppBrightness(int brightness) {
Window window = ((Activity) getContext()).getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
if (brightness == -1) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
}
window.setAttributes(lp);
}
/**
* 改变声音
*
* @param moveY 移动距离
*/
private void changeVolume(int moveY) {
int curVolume = PlayerUtils.checkSize(preVolume + (moveY * getSystemMaxVolume() / getHeight()), 0, getSystemMaxVolume());
if (curVolumeProgress == curVolume) return;
int progress = curVolume * DensityUtils.dp2px(getContext(), 93) / getSystemMaxVolume();
brightnessAndVolumeProgress.set(progress);
curVolumeProgress = curVolume;
setSystemVolume(curVolume);
}
/**
* 获取系统的媒体音量
*
* @return
*/
private int getSystemVolume() {
return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}
/**
* 获取系统的媒体音量
*
* @return
*/
private int getSystemMaxVolume() {
return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
/**
* 设置系统的媒体音量
*/
private void setSystemVolume(int curVolume) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, curVolume, 0);
}
public interface VideoTouchListener {
/**
* 改变播放进度的回调
*
* @param percent 当前手势改变了进度的百分比
* @param update true 停止滑动,false正在滑动
* @return 要显示的时间字符串
*/
String change_value_progress(int percent, boolean update);
void click();
void down();
/**
* 是否需要拦截touch事件
*
* @return true拦截,false不拦截,其他的回调将不会执行
*/
boolean canTouch();
}
}