Question

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;
    }
    
Was it helpful?

Solution

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.

OTHER TIPS

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;
}

This works great for me:

@Override
public boolean onTouchEvent(MotionEvent event) {
    m_sGestureDetector.onTouchEvent(event);
    m_GestureDetector.onTouchEvent(event);
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top