如何实现两个手指双击的Android? [英] How to implement a two-finger double-click in Android?
问题描述
我知道如何检测双击和双指触摸事件,但我怎么能结合这些反应使某人需要双击用两个手指?
I know how to detect a double-click and a two-finger touch event, but how can I combine these to react so somebody needs to double click with two fingers?
在默认情况下,Android已经长preSS作为点击的第二个形式,但我专门找了两个手指双击。
By default, Android has the long press to act as a second form of clicking, but I'm specifically looking for a two-finger double-click.
推荐答案
我想要一个简单的,可重复使用的接口,侦听两指双水龙头和行为就像GestureDetector。所以,你可以使用它像这样(全部剪切和放大器;粘贴可运行code):
I wanted a simple and reusable interface that listens for two finger double taps and behaves like GestureDetector. So that you could use it like this (all cut & paste runnable code):
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。 (这是一个很长的名字,但它是描述性的,可以将其重命名为任何你想要的。)此新文件保存您的项目中或作为一个库:
I created SimpleTwoFingerDoubleTapDetector. (It's a long name, but it is descriptive. You can rename it as anything you want.) Save this new file inside your project or as a library:
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()
被解雇通过第二次敲击的每一个动作:向下,移动和向上
- Android's
onDoubleTap()
event uses a standard timeout value from ViewConfiguration. I refer to the same time. - They measure the elapsed time from the first tap's finger-down event to the second tap's finger-down event, and then broadcast
onDoubleTap()
andonDoubleTapEvent()
.onDoubleTap()
is fired only when the second tap's finger-down event occurs.onDoubleTapEvent()
is fired for every action by the second tap: down, move, and up.
上的几个注意事项 SimpleTwoFingerDoubleTapDetector
- 我的超时被从第一手指放下事件测量到最后的指印的向上的事件至prevent假双击通知。我加了一点额外的时间来默认ViewConfiguration双击超时考虑到这一点。
- 在Android的GestureDetector措施污水(相隔多远两个水龙头)。我没有看到这方面的需要在这里,我也没有检查两个手指在每个抽头之间的距离。
- 我只播出一个事件
onTwoFingerDoubleTap()
。
- My timeout is measured from the first finger-down event to the last finger-up event to prevent false double-tap notifications. I added a little extra time to the default ViewConfiguration double tap timeout to account for this.
- Android's GestureDetector measures slop (how far apart the two taps are). I didn't see the need for this here, nor did I check the distance between the two fingers on each tap.
- I only broadcast one event
onTwoFingerDoubleTap()
.
最后说明: 你可以的轻松的改变这种表现得像一个OnTouchListener:
Final note: You can easily change this to behave like an OnTouchListener:
-
更改SimpleTwoFingerDoubleTapDetector的定义:
Change SimpleTwoFingerDoubleTapDetector's definition:
public abstract class SimpleTwoFingerDoubleTapListener implements OnTouchListener {
-
添加一个新的类变量:
Add a new class variable:
private View mFirstView;
-
更改
ACTION_DOWN
案例:case MotionEvent.ACTION_DOWN: if(mFirstDownTime == -1 || mFirstView != v || hasTimedOut(event.getEventTime())) { mFirstView = v; reset(event.getDownTime()); } break;
-
通
mFirstView
的ACTION_UP
盒内: Pass
mFirstView
inside theACTION_UP
case:onTwoFingerDoubleTap(mFirstView);
-
最后,修改
onTwoFingerDoubleTap()
的方法,以反映其景观被窃听: Last, change the
onTwoFingerDoubleTap()
method to reflect which View was tapped:public abstract void onTwoFingerDoubleTap(View v);
这篇关于如何实现两个手指双击的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-