java Android - 如何处理两指触摸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11350064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to handle two finger touch
提问by Bemipefe
The documentation say this about that:
文档是这样说的:
A gesture starts with a motion event with ACTION_DOWN that provides the location of the first pointer down. As each additional pointer that goes down or up, the framework will generate a motion event with ACTION_POINTER_DOWN or ACTION_POINTER_UP accordingly.
一个手势从一个带有 ACTION_DOWN 的运动事件开始,它提供了第一个向下指针的位置。随着每个额外的指针下降或上升,框架将相应地生成一个带有 ACTION_POINTER_DOWN 或 ACTION_POINTER_UP 的运动事件。
So i have done the override of onTouchEvent function in my activity:
所以我在我的活动中完成了 onTouchEvent 函数的覆盖:
@Override
public boolean onTouchEvent(MotionEvent MEvent)
{
motionaction = MEvent.getAction();
if(motionaction == MotionEvent.ACTION_DOWN)
{
System.out.println("DEBUG MESSAGE POINTER1 " + MEvent.getActionIndex() );
}
if(motionaction == MotionEvent.ACTION_POINTER_DOWN)
{
System.out.println("DEBUG MESSAGE POINTER2 " + MEvent.getActionIndex() );
}
}
Unfortunately the second if is never entered. The activity contains 2 view with 2 OnTouchListener, i know that onTouchEventis called only if the view of the activity don't consume the event so i tried to return false in the listener and in that way i can recognize only the first finger touch but this avoid the listener to receive the ACTION_UP event and don't allow me to recognize the second finger touch. I also tried to return true in the listener but after manually invoke the onTouchEvent function but this allow me to recognize only the first finger touch too.
不幸的是,第二个 if 从未输入过。该活动包含 2 个带有 2 个OnTouchListener 的视图,我知道只有在 Activity 的视图不消耗该事件时才会调用onTouchEvent所以我试图在侦听器中返回 false 并且这样我只能识别第一个手指触摸但是这避免了侦听器接收 ACTION_UP 事件并且不允许我识别第二个手指触摸。我还尝试在侦听器中返回 true,但是在手动调用 onTouchEvent 函数之后,这也让我只能识别第一根手指触摸。
What's wrong in my code ?
我的代码有什么问题?
回答by The Original Android
I believe your code is missing the masking operation like:
我相信您的代码缺少屏蔽操作,例如:
switch (motionaction & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
}
This code should be able to check for ACTION_POINTER_DOWN.
此代码应该能够检查 ACTION_POINTER_DOWN。
Good luck & tell us what happens.
祝你好运并告诉我们会发生什么。
Tommy Kwee
汤米·奎