开发者

ScaleGestureDetector not detecting gesture end

开发者 https://www.devze.com 2023-01-23 09:47 出处:网络
I have hooked in a ScaleGestureDetector to an OnTouchListener as instructed in Android documentation. For some reason the ScaleGestureDetector does not always detect end of a pinch gesture. This happe

I have hooked in a ScaleGestureDetector to an OnTouchListener as instructed in Android documentation. For some reason the ScaleGestureDetector does not always detect end of a pinch gesture. This happens mostly when pinching fast from larg开发者_如何学编程e to small.

The effect is that after I have released both fingers the detector does not fire the gesture end event. If I touch the screen with one afterwards it still thinks I'm continuing the scale gesture (keeps firing onScale events). I have to do another gesture to get the detector to fire end event.

I have added logs to the OnTouchListener and when the scale gesture gets stuck I still get motion events normally when using one finger and event.getPointerCount() is 1.

I have made sure that I don't have any other listeners intervening. View that has the onTouchListener is not the only view on screen but this effect happens also when I'm very careful to begin and end the gesture inside this one view.

Is there any way to improve the end detection?

Or if there a way for me to manually set the ScaleGestureDetector to fire onScaleEnd and change scaleGestureDetector.isInProgress() to false?


I came up with a solution, after applying the above fix. My application uses both a ScaleGestureDetector and a normal GestureDetector so I implemented in my onScroll method (from the GestureDetector):

if (m_dualFingerGestureDetector.isInProgress() && (evt1.getPointerCount() == 2 || evt2.getPointerCount == 2)
{
    return false;
}
...

My variables are as follows:

m_dualFingerGestureDetector is my ScaleGestureDetector
evt1 and evt2 are MotionEvents for a drag gesture. If you only have one event, you can shorten the above code:

if (m_dualFingerGestureDetector.isInProgress() && evt.getPointerCount() == 2)
{
    return false;
}
...

This allows very nice user interfaces to be built. I would always recommend using the combination of gesture detectors to add pan-and-zoom features to your app.


You might be running into the following bug http://code.google.com/p/android/issues/detail?id=10067

I've had to copy the ScaleGestureDetector implementation into my local project so I could apply the fix. I'm also targeting 2.1 devices so this class is otherwise not available to me unless I copy it into my project.

https://android.googlesource.com/platform/frameworks/base/+/gingerbread-release/core/java/android/view/ScaleGestureDetector.java

0

精彩评论

暂无评论...
验证码 换一张
取 消