视频屏幕左半部分上下滑动改变亮度,右半部分上下滑动改变声音

Posted libuyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视频屏幕左半部分上下滑动改变亮度,右半部分上下滑动改变声音相关的知识,希望对你有一定的参考价值。

说明:
实现功能:
(1)屏幕右半部分上滑,声音变大,下滑,声音变小 屏幕左半部分上滑,亮度变大,下滑,亮度变小
(2)如果亮度>1或者小于0.2时,手机震动


private float startY;//记录手指按下时的Y坐标
private float startX;//记录手指按下时的Y坐标
private int downVol;//记录手指按下时的音量
private Vibrator vibrator;//手机震动器
//不要忘记震动权限<uses-permission android:name="android.permission.VIBRATE" />
/*
* 设置屏幕亮度 lp = 0 全暗 ,lp= -1,根据系统设置, lp = 1; 最亮
*/
public void setBrightness(float brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
// if (lp.screenBrightness <= 0.1) {
// return;
// }
lp.screenBrightness = lp.screenBrightness + brightness / 255.0f;
if (lp.screenBrightness > 1) {
lp.screenBrightness = 1;
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = { 10, 200 }; // OFF/ON/OFF/ON... 关闭10秒震动200毫秒,不停切换
vibrator.vibrate(pattern, -1);
} else if (lp.screenBrightness < 0.2) {
lp.screenBrightness = (float) 0.2;
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] pattern = { 10, 200 }; // OFF/ON/OFF/ON...
vibrator.vibrate(pattern, -1);
}
getWindow().setAttributes(lp);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//把事件传递给手势识别器(注:对事件只进行了解析处理,没有拦截,解析成手势识别的单击、双击、长按)
detector.onTouchEvent(event);

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
startY=event.getY();
startX=event.getX();
downVol=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
handler.removeMessages(HIDE_CONTROL);
break;
case MotionEvent.ACTION_MOVE :
float endY=event.getY();
float distanceY=startY-endY;
if(startX>screenWidth/2){
//屏幕右半部分上滑,声音变大,下滑,声音变小
int touchRang=Math.min(screenWidth,screenHeight);
//int curvol= (int) (downVol+(distance/screenHeight)*maxVolume);
int curvol= (int) (downVol+(distanceY/touchRang)*maxVolume);//考虑到横竖屏切换的问题
int volume=Math.min(Math.max(0,curvol),maxVolume);
updateVolume(volume);
}else{
//屏幕左半部分上滑,亮度变大,下滑,亮度变小
final double FLING_MIN_DISTANCE = 0.5;
final double FLING_MIN_VELOCITY = 0.5;
if (distanceY > FLING_MIN_DISTANCE && Math.abs(distanceY) > FLING_MIN_VELOCITY) {
setBrightness(20);
}
if (distanceY < FLING_MIN_DISTANCE
&& Math.abs(distanceY) > FLING_MIN_VELOCITY) {
setBrightness(-20);
}
}

break;
case MotionEvent.ACTION_UP :
handler.sendEmptyMessageDelayed(HIDE_CONTROL,5000);
break;
}
return super.onTouchEvent(event);
}

以上是关于视频屏幕左半部分上下滑动改变亮度,右半部分上下滑动改变声音的主要内容,如果未能解决你的问题,请参考以下文章

Flutter videoplayer

Flutter videoplayer

Flutter videoplayer

Flutter videoplayer

前端小demo——鼠标控制图片上下滑动

在百度看小说怎么取消左、右滑动,在怎么打开上、下滑动模式