一个 OnGestureListener 对象可以处理两个 GestureDetector 对象吗?
我正在创建一个需要消耗几乎所有手势的视图。为此,我创建了一个 ScaleGestureDetector 和一个 GestureDetector。我还创建了一个侦听器类,并意识到我可以让它实现我需要的每个接口;我就是这么做的。这对于 OnGestureListener 和 OnDoubleTapListener 来说完全有意义,因为它们来自同一个类,但是:
- ScaleGestureDetector 是否期望有自己的侦听器类?
- 如果它对同一个类感到满意,它会期望有自己的对象吗?
- 相反,我是否需要对两个检测器使用相同的侦听器?
实验证实了以下几点:
- 您确实可以使用一个侦听器类,但
- ScaleGestureDetector 和 GestureDetector 如果消耗相同的事件,可能会互相干扰。然而,
- 似乎您可以通过始终先调用秤检测器然后在运行常规检测器之前检查其 isInProgress() 方法来防止这种相互烦恼:
public boolean onTouchEvent(MotionEvent event) { //让ScaleGestureDetector先尝试 mScaleDetector.onTouchEvent(事件); //如果 isInProgress() 返回 true 则它正在消耗该事件 if(mScaleDetector.isInProgress()) 返回 true; //如果 isInProgress() 返回 false 则不会消耗该事件 //因此将其传递给常规检测器是安全的 mPrimaryDetector.onTouchEvent(事件); 返回真; }
I am creating a View that needs to consume pretty much any gesture going. To do this I created a ScaleGestureDetector and a GestureDetector. I also created one listener class and realized I could have it implement every interface I needed; so I did. This makes total sense for OnGestureListener and OnDoubleTapListener because they come from the same class, but:
- Will the ScaleGestureDetector expect its own listener class?
- If it's happy with the same class, will it expect its own object?
- Conversely, do I NEED to use the same listener with both detectors?
Experiment has confirmed the following:
- You can indeed use one listener class, but
- ScaleGestureDetector and GestureDetector can annoy each other if they consume the same event. However
It seems you can prevent this mutual irking by always calling the scale detector FIRST and then checking its isInProgress() method before running the regular detector:
public boolean onTouchEvent(MotionEvent event) { //let the ScaleGestureDetector try first mScaleDetector.onTouchEvent(event); //if isInProgress() returns true then it's consuming the event if(mScaleDetector.isInProgress()) return true; //if isInProgress() returns false it isn't consuming the event //it's therefore safe to pass it to the regular detector mPrimaryDetector.onTouchEvent(event); return true; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要确定 MotionEvent 是否为多点触摸事件,只需使用 MotionEvent.getPointerCount() > 1..所以我认为下面的代码会很好地工作:
To determine whether the MotionEvent is a multitouch event, simply use the
MotionEvent.getPointerCount() > 1
. So I think the following code will work well:就个人而言,我没有发现让它们处理相同的触摸事件有任何问题。
android
GestureDetector
有一个 构造函数,采用布尔值忽略MultiTouch
。将ignoreMultiTouch
设置为true
将确保GestureDetector
触摸事件处理忽略任何多点触摸事件。 (如果目标 API 级别 >= Froyo,Android 实际上会将ignoreMultiTouch
设置为true
,因此您可能不需要显式设置它。)如果您只调用 < code>mPrimaryDetector.onTouchEvent(event),当
mScaleDetector.isInProgress()
返回false时,会错误地获取长按事件。原因是GestureDetector
在其onTouchEvent(MotionEvent ev)
中有以下代码,以确保它不会与多点触控手势冲突:cancel()
将按照其指示执行并取消任何单点触摸手势。 (如果你真的很好奇,你可以看看 GestureDetector 代码 自己的;它实际上使用处理程序来发送/删除消息)。希望这可以帮助任何遇到与我相同问题的人。
Personally, I have not found any issues by letting them both handle the same touch event.
The android
GestureDetector
has a constructor which takes a booleanignoreMultiTouch
. SettingignoreMultiTouch
totrue
will ensure that theGestureDetector
touch event processing ignores any mutitouch events. (Android actually setsignoreMultiTouch
totrue
if the target API level is >= Froyo, so you probably won't need to explicitly set it.)If you only call
mPrimaryDetector.onTouchEvent(event)
, whenmScaleDetector.isInProgress()
returns false, you will incorrectly get a long press event. The reason is theGestureDetector
has the following code in itsonTouchEvent(MotionEvent ev)
to ensure it does not conflict with multitouch gestures:cancel()
will do what it says and cancel any single touch gestures. (If you're really curious you can look at that GestureDetector code yourself; it actually uses a handler to send/remove messages).Hope this helps anyone who was having the same issues I was.
这对我来说非常有用:
This works great for me: