滑动屏幕改变音量及亮度

简述

将自己的视频播放器的屏幕界面平分为左右两部分,其中在屏幕左部分上下滑动改变亮度,在屏幕右部分上下滑动改变声音。

滑动屏幕左边改变亮度滑动右边改变声音

滑动屏幕改变音量及亮度_第1张图片

参考博客:http://blog.csdn.net/piaozhiye/article/details/6544450

www.2cto.com/kf/201609/551320.html

在SystemVideoPlayer以及VitamioVideoPlayer的onTouchEvent方法中,根据手指滑动的X轴的值endX进行判断手指滑动在左边还是右边,然后在Y轴上做具体的调节亮度或声音的操作即可,同时当的屏幕达到最亮或最暗(20%)的时候,设备会震动,因此需要添加设备振动的权限。


-------------------------------------------------------------
private float startY;//手指刚开始滑动时记录点 Y轴
private float startX;//手指刚开始滑动时记录点 X轴
private Vibrator vibrator;//振动
-----------------------------------------------
public boolean onTouchEvent(MotionEvent event) {
    detectlor.onTouchEvent(event);//把事件传递给手势识别器
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN://手指按下
            //1.按下时记录相关值
            startY = event.getY();
            startX = event.getX();
            mVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
            touchRang = Math.min(screenWidth, screenHeight);//固定为横屏 则屏幕高度小于屏幕宽度
            handler.removeMessages(HIDE_MEDIACONTROLLER);//移除隐藏控制面板的消息

            break;
        case MotionEvent.ACTION_MOVE://手指滑动
            //2.滑动时记录相关值
            float endY = event.getY();
            float endX = event.getX();
            float distanceY = startY - endY;//滑动距离

            if (endX < screenWidth/2){
                //左边屏幕 调节亮度
                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) {
                  //  Log.e(TAG, "up");
                    setBrightness(20);
                }
                if (distanceY < FLING_MIN_DISTANCE
                        && Math.abs(distanceY) > FLING_MIN_VELOCITY) {
                   // Log.e(TAG, "down");
                    setBrightness(-20);
                }
            }else {
                //右边屏幕  调节声音
                //改变的音量= (滑动屏幕的距离/屏幕宽度 )* 音量的最大值
                float changedVolume = (distanceY / touchRang) * maxVolume;
                //最终音量= 原来的音量 + 改变的音量
                int volume = (int) Math.min(Math.max(mVolume + changedVolume, 0), maxVolume);
                if (changedVolume != 0) {
                    isMute = false;
                    updateVolume(volume, isMute);
                }
            }
            break;
        case MotionEvent.ACTION_UP://手指离开
            handler.sendEmptyMessageDelayed(HIDE_MEDIACONTROLLER, 3000);//延迟3秒发送隐藏控制面板的消息

            break;
    }
    return super.onTouchEvent(event);
}
---------------------------------------------------------------------
/*
 *
 * 设置屏幕亮度 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...
        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);
    }
   // Log.e(TAG, "lp.screenBrightness= " + lp.screenBrightness);
    getWindow().setAttributes(lp);
}

你可能感兴趣的:(滑动屏幕改变音量及亮度)