当前位置:   article > 正文

Android 视频添加手势滑动调节亮度和音量以及更新进度_手势控制手机音量

手势控制手机音量

看公众号文章有一句话很有意思 .


人生  难以量化;

收获  静静等待。

 

 

直接上效果图

 

关于 Android 手势相关知识在此先不做太多的补充 ,等我忙完这一段时间之后来还这篇文章的技术债

Android 手势 当然是 GestureDetector 非他莫属 ,直接上代码 

1.手势回调接口

  1. public interface GenseePlayerListener {
  2. /**
  3. * 亮度手势,在View左半部区域上下滑动时候调用
  4. * @param e1
  5. * @param e2
  6. * @param distanceX
  7. * @param distanceY
  8. */
  9. public void onBrightnessGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
  10. /**
  11. * 音量手势,在View右半部区域上下滑动时候调用
  12. * @param e1
  13. * @param e2
  14. * @param distanceX
  15. * @param distanceY
  16. */
  17. public void onVolumeGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
  18. /**
  19. * 快进快退手势,在View左右滑动的时候调用
  20. * @param e1
  21. * @param e2
  22. * @param distanceX
  23. * @param distanceY
  24. */
  25. public void onVideoSpeedGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
  26. /**
  27. * 打击手势
  28. * @param e
  29. */
  30. public void onSingleTapGesture(MotionEvent e);
  31. /**
  32. * 双击手势
  33. * @param e
  34. */
  35. public void onDoubleTapGesture(MotionEvent e);
  36. /**
  37. * 按下手势
  38. * @param e
  39. */
  40. public void onDown(MotionEvent e);
  41. /**
  42. * 快进快退执行后的松开时候调用
  43. * @param e
  44. */
  45. public void onVideoSpeedEnd(MotionEvent e);
  46. }

2.自定义View 

  1. public class GenseeTopFragment extends FrameLayout {
  2. private static final int NONE = 0, VOLUME = 1, BRIGHTNESS = 2, FF_REW = 3;
  3. private int mScrollMode = NONE;
  4. private GenseePlayOnGestureListener genseePlayOnGestureListener;
  5. private GestureDetector gestureDetector;
  6. private GenseePlayerListener genseePlayerListener;
  7. //横向偏移检测,让快进快退不那么敏感
  8. private int offsetX = 1;
  9. private boolean videoSpeedEnd = false;
  10. public GenseeTopFragment(@NonNull Context context, @Nullable AttributeSet attrs) {
  11. super(context, attrs);
  12. init(context);
  13. }
  14. public GenseeTopFragment(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  15. super(context, attrs, defStyleAttr);
  16. init(context);
  17. }
  18. public GenseeTopFragment(@NonNull Context context) {
  19. super(context);
  20. init(context);
  21. }
  22. private void init(Context context) {
  23. genseePlayOnGestureListener = new GenseePlayOnGestureListener();
  24. gestureDetector = new GestureDetector(context, genseePlayOnGestureListener);
  25. //取消长按,不然会影响滑动
  26. gestureDetector.setIsLongpressEnabled(false);
  27. setOnTouchListener(new OnTouchListener() {
  28. @Override
  29. public boolean onTouch(View v, MotionEvent event) {
  30. if (event.getAction() == MotionEvent.ACTION_UP) {
  31. if (videoSpeedEnd) {
  32. if (genseePlayerListener != null) {
  33. genseePlayerListener.onVideoSpeedEnd(event);
  34. }
  35. videoSpeedEnd = false;
  36. }
  37. }
  38. //监听触摸事件
  39. return gestureDetector.onTouchEvent(event);
  40. }
  41. });
  42. }
  43. public void setGenseePlayerListener(GenseePlayerListener genseePlayerListener) {
  44. this.genseePlayerListener = genseePlayerListener;
  45. }
  46. /**
  47. * 拦截事件,禁止子View消费事件
  48. */
  49. @Override
  50. public boolean onInterceptTouchEvent(MotionEvent ev) {
  51. return true;
  52. }
  53. /**
  54. * 手势监听
  55. */
  56. public class GenseePlayOnGestureListener extends GestureDetector.SimpleOnGestureListener {
  57. @Override
  58. public boolean onDown(MotionEvent e) {
  59. videoSpeedEnd = false;
  60. //每次按下都重置为NONE
  61. mScrollMode = NONE;
  62. if (genseePlayerListener != null) {
  63. genseePlayerListener.onDown(e);
  64. }
  65. return true;
  66. }
  67. @Override
  68. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  69. switch (mScrollMode) {
  70. case NONE:
  71. Log.i("AAAAAAA", "NONE: ");
  72. //offset是让快进快退不要那么敏感的值
  73. if (Math.abs(distanceX) - Math.abs(distanceY) > offsetX) {
  74. mScrollMode = FF_REW;
  75. } else {
  76. if (e1.getX() < getWidth() / 2) {
  77. mScrollMode = BRIGHTNESS;
  78. } else {
  79. mScrollMode = VOLUME;
  80. }
  81. }
  82. break;
  83. case VOLUME:
  84. if (genseePlayerListener != null) {
  85. genseePlayerListener.onVolumeGesture(e1, e2, distanceX, distanceY);
  86. }
  87. Log.d("AAAAAAA", "VOLUME: ");
  88. break;
  89. case BRIGHTNESS:
  90. if (genseePlayerListener != null) {
  91. genseePlayerListener.onBrightnessGesture(e1, e2, distanceX, distanceY);
  92. }
  93. Log.d("AAAAAAA", "BRIGHTNESS: ");
  94. break;
  95. case FF_REW:
  96. if (genseePlayerListener != null) {
  97. genseePlayerListener.onVideoSpeedGesture(e1, e2, distanceX, distanceY);
  98. }
  99. videoSpeedEnd = true;
  100. Log.d("AAAAAAA", "FF_REW: ");
  101. break;
  102. }
  103. return true;
  104. }
  105. @Override
  106. public boolean onContextClick(MotionEvent e) {
  107. return super.onContextClick(e);
  108. }
  109. @Override
  110. public boolean onDoubleTap(MotionEvent e) {
  111. return super.onDoubleTap(e);
  112. }
  113. @Override
  114. public boolean onDoubleTapEvent(MotionEvent e) {
  115. return super.onDoubleTapEvent(e);
  116. }
  117. @Override
  118. public boolean onSingleTapUp(MotionEvent e) {
  119. return super.onSingleTapUp(e);
  120. }
  121. @Override
  122. public void onLongPress(MotionEvent e) {
  123. super.onLongPress(e);
  124. }
  125. @Override
  126. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  127. return super.onFling(e1, e2, velocityX, velocityY);
  128. }
  129. @Override
  130. public void onShowPress(MotionEvent e) {
  131. super.onShowPress(e);
  132. }
  133. @Override
  134. public boolean onSingleTapConfirmed(MotionEvent e) {
  135. if (genseePlayerListener != null) {
  136. genseePlayerListener.onSingleTapGesture(e);
  137. }
  138. return super.onSingleTapConfirmed(e);
  139. }
  140. }
  141. }

 

PS:自定义VIew 有一个特殊功能禁止 子View 消费事件(跟手势滑动无关)

3.在Activity 使用 

  1. /**************************手势监听*****************************/
  2. private int newProgress = 0, oldProgress = 0;
  3. private int oldVolume = 0;
  4. private int maxVolume = 0;
  5. private float brightness = 1;
  6. private BrightnessHelper mBrightnessHelper;
  7. private AudioManager mAudioManager;
  8. private Window mWindow;
  9. private WindowManager.LayoutParams mLayoutParams;
  10. private ChangeBarView changeBarView;// 音量/亮度
  11. @Override
  12. public void onDown(MotionEvent e) {
  13. //每次按下的时候更新当前亮度和音量,还有进度
  14. oldProgress = newProgress;
  15. oldVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
  16. brightness = mLayoutParams.screenBrightness;
  17. if (brightness == -1) {
  18. //一开始是默认亮度的时候,获取系统亮度,计算比例值
  19. brightness = mBrightnessHelper.getBrightness() / 255f;
  20. }
  21. }
  22. @Override
  23. public void onBrightnessGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  24. //下面这是设置当前APP亮度的方法
  25. Log.d(TAG, "onBrightnessGesture: old" + brightness);
  26. float newBrightness = (e1.getY() - e2.getY()) / fl_big_layout.getHeight();
  27. newBrightness += brightness;
  28. if (newBrightness < 0) {
  29. newBrightness = 0;
  30. } else if (newBrightness > 1) {
  31. newBrightness = 1;
  32. }
  33. mLayoutParams.screenBrightness = newBrightness;
  34. mWindow.setAttributes(mLayoutParams);
  35. changeBarView.showInfo((int) (newBrightness * 100), false);
  36. }
  37. @Override
  38. public void onVolumeGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  39. int value = fl_big_layout.getHeight() / maxVolume;
  40. int newVolume = (int) ((e1.getY() - e2.getY()) / value + oldVolume);
  41. mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, AudioManager.FLAG_PLAY_SOUND);
  42. if (newVolume < 0) {
  43. newVolume = 0;
  44. }
  45. //要强行转Float类型才能算出小数点,不然结果一直为0
  46. int volumeProgress = (int) (newVolume / Float.valueOf(maxVolume) * 100);
  47. if (volumeProgress > 100) {
  48. volumeProgress = 100;
  49. }
  50. changeBarView.showInfo(volumeProgress, true);
  51. }
  52. @Override
  53. public void onVideoSpeedGesture(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  54. }
  55. @Override
  56. public void onSingleTapGesture(MotionEvent e) {
  57. hideController(isGoneView);
  58. }
  59. @Override
  60. public void onDoubleTapGesture(MotionEvent e) {
  61. }
  62. @Override
  63. public void onVideoSpeedEnd(MotionEvent e) {
  64. }

 

 

PS:只是简单地进行复制代码 ,保证可以使用 。 稍等我在上传 demo 。

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/643023
推荐阅读
相关标签
  

闽ICP备14008679号