视频播放中GestureDetector的应用

gesture:手势;detector:检测器

  • GestureDetector类提供了两个接口:OnGestureListener,OnDoubleTapListener,一个被static修饰的内部类:
    SimpleOnGestureListener;

  • 实现OnGestureListener接口时需要重写全部的手势处理方法:

onDown():手指触摸屏幕的瞬间
onFling():手指在屏幕上迅速移动并松开
onLongPress():手指长按屏幕
onScroll():手指在屏幕上滑动
onShowPress():手指按在屏幕上时,在长按之前
onSlingleTapUp():手指离开屏幕时
onDoubleTap():双击屏幕时

  • 而继承SimpleOnGestureListener类,只需要重写需要的方法即可:
 class MyGestureListener extends GestureDetector.SimpleOnGestureListener {  
 
        /**
        * MotionEvent e1 手势起点事件
        * MotionEvent e2 手势终点事件
        * distanceX x轴方向上移动的速度/s
        * distanceY Y轴方向上移动的速度/s
        */
        @Override  
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
            float distanceY) {  
            Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());  
            return true;  
        }
    }  
  • activity,view 中使用手势需要重写onTouchEvent()
//activity
 @Override   
    public boolean onTouchEvent(MotionEvent event){  
        if(this.mMyGestureListener.onTouchEvent(event))
            return true; 
        return super.onTouchEvent(event);  
    }
//view
 mView.setOnTouchListener(new OnTouchListener() {  
        public boolean onTouch(View v, MotionEvent event) {  
            if(this.mMyGestureListener.onTouchEvent(event))
                return true;  
            return super.onTouchEvent(event);  
        }  
    });  

  • 场景:视屏播放时,在屏幕左侧上下滑动,调节亮度;屏幕右侧上下滑动,调节音量
     @Override
        public boolean onDown(MotionEvent e) {
            firstTouch = true;
            return super.onDown(e);
        }

    /**
         * 滑动
         */
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (e1 != null && e2 != null) {
                float mOldX = e1.getX(), mOldY = e1.getY();
                float deltaY = mOldY - e2.getY();
                float deltaX = mOldX - e2.getX();
                if (firstTouch) {
                    //x偏移量的绝对值 大于 y偏移量的绝对值时 为水平滑动,否则为竖直滑动
                    toSeek = Math.abs(distanceX) >= Math.abs(distanceY);
                    //将屏幕分为左右两个,一个控制亮度,一个控制音量
                    volumeControl = mOldX > activity.getResources().getDisplayMetrics().widthPixels * 0.5f;
                    firstTouch = false;
                }

                if (!toSeek) {
                    float percent = deltaY / videoView.getHeight();
                    if (volumeControl) {
                        onVolumeSlide(percent);
                    } else {
                        onBrightnessSlide(percent);
                    }
                } else {
                    //水平滑动操作
                    ...
                }
            }
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
 /**
     * 滑动改变声音大小
     *
     * @param percent
     */
    private void onVolumeSlide(float percent) {
        if (volume == -1) {
            volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            if (volume < 0)
                volume = 0;
        }
        int index = (int) (percent / 2 * mMaxVolume) + volume;
        if (index > mMaxVolume) {
            index = mMaxVolume;
        } else if (index < 0) {
            index = 0;
        }
        volume = index;
        // 改变音量
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
    }
/**
     * 滑动改变亮度
     *
     * @param percent
     */
    private void onBrightnessSlide(float percent) {
        if (brightness < 0) {
            //获取屏幕亮度
            brightness = activity.getWindow().getAttributes().screenBrightness;
            if (brightness <= 0.00f) {
                brightness = 0.5f;
            } else if (brightness >= 0.01f) {
                brightness = 0.01f;
            }
        }
        //设置屏幕亮度
        WindowManager.LayoutParams lpa = activity.getWindow().getAttributes();
        lpa.screenBrightness = brightness + percent / 3;
        brightness = lpa.screenBrightness;
        if (lpa.screenBrightness > 1.0f) {
            lpa.screenBrightness = 1.0f;
        } else if (lpa.screenBrightness < 0.01f) {
            lpa.screenBrightness = 0.01f;
        }
        activity.getWindow().setAttributes(lpa);
    }
  • 场景:双击暂停播放
   /**
         * 双击:播放or暂停
         */
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (isPlaying()) {
                pause();
                mPlayPause.setSelected(true);
            } else {
                start();
                mPlayPause.setSelected(false);
            }
            return true;
        }

你可能感兴趣的:(视频播放中GestureDetector的应用)