一个 OnGestureListener 对象可以处理两个 GestureDetector 对象吗?

发布于 2024-11-18 18:25:03 字数 843 浏览 3 评论 0原文

我正在创建一个需要消耗几乎所有手势的视图。为此,我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卷耳 2024-11-25 18:25:03

要确定 MotionEvent 是否为多点触摸事件,只需使用 MotionEvent.getPointerCount() > 1..所以我认为下面的代码会很好地工作:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getPointerCount() > 1) {
        mScaleDetector.onTouchEvent(event);
    } else {
        mDetector.onTouchEvent(event);
    }
    return true;
}

To determine whether the MotionEvent is a multitouch event, simply use the MotionEvent.getPointerCount() > 1. So I think the following code will work well:

public boolean onTouchEvent(MotionEvent event) {
    if (event.getPointerCount() > 1) {
        mScaleDetector.onTouchEvent(event);
    } else {
        mDetector.onTouchEvent(event);
    }
    return true;
}
彼岸花ソ最美的依靠 2024-11-25 18:25:03

如果 ScaleGestureDetector 和 GestureDetector 相互干扰,它们可能会互相干扰
消费同一个事件。然而,似乎你可以阻止这种相互的
总是先调用水垢检测器然后检查,这令人烦恼
运行常规检测器之前的 isInProgress() 方法

就个人而言,我没有发现让它们处理相同的触摸事件有任何问题。

android GestureDetector 有一个 构造函数,采用布尔值忽略MultiTouch。将 ignoreMultiTouch 设置为 true 将确保 GestureDetector 触摸事件处理忽略任何多点触摸事件。 (如果目标 API 级别 >= Froyo,Android 实际上会将 ignoreMultiTouch 设置为 true,因此您可能不需要显式设置它。)

如果您只调用 < code>mPrimaryDetector.onTouchEvent(event),当mScaleDetector.isInProgress()返回false时,会错误地获取长按事件。原因是 GestureDetector 在其 onTouchEvent(MotionEvent ev) 中有以下代码,以确保它不会与多点触控手势冲突:

case MotionEvent.ACTION_POINTER_DOWN:
  if (mIgnoreMultitouch) {
    // Multitouch event - abort.
    cancel();
  }
  break;

cancel()将按照其指示执行并取消任何单点触摸手势。 (如果你真的很好奇,你可以看看 GestureDetector 代码 自己的;它实际上使用处理程序来发送/删除消息)。

希望这可以帮助任何遇到与我相同问题的人。

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

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 boolean ignoreMultiTouch. Setting ignoreMultiTouch to true will ensure that the GestureDetector touch event processing ignores any mutitouch events. (Android actually sets ignoreMultiTouch to true if the target API level is >= Froyo, so you probably won't need to explicitly set it.)

If you only call mPrimaryDetector.onTouchEvent(event), when mScaleDetector.isInProgress() returns false, you will incorrectly get a long press event. The reason is the GestureDetector has the following code in its onTouchEvent(MotionEvent ev) to ensure it does not conflict with multitouch gestures:

case MotionEvent.ACTION_POINTER_DOWN:
  if (mIgnoreMultitouch) {
    // Multitouch event - abort.
    cancel();
  }
  break;

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.

疾风者 2024-11-25 18:25:03

这对我来说非常有用:

@Override
public boolean onTouchEvent(MotionEvent event) {
    m_sGestureDetector.onTouchEvent(event);
    m_GestureDetector.onTouchEvent(event);
    return true;
}

This works great for me:

@Override
public boolean onTouchEvent(MotionEvent event) {
    m_sGestureDetector.onTouchEvent(event);
    m_GestureDetector.onTouchEvent(event);
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文