自定义ImageView处理其长按和点击事件

来源:互联网 发布:淘宝刷好评多少钱一单 编辑:程序博客网 时间:2024/05/22 15:06

    ​在最近的工作中,有一个需求,就是需要自定义一个按钮,在点击的时候进行拍照,在长按的时候进行视频的录制模式,所以想到了继承ImageView,或者其他的Button也OK,在事件拦截中去处理该操作。具体的思路是在MotionEvent.ACTION_DOWN方法中去启动一个线程去进行计时操作,如果在指定时间段内,比如500毫秒,没有将手指抬起来,则认为用户进行了长按的操作,否则用户就是进行了点击按钮的操作,就可以当作拍照处理。以下是具体的代码实现,注释比较清楚:

  1. package com.example.cretin.previoustesttwo;
  2. import android.annotation.TargetApi;
  3. import android.content.Context;
  4. import android.os.Build;
  5. import android.util.AttributeSet;
  6. import android.view.MotionEvent;
  7. import android.widget.ImageView;
  8. /**
  9. * Created by cretin on 15/12/15.
  10. */
  11. public class TakePhotoView extends ImageView {
  12. //是否释放了
  13. private boolean isReleased;
  14. //计数器,防止多次点击导致最后一次形成longpress的时间变短
  15. private int mCounter;
  16. //长按的runnable
  17. private Runnable mLongPressRunnable;
  18. //定义相应时间 以此为依据判断是长按还是短按 默认为300毫秒
  19. private int minTime = 300;
  20. public int getMinTime() {
  21. return minTime;
  22. }
  23. public void setMinTime(int minTime) {
  24. this.minTime = minTime;
  25. }
  26. public OnTakePhotoClickListener getShortClickListener() {
  27. return shortClickListener;
  28. }
  29. public void setShortClickListener(OnTakePhotoClickListener shortClickListener) {
  30. this.shortClickListener = shortClickListener;
  31. }
  32. private OnTakePhotoClickListener shortClickListener;
  33. private OnLongTakePhotoClickListener longClickListener;
  34. public OnLongTakePhotoClickListener getLongClickListener() {
  35. return longClickListener;
  36. }
  37. public void setLongClickListener(OnLongTakePhotoClickListener longClickListener) {
  38. this.longClickListener = longClickListener;
  39. }
  40. public TakePhotoView(Context context) {
  41. this(context, null,0);
  42. }
  43. public TakePhotoView(Context context, AttributeSet attrs) {
  44. this(context, attrs, 0);
  45. }
  46. public TakePhotoView(Context context, AttributeSet attrs, int defStyleAttr) {
  47. super(context, attrs, defStyleAttr);
  48. mLongPressRunnable = new Runnable() {
  49. @Override
  50. public void run() {
  51. mCounter--;
  52. //计数器大于0,说明当前执行的Runnable不是最后一次down产生的 判断为短按。
  53. if(mCounter>0 || isReleased) {
  54. if (shortClickListener != null) {
  55. shortClickListener.onClick();
  56. }
  57. return;
  58. }
  59. //否则判断为长按操作
  60. if(longClickListener!=null){
  61. longClickListener.onLongClick();
  62. }
  63. }
  64. };
  65. }
  66. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  67. public TakePhotoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  68. this(context, attrs, defStyleAttr);
  69. }
  70. public boolean dispatchTouchEvent(MotionEvent event) {
  71. switch(event.getAction()) {
  72. case MotionEvent.ACTION_DOWN:
  73. mCounter++;
  74. isReleased = false;
  75. postDelayed(mLongPressRunnable, minTime);
  76. break;
  77. case MotionEvent.ACTION_UP:
  78. //释放了
  79. isReleased = true;
  80. break;
  81. }
  82. return true;
  83. }
  84. /**
  85. * 为长按拍照设置一个监听事件
  86. */
  87. public interface OnLongTakePhotoClickListener {
  88. void onLongClick();
  89. }
  90. /**
  91. * 为短按拍照设置一个监听事件
  92. */
  93. public interface OnTakePhotoClickListener {
  94. void onClick();
  95. }
  96. }

0 0
原创粉丝点击