【问题标题】:Error in implementing Android double Tap实现 Android 双击时出错
【发布时间】:2014-01-03 05:32:26
【问题描述】:

我在我的 Activity 类中实现了 OnDoubleTapListener 并覆盖了以下三个方法。

@Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }



    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        if(e.getAction() == 1){
            Toast.makeText(getApplicationContext(),"Double tap happened", Toast.LENGTH_SHORT).show();
        }
        return true;
    }



    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

但是当我在真实设备上运行它时,什么也没发生。错误是什么?另外我怎么能找到特定的录音项目(双录音项目)?

我看到一些教程使用 onTouchEvent(MotionEvent e) 方法并计算两次触摸之间的时间差。执行此过程的正确方法是什么?

【问题讨论】:

    标签: android events ontouchlistener double-click tap


    【解决方案1】:

    试试这个:

    public class MyView extends View {
    
        GestureDetector gestureDetector;
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            // creating new gesture detector
            gestureDetector = new GestureDetector(context, new GestureListener());
        }
    
        // skipping measure calculation and drawing
    
        // delegate the event to the gesture detector
        @Override
        public boolean onTouchEvent(MotionEvent e) {
            return gestureDetector.onTouchEvent(e);
        }
    
    
        private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
            // event when double tap occurs
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                float x = e.getX();
                float y = e.getY();
    
                Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
    
                return true;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      GestureDetector 将消耗你已经在孩子身上拥有的触摸事件,例如,如果你不希望你可以使用下面的代码,我不使用ACTION_UP 所以我不需要消耗事件(只有第二次点击将被消耗)

      public class DoubleTapDetector {
      
          public interface OnDoubleTapListener {
              boolean onDoubleTap(MotionEvent e);
      
              boolean onDoubleTapEvent(MotionEvent e);
          }
      
          private int mDoubleTapSlopSquare;
      
          private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();
          private static final int DOUBLE_TAP_MIN_TIME = 40;
      
          private static final int DOUBLE_TAP_SLOP = 100;
      
          private OnDoubleTapListener mDoubleTapListener;
      
          private MotionEvent mCurrentDownEvent;
      
          public DoubleTapDetector(Context context, OnDoubleTapListener listener) {
              mDoubleTapListener = listener;
              init(context);
          }
      
          private void init(Context context) {
              if (mDoubleTapListener == null) {
                  throw new NullPointerException("OnDoubleTapListener must not be null");
              }
      
              int doubleTapSlop;
              if (context == null) {
                  doubleTapSlop = DOUBLE_TAP_SLOP;
              } else {
                  final ViewConfiguration configuration = ViewConfiguration.get(context);
                  doubleTapSlop = configuration.getScaledDoubleTapSlop();
              }
              mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;
          }
      
          public boolean onTouchEvent(MotionEvent ev) {
              final int action = ev.getAction();
      
              boolean handled = false;
      
              switch (action) {
              case MotionEvent.ACTION_DOWN:
                  if ((mCurrentDownEvent != null) &&
                      isConsideredDoubleTap(mCurrentDownEvent, ev)) {
                      // This is a second tap
                      // Give a callback with the first tap of the double-tap
                      handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
                      // Give a callback with down event of the double-tap
                      handled |= mDoubleTapListener.onDoubleTapEvent(ev);
                  } else {
                      // This is a first tap
                  }
      
                  if (mCurrentDownEvent != null) {
                      mCurrentDownEvent.recycle();
                  }
                  mCurrentDownEvent = MotionEvent.obtain(ev);
                  break;
              }
      
              return handled;
          }
      
          private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent secondDown) {
              final long deltaTime = secondDown.getEventTime() - firstDown.getEventTime();
              if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
                  return false;
              }
      
              int deltaX = (int) firstDown.getX() - (int) secondDown.getX();
              int deltaY = (int) firstDown.getY() - (int) secondDown.getY();
              return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);
          }
      
      }
      

      用法:

      DoubleTapDetector doubleTapDetector = new DoubleTapDetector(conversationWindow, new DoubleTapDetector.OnDoubleTapListener() {
              @Override
              public boolean onDoubleTap(MotionEvent e) {
                  // Double tap detected.
                  return false;
              }
      
              @Override
              public boolean onDoubleTapEvent(MotionEvent e) {
                  return false;
              }
          });
      
      someView.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View view, MotionEvent e) {
                  return doubleTapDetector.onTouchEvent(e);
              }
          });
      

      【讨论】:

      • @fa Gazar 感谢您的回复!但是我得到一个运行时异常并且应用程序已经停止。
      • @IBunny 你能分享一些代码或者运行时异常的原因
      • 这是我得到的例外:
      • 这里发帖太长了。我在我的活动中这样写了你的代码: DoubleTapDetector doubleTapDetector = new DoubleTapDetector(this, new DoubleTapDetector.OnDoubleTapListener() { The exception :01-22 10:07:50.207: E/AndroidRuntime(21884): at com.example.androiddoubletap .DoubleTapDetector.init(DoubleTapDetector.java:43) 01-22 10:07:50.207: E/AndroidRuntime(21884): at com.example.androiddoubletap.DoubleTapDetector.(DoubleTapDetector.java:30)
      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多