如何实现在Android上两个手指双击?(How to implement a two-finger

2019-06-28 08:31发布

我知道如何检测双击和双指触摸事件,但我怎么能组合这些反应使某人需要双击用两个手指?

默认情况下,Android已经长按充当点击的第二种形式,但我专门找了一两个手指双击。

Answer 1:

我想一个简单的和可重复使用的界面,侦听两个指双水龙头和行为像GestureDetector。 所以,你可以使用它像这样(所有的剪切和粘贴运行的代码):

public class Example extends Activity {
    SimpleTwoFingerDoubleTapDetector multiTouchListener = new SimpleTwoFingerDoubleTapDetector() {
        @Override
        public void onTwoFingerDoubleTap() {
            // Do what you want here, I used a Toast for demonstration
            Toast.makeText(Example.this, "Two Finger Double Tap", Toast.LENGTH_SHORT).show();
        }
    };

    // Override onCreate() and anything else you want

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(multiTouchListener.onTouchEvent(event))
            return true;
        return super.onTouchEvent(event);
    }
}

我创建SimpleTwoFingerDoubleTapDetector。 (这是一个很长的名字,但它是描述性的,您可以将其命名为任何你想要的。)此新文件保存您的项目中或作为一个库:

public abstract class SimpleTwoFingerDoubleTapDetector {
    private static final int TIMEOUT = ViewConfiguration.getDoubleTapTimeout() + 100;
    private long mFirstDownTime = 0;
    private boolean mSeparateTouches = false;
    private byte mTwoFingerTapCount = 0;

    private void reset(long time) {
        mFirstDownTime = time;
        mSeparateTouches = false;
        mTwoFingerTapCount = 0;
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            if(mFirstDownTime == 0 || event.getEventTime() - mFirstDownTime > TIMEOUT) 
                reset(event.getDownTime());
            break;
        case MotionEvent.ACTION_POINTER_UP:
            if(event.getPointerCount() == 2)  
                mTwoFingerTapCount++;
            else 
                mFirstDownTime = 0;
            break;
        case MotionEvent.ACTION_UP:
            if(!mSeparateTouches)
                mSeparateTouches = true;
            else if(mTwoFingerTapCount == 2 && event.getEventTime() - mFirstDownTime < TIMEOUT) {
                onTwoFingerDoubleTap();
                mFirstDownTime = 0;
                return true;
            }
        }               

        return false;
    }

    public abstract void onTwoFingerDoubleTap();
}

首先,关于Android(一触式)GestureDetector几个注意事项:

  • Android的onDoubleTap()事件使用从ViewConfiguration标准的超时值。 我指的是同一时间。
  • 他们测量从第一次轻触的手指落下事件的第二次敲击的手指落下事件经过的时间,然后进行广播onDoubleTap()onDoubleTapEvent()
    • onDoubleTap()当第二次敲击的手指落下事件发生仅触发。
    • onDoubleTapEvent()是通过第二次敲击解雇的每一个动作:向下,移动和向上。

SimpleTwoFingerDoubleTapDetector几点注意事项:

  • 我超时从第一手指向下事件,测量到最后的手指甲事件,以防止假双击通知。 我加一点额外的时间为默认ViewConfiguration双击超时考虑到这一点。
  • Android的GestureDetector测量斜率(相隔多远两个水龙头)。 我没有看到这样做的必要性在这里,我也没有检查每个水龙头的两根手指之间的距离。
  • 我只播出一个事件onTwoFingerDoubleTap()

最后要注意:您可以轻松地改变这种表现得像一个OnTouchListener:

  1. 更改SimpleTwoFingerDoubleTapDetector的定义:

     public abstract class SimpleTwoFingerDoubleTapListener implements OnTouchListener { 
  2. 添加一个新的类变量:

     private View mFirstView; 
  3. 更改ACTION_DOWN情况:

     case MotionEvent.ACTION_DOWN: if(mFirstDownTime == -1 || mFirstView != v || hasTimedOut(event.getEventTime())) { mFirstView = v; reset(event.getDownTime()); } break; 
  4. 通过mFirstView里面ACTION_UP情况:

     onTwoFingerDoubleTap(mFirstView); 
  5. 最后,改变onTwoFingerDoubleTap()方法,以反映该观点是挖掘:

     public abstract void onTwoFingerDoubleTap(View v); 


Answer 2:

这是一个双击监听我创建检测两个手指双击。

变量使用:

private GestureDetector gesture;
private View.OnTouchListener gestureListener;
boolean click1 = false;
boolean click2 = false;
long first = 0;
long second = 0;

在活动的onCreate()来注册触摸事件:

gesture = new GestureDetector(getApplicationContext(), new SimpleOnGestureListener(){
    public boolean onDown(MotionEvent event) {
        return true;
    }
});
gestureListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gesture.onTouchEvent(event);
    }
};

onCreate()在活动中:

@Override
public boolean onTouchEvent(MotionEvent event) {   
    try {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        //capture the event when the user lifts their fingers, not on the down press
        //to make sure they're not long pressing
        if (action == MotionEvent.ACTION_POINTER_UP) {
            //timer to get difference between clicks
            Calendar now = Calendar.getInstance();

            //detect number of fingers, change to 1 for a single-finger double-click, 3 for a triple-finger double-click, etc.
            if (event.getPointerCount() == 2) {
                if (!click1) {
                    //if this is the first click, then there hasn't been a second
                    //click yet, also record the time
                    click1 = true;
                    click2 = false;
                    first = now.getTimeInMillis(); 
                } else if (click1) {
                    //if this is the second click, record its time 
                    click2 = true;
                    second = now.getTimeInMillis();

                    //if the difference between the 2 clicks is less than 500 ms (1/2 second)
                    //Math.abs() is used because you need to be able to detect any sequence of clicks, rather than just in pairs of two
                    //(e.g. click1 could be registered as a second click if the difference between click1 and click2 > 500 but
                    //click2 and the next click1 is < 500)
                    if (Math.abs(second-first) < 500) {

                        //do something!!!!!!

                    } else if (Math.abs(second-first) >= 500) {
                        //reset to handle more clicks
                        click1 = false;
                        click2 = false;
                    }
                }
            }
        }
    } catch (Exception e){

    }
    return true;
}


文章来源: How to implement a two-finger double-click in Android?