My application makes use of the support package to implement Fragments on 2.2 upwards.
I have a View subclass (let’s call it ZoomableView) that makes use of ScaleGestureDetector to detect pinch scaling events. This is done in the familiar way; that is, within onTouchEvent(), the MotionEvents are passed to the detector using mScaleDetector.onTouchEvent(event). A SimpleOnScaleGestureListener is used to receive the scale events.
This ZoomableView class is always displayed as the sole View within a Fragment (let’s call it ZoomableFragment). There are two use cases for it within the application. In one use case, the layout only consists of the ZoomableFragment, containing the ZoomableView. In the second use case, the ZoomableFragment will be displayed alongside another Fragment. This other Fragment happens to be housing a MapView, but the use of MapView isn’t relevant to the problem (the problem still occurs if that second Fragment just contains an ordinary View).
The actual problem is that when testing on a pre-Jellybean device (specifically, a Froyo phone), when using the Activity that shows the twin-Fragment layout, the ZoomableFragment‘s ZoomableView ScaleGestureDetector doesn’t work. When I say ‘doesn’t work’, I can see in the debugger that it processes the MotionEvents, but none of the methods in the SimpleOnScaleGestureListener are called. However, when using the Activity that contains only the ZoomableFragment, the scale detection does work. If I try the application on my JellyBean devices, the twin-Fragment situation works absolutely fine; that is, the scale gestures are received.
I’ve searched around and the only issue I can find is where people have implemented OnScaleGestureListener and prevented it from working by returning false from onScaleBegin(). That’s not my issue here.
So in summary, a ScaleGestureDetector doesn’t call its listener when it should do when used within a View on a multiple support Fragment layout on Froyo (Android 2.2) and I assume up to and including ICS. If however the Fragment is the only one on the screen, the detector works fine. Furthermore, the problem doesn’t exist whatsoever on my 4.1+ Jellybean devices.
EDIT 1 In my twin-Fragment layout, the ZoomableFragment has always been placed on the right or bottom depending on orientation. An interesting observation I just made is that if I flip the layout such that the ZoomableFragment is at the top (rather than being the lower fragment) in the portrait orientation, or is on the left in horizontal orientation, then the scale detector works! I therefore suspect that this is something to do with the relation between the results of the MotionEvent getRawX() / getX() (and similar for Y) methods, when Fragments are used. But when I flip the layout like this, pinch zooming then doesn’t work for the adjacent MapView Fragment! So, in summary, on Froyo it seems that ScaleGestureDetector is working only for a View contained in a Fragment that has a corner at 0,0 of the screen.
EDIT 2 I’ve now answered this question myself with a simple solution I’ve come to after inspecting the source for ScaleGestureDetector on grepcode. Because the source even as far as ICS made use of getRawX() / getRawY(), I’ve updated this question to state as far as ICS being affected (I believe), rather than just Froyo as I first thought.
EDIT 3 I’ve done a simple test to completely eliminate anything to do with Fragments or anything else in my application. I took the simple Google MapView example project and simply changed the layout so that most of the screen is taken up by a TextView (weight 0.9) and a tiny MapView sits at the bottom with a weight of just 0.1. On my Froyo device, it will no longer pinch zoom. On my JB device, it does. So it seems I’m not going nuts, and I have come across a problem with the GestureScaleDetector on ICS and below (I think – certainly Froyo, anyway) for which the answer gives a fix.
From inspecting the
ScaleGestureDetectorsource code on grepcode, I see that Jellybean versions ofScaleGestureDetectordo not make use ofgetRawX()andgetRawY()methods of theMotionEvent. However, the earlier versions (Froyo, ICS etc) do make use ofgetRawX()andgetRawY()for the purpose of slop calculation. This is where the problem is, because it means that the scale detector won’t work if the targetViewdoesn’t have its top left near 0,0.I got the
ScaleGestureDetectorto work on my customZoomView, even with it being placed on the right or bottom, by simply doing this withinonTouchEvent():That causes adjustment of the
MotionEvent‘s internal offset variables such that thegetX/YandgetRawX/Ymethods now return exactly the same value. This prevents the slop calculation failing. The scale detector of course works with relative values and so it doesn’t matter about the absolute values of X / Y. If you have further code that relies on the absolute values, you could do what I did to restore them after:Furthermore, if I want to place my
Fragmentthat contains aMapViewon the bottom or right, I am already using a custom subclass ofMapViewanyway and so I just added this:Now, my
MapViewwill pinch scale properly too, no matter where itsFragmentis placed. (By the way – totally OT to this question, but in case anyone is wondering, I placed aMapViewinto aFragmentsuccessfully using theLocalActivityManagersolution as given on StackOverflow). EDIT: Actually, something is not 100% right: The map doesn’t zoom around the center of the pinch gesture. I think it’s to do with the ActionBar / notification bar height not being compensated for in thegetTop().