Weird onScroll event triggered after onScale event

随声附和 提交于 2019-12-05 06:05:50

I also found this peculiar behaviour building a custom view with pan/zoom capabilities. But after some thought, here is my reasoning:

In a multitouch environment, each finger is registered and their respective motions are processed in some kind of parallel analysis. This is what allows the detection of the different touch events the system can send by means of the OnGestureListener and OnScaleGestureListener.

Right, nothing you don't already know.

Now, think the different behaviour of the two detectors:

  • GestureDetector detects an scroll event by means of a drag with only one finger inside the limits of the viewable area of the view. Its pattern responds to: down - drag - up. Scroll events are produced as the drag events are detected.

  • ScaleGestureDetector detects scale events by means of two simultaneous drags fired by two fingers in a multitouch environment. Its pattern responds to: (down1&down2) - (drag1 and/or drag2) - (up1 or up2).

And now, think of a custom view where you only need to detect scroll events (ignoring all the other). In such a case, the scroll event must be fired aside all other considerations because you have performed its pattern (down-drag-up).

When you combine this two detectors, they act independently, so the scale detector fires first, but when you lift the second finger, the scroll detector fires because it detects one finger that is dragging and finishing with an up event!

Conclusion: The behaviour seems reasonable... yet Android could have provided some cross detector for the simultaneous situation.

Well, you can simply put a boolean and solve the problem. I had this done in my implementation:

  • Declare a boolean named scaling
  • Make your onDown event (on the ACTION_DOWN event) to clear scaling
  • Make your onScale event to set scaling
  • Make your onScroll event not to process the scroll if the scaling flag is true

This worked for me already.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!