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; }
Personally, I have not found any issues by letting them both handle the same touch event.
The android
GestureDetectorhas a constructor which takes a booleanignoreMultiTouch. SettingignoreMultiTouchtotruewill ensure that theGestureDetectortouch event processing ignores any mutitouch events. (Android actually setsignoreMultiTouchtotrueif 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 theGestureDetectorhas 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.