赞
踩
看公众号文章有一句话很有意思 .
人生 难以量化;
收获 静静等待。
直接上效果图
关于 Android 手势相关知识在此先不做太多的补充 ,等我忙完这一段时间之后来还这篇文章的技术债 。
Android 手势 当然是 GestureDetector 非他莫属 ,直接上代码
1.手势回调接口
- public interface GenseePlayerListener {
-
- /**
- * 亮度手势,在View左半部区域上下滑动时候调用
- * @param e1
- * @param e2
- * @param distanceX
- * @param distanceY
- */
- public void onBrightnessGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
-
-
- /**
- * 音量手势,在View右半部区域上下滑动时候调用
- * @param e1
- * @param e2
- * @param distanceX
- * @param distanceY
- */
- public void onVolumeGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
-
- /**
- * 快进快退手势,在View左右滑动的时候调用
- * @param e1
- * @param e2
- * @param distanceX
- * @param distanceY
- */
- public void onVideoSpeedGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
-
- /**
- * 打击手势
- * @param e
- */
- public void onSingleTapGesture(MotionEvent e);
-
-
- /**
- * 双击手势
- * @param e
- */
- public void onDoubleTapGesture(MotionEvent e);
-
-
- /**
- * 按下手势
- * @param e
- */
- public void onDown(MotionEvent e);
-
-
- /**
- * 快进快退执行后的松开时候调用
- * @param e
- */
- public void onVideoSpeedEnd(MotionEvent e);
- }
2.自定义View
- public class GenseeTopFragment extends FrameLayout {
-
- private static final int NONE = 0, VOLUME = 1, BRIGHTNESS = 2, FF_REW = 3;
-
- private int mScrollMode = NONE;
-
- private GenseePlayOnGestureListener genseePlayOnGestureListener;
- private GestureDetector gestureDetector;
- private GenseePlayerListener genseePlayerListener;
- //横向偏移检测,让快进快退不那么敏感
- private int offsetX = 1;
- private boolean videoSpeedEnd = false;
-
- public GenseeTopFragment(@NonNull Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
-
- public GenseeTopFragment(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context);
- }
-
- public GenseeTopFragment(@NonNull Context context) {
- super(context);
- init(context);
- }
-
-
- private void init(Context context) {
- genseePlayOnGestureListener = new GenseePlayOnGestureListener();
- gestureDetector = new GestureDetector(context, genseePlayOnGestureListener);
- //取消长按,不然会影响滑动
- gestureDetector.setIsLongpressEnabled(false);
-
- setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_UP) {
- if (videoSpeedEnd) {
- if (genseePlayerListener != null) {
- genseePlayerListener.onVideoSpeedEnd(event);
- }
- videoSpeedEnd = false;
- }
- }
- //监听触摸事件
- return gestureDetector.onTouchEvent(event);
- }
- });
-
- }
-
- public void setGenseePlayerListener(GenseePlayerListener genseePlayerListener) {
- this.genseePlayerListener = genseePlayerListener;
- }
-
- /**
- * 拦截事件,禁止子View消费事件
- */
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- return true;
- }
-
-
- /**
- * 手势监听
- */
- public class GenseePlayOnGestureListener extends GestureDetector.SimpleOnGestureListener {
- @Override
- public boolean onDown(MotionEvent e) {
- videoSpeedEnd = false;
- //每次按下都重置为NONE
- mScrollMode = NONE;
- if (genseePlayerListener != null) {
- genseePlayerListener.onDown(e);
- }
- return true;
- }
-
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- switch (mScrollMode) {
- case NONE:
- Log.i("AAAAAAA", "NONE: ");
- //offset是让快进快退不要那么敏感的值
- if (Math.abs(distanceX) - Math.abs(distanceY) > offsetX) {
- mScrollMode = FF_REW;
- } else {
- if (e1.getX() < getWidth() / 2) {
- mScrollMode = BRIGHTNESS;
- } else {
- mScrollMode = VOLUME;
- }
- }
- break;
- case VOLUME:
- if (genseePlayerListener != null) {
- genseePlayerListener.onVolumeGesture(e1, e2, distanceX, distanceY);
- }
- Log.d("AAAAAAA", "VOLUME: ");
- break;
- case BRIGHTNESS:
- if (genseePlayerListener != null) {
- genseePlayerListener.onBrightnessGesture(e1, e2, distanceX, distanceY);
- }
- Log.d("AAAAAAA", "BRIGHTNESS: ");
- break;
- case FF_REW:
- if (genseePlayerListener != null) {
- genseePlayerListener.onVideoSpeedGesture(e1, e2, distanceX, distanceY);
- }
- videoSpeedEnd = true;
- Log.d("AAAAAAA", "FF_REW: ");
- break;
- }
- return true;
- }
-
- @Override
- public boolean onContextClick(MotionEvent e) {
- return super.onContextClick(e);
- }
-
- @Override
- public boolean onDoubleTap(MotionEvent e) {
- return super.onDoubleTap(e);
- }
-
- @Override
- public boolean onDoubleTapEvent(MotionEvent e) {
- return super.onDoubleTapEvent(e);
- }
-
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- return super.onSingleTapUp(e);
- }
-
- @Override
- public void onLongPress(MotionEvent e) {
- super.onLongPress(e);
- }
-
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- return super.onFling(e1, e2, velocityX, velocityY);
- }
-
- @Override
- public void onShowPress(MotionEvent e) {
- super.onShowPress(e);
- }
-
-
- @Override
- public boolean onSingleTapConfirmed(MotionEvent e) {
- if (genseePlayerListener != null) {
- genseePlayerListener.onSingleTapGesture(e);
- }
- return super.onSingleTapConfirmed(e);
- }
- }
- }
PS:自定义VIew 有一个特殊功能禁止 子View 消费事件(跟手势滑动无关)
3.在Activity 使用
- /**************************手势监听*****************************/
- private int newProgress = 0, oldProgress = 0;
- private int oldVolume = 0;
- private int maxVolume = 0;
- private float brightness = 1;
- private BrightnessHelper mBrightnessHelper;
- private AudioManager mAudioManager;
- private Window mWindow;
- private WindowManager.LayoutParams mLayoutParams;
- private ChangeBarView changeBarView;// 音量/亮度
-
- @Override
- public void onDown(MotionEvent e) {
- //每次按下的时候更新当前亮度和音量,还有进度
- oldProgress = newProgress;
- oldVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
- brightness = mLayoutParams.screenBrightness;
- if (brightness == -1) {
- //一开始是默认亮度的时候,获取系统亮度,计算比例值
- brightness = mBrightnessHelper.getBrightness() / 255f;
- }
-
- }
-
- @Override
- public void onBrightnessGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- //下面这是设置当前APP亮度的方法
- Log.d(TAG, "onBrightnessGesture: old" + brightness);
- float newBrightness = (e1.getY() - e2.getY()) / fl_big_layout.getHeight();
- newBrightness += brightness;
-
- if (newBrightness < 0) {
- newBrightness = 0;
- } else if (newBrightness > 1) {
- newBrightness = 1;
- }
- mLayoutParams.screenBrightness = newBrightness;
- mWindow.setAttributes(mLayoutParams);
- changeBarView.showInfo((int) (newBrightness * 100), false);
- }
-
- @Override
- public void onVolumeGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- int value = fl_big_layout.getHeight() / maxVolume;
- int newVolume = (int) ((e1.getY() - e2.getY()) / value + oldVolume);
-
- mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, AudioManager.FLAG_PLAY_SOUND);
-
- if (newVolume < 0) {
- newVolume = 0;
- }
-
- //要强行转Float类型才能算出小数点,不然结果一直为0
- int volumeProgress = (int) (newVolume / Float.valueOf(maxVolume) * 100);
- if (volumeProgress > 100) {
- volumeProgress = 100;
- }
-
- changeBarView.showInfo(volumeProgress, true);
- }
-
- @Override
- public void onVideoSpeedGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
- }
-
- @Override
- public void onSingleTapGesture(MotionEvent e) {
- hideController(isGoneView);
- }
-
- @Override
- public void onDoubleTapGesture(MotionEvent e) {
-
- }
-
-
- @Override
- public void onVideoSpeedEnd(MotionEvent e) {
-
- }
PS:只是简单地进行复制代码 ,保证可以使用 。 稍等我在上传 demo 。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。