如何在Android中实现两个手指的双击?
我知道如何检测双击和两个手指触摸事件,但我如何将它们结合起来做出反应,以便有人需要用两个手指双击如何在Android中实现两个手指的双击?,android,multi-touch,gesture,double-click,Android,Multi Touch,Gesture,Double Click,我知道如何检测双击和两个手指触摸事件,但我如何将它们结合起来做出反应,以便有人需要用两个手指双击 默认情况下,Android有长按按钮作为第二种形式的点击,但我特别寻找双指双击。这是我创建的用于检测双指双击的双击侦听器 使用的变量: private GestureDetector gesture; private View.OnTouchListener gestureListener; boolean click1 = false; boolean click2 = false; long f
默认情况下,Android有长按按钮作为第二种形式的点击,但我特别寻找双指双击。这是我创建的用于检测双指双击的双击侦听器 使用的变量:
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;
}
@覆盖
公共布尔onTouchEvent(MotionEvent事件){
试一试{
int action=event.getAction()&MotionEvent.action\u掩码;
//当用户抬起手指而不是按下按钮时捕获事件
//以确保他们不会长期处于压力之下
if(action==MotionEvent.action\u指针\u向上){
//计时器以获取单击之间的差异
Calendar now=Calendar.getInstance();
//检测手指数,单手指双击更改为1,三手指双击更改为3,等等。
if(event.getPointerCount()==2){
如果(!单击1){
//如果这是第一次点击,那么就没有第二次了
//点击,同时记录时间
单击1=真;
click2=false;
first=now.getTimeInMillis();
}否则如果(单击1){
//如果这是第二次单击,请记录其时间
click2=true;
second=now.getTimeInMillis();
//如果两次点击之间的差异小于500毫秒(1/2秒)
//之所以使用Math.abs(),是因为您需要能够检测任何点击序列,而不仅仅是两次成对的点击
//(例如,如果click1和click2之间的差异大于500,但
//单击2,下一次单击1小于500)
if(数学绝对值(第二优先)<500){
//做点什么!!!!!!
}如果(数学绝对值(第二优先)>=500){
//重置以处理更多单击
click1=false;
click2=false;
}
}
}
}
}捕获(例外e){
}
返回true;
}
我想要一个简单且可重复使用的界面,它可以监听两个手指的双击,其行为类似于GestureDetector。这样您就可以像这样使用它(所有剪切粘贴可运行代码):
我创建了SimpleTofIngerDoubleTapDetector。(这是一个很长的名称,但它是描述性的。您可以根据需要重命名它。)将此新文件保存在项目中或保存为库:
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();
}
private View mFirstView;
操作向下更改
案例:
case MotionEvent.ACTION_DOWN:
if(mFirstDownTime == -1 || mFirstView != v || hasTimedOut(event.getEventTime())) {
mFirstView = v;
reset(event.getDownTime());
}
break;
onTwoFingerDoubleTap(mFirstView);
mFirstView
传递到ACTION\u UP
案例中:
case MotionEvent.ACTION_DOWN:
if(mFirstDownTime == -1 || mFirstView != v || hasTimedOut(event.getEventTime())) {
mFirstView = v;
reset(event.getDownTime());
}
break;
onTwoFingerDoubleTap(mFirstView);
onTwoFingerDoubleTap()
方法以反映所点击的视图:
public abstract void onTwoFingerDoubleTap(View v);
if(click1&&click2&&Math.abs(第二个第一)<500){…}
。除非click1&&click2
已为true,否则无法访问代码的此特定部分。要到达这里,您必须通过以下步骤:if(click1){click2=true;…}
。如果(Math.abs(second-first)<500){//do something!!!!!!!}或者{click1=false;click2=false;}@Nezam,我已经好几年没有使用过这个代码了,但我不确定为什么会这样-它在我们的应用程序中运行得很好这个答案太棒了!我试过这段代码(谢谢!),但没有成功——我只是在处理一些事件。我相信onTouchEvent()函数必须返回True(对于ACTION\u DOWN),否则之后将不会收到ACTION\u UP。参考:谢谢,尽管它不起作用。这有助于我找到正确的答案。