如何检测Android中的双指点按?

时间:2012-08-29 14:52:09

标签: android multi-touch gesture

我需要检测我正在处理的Android应用中的双指点击。我还使用ScaleGestureDetectorGestureDetector来检测点击,双击,长按和缩放。

我的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;
    .
    .
    .

问题是缩放也被检测为双指敲击。有关解决此问题的任何想法?谢谢!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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;
        }