【问题标题】:How to detect two-finger tap in Android?如何在Android中检测两指点击?
【发布时间】:2017-04-12 14:51:29
【问题描述】:

我需要在我正在开发的 Android 应用程序中检测两指点击。我还使用ScaleGestureDetector 和GestureDetector 来检测点击、双击、长按和缩放。

在我的onTouchEvent 方法中,我有:

@Override
public boolean onTouchEvent(MotionEvent event) {
    scaleGestureDetector.onTouchEvent(event);
    gestureDetector.onTouchEvent(event);

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        activePointerId = event.getPointerId(0);
        break;
    .
    .
    .
    case MotionEvent.ACTION_POINTER_UP:
        int pointerId = event.getPointerId(event.getActionIndex());

        if (pointerId == activePointerId) {
            // change active pointer
        } else if (!scaleDetector.isInProgress() && (event.getPointerCount() == 2)) {
            // handle two-finger tap
        }
        break;
    .
    .
    .

问题在于,缩放也被检测为两指轻敲。关于解决这个问题的任何想法?谢谢!

【问题讨论】:

    标签: android multi-touch gesture


    【解决方案1】:

    我以前也遇到过这样的问题。最后我必须编写自己的类来扩展 GestureDetector。你可以自己实现缩放。

    【讨论】:

      【解决方案2】:
      private int tap_count=0;
          @Override
              public boolean onTouchEvent(MotionEvent event) {
      
              int action = event.getAction() & MotionEvent.ACTION_MASK;
              switch(action) {
      
                  case MotionEvent.ACTION_DOWN : {
      
                      ++tap_count;
                      Log.d(LOG_TAG, "MotionEvent.ACTION_DOWN "+tap_count);
                      break;
                  }
      
                  case MotionEvent.ACTION_MOVE : {
                      break;
                  }
      
                  case MotionEvent.ACTION_POINTER_DOWN : {
      
                      ++tap_count;
                      Log.d(LOG_TAG, "MotionEvent.ACTION_POINTER_DOWN "+tap_count);
                      break;
                  }
      
                  case MotionEvent.ACTION_POINTER_UP : {
      
                      ++tap_count;
                      Log.d(LOG_TAG, "MotionEvent.ACTION_POINTER_UP "+tap_count);
                      break;
                  }
      
                  case MotionEvent.ACTION_UP : {
      
                      --tap_count;
                      Log.d(LOG_TAG, "MotionEvent.ACTION_UP "+tap_count);
      
                      if(tap_count == 2){
                          tap_count = 0;
                          Toast.makeText(mContext,"Enableing Accessbility",Toast.LENGTH_SHORT).show();
                          return true;
                      }
                      break;
                  }
              }
              return true;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多