问题
My application makes use of the support package to implement Fragment
s 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 MotionEvent
s 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 MotionEvent
s, 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 Fragment
s 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.
回答1:
From inspecting the ScaleGestureDetector
source code on grepcode, I see that Jellybean versions of ScaleGestureDetector
do not make use of getRawX()
and getRawY()
methods of the MotionEvent
. However, the earlier versions (Froyo, ICS etc) do make use of getRawX()
and getRawY()
for the purpose of slop calculation. This is where the problem is, because it means that the scale detector won't work if the target View
doesn't have its top left near 0,0.
I got the ScaleGestureDetector
to work on my custom ZoomView
, even with it being placed on the right or bottom, by simply doing this within onTouchEvent()
:
event.setLocation(event.getRawX(), event.getRawY());
mScaleDetector.onTouchEvent(event);
That causes adjustment of the MotionEvent
's internal offset variables such that the getX/Y
and getRawX/Y
methods 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:
float originalY = event.getY();
float originalX = event.getX();
event.setLocation(event.getRawX(), event.getRawY());
mScaleDetector.onTouchEvent(event);
event.setLocation(originalX, originalY);
Furthermore, if I want to place my Fragment
that contains a MapView
on the bottom or right, I am already using a custom subclass of MapView
anyway and so I just added this:
public boolean onTouchEvent(MotionEvent event) {
// Work around required for ScaleGestureDetector.
// Before calling the scale detector, perform a work-around that is needed for earlier APIs (I suspect
// ICS and below, judging from inspection of ScaleGestureDetector) to make the MotionEvent give the
// same values for getY / getRawY and getX / getRawX. Wildly different values, caused by the View
// being nowhere near the screen origin (i.e. the View is on the right or the bottom) means the
// detector doesn't work.
event.setLocation(event.getRawX() - getLeft(), event.getRawY() - getTop());
return super.onTouchEvent(event);
}
Now, my MapView
will pinch scale properly too, no matter where its Fragment
is placed. (By the way - totally OT to this question, but in case anyone is wondering, I placed a MapView
into a Fragment
successfully using the LocalActivityManager
solution 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 the getTop()
.
来源:https://stackoverflow.com/questions/13791904/scalegesturedetector-not-working-for-view-within-fragment-or-viewgroup-layout-on