PlayerTouchView.java 9.19 KB
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();
    }
}