Android gesture ImageView trilogy Part 3

Following the Android gesture ImageView Trilogy (2) in the previous section, we talked about the gesturedetector framework on GitHub, and attached the GitHub link first: https://github.com/Almeros/android-gesture-detectors In fact, the main idea of this framework is also referred to the scalegestuuredetector tool class of Android, Scalegestuuredetector is estimated to be the reference gesturedetector tool class. No matter who references whom, since we encounter it, we will become our own things. If we really can't all become our own things, at least

We need to understand its thought.

Let's first take a look at the scale gesture detector that comes with Android:

Scalegestuedetector and gesturedetector are similar in structure, but scalegestuedetector can only be used to monitor scaling gestures, and gesturedetector monitors more gestures, which we mentioned in the previous section.

Some usages and APIs of scalegestuedetector. Small partners can check the official website documents by themselves: https://developer.android.google.cn/reference/android/view/ScaleGestureDetector.html

How do we use it (I take the last demo in the first section as an example)?

First create a scalegestuuredetector object:

Then pass a callback interface called scalelistener to it. There are three callback methods in scalelistener:

You should understand it. It is called back when onscale is zoomed out, when onscalebegin is zoomed in, and after onscaleend is zoomed out.

Finally, in the ontouchevent method of view, give the event to the scalegestuedetector object:

At the end of the section, I wrote a small demo to realize the displacement of the picture. Let's continue to add the scaling of the picture:

Embarrassed, the simulator is not very good for the recording of two finger scaling, so the effect partner gets the code and runs it by himself ~!!!

Let's talk about the source code of scalegestuuredetector: we know that the core code of scalegestuuredetector is ontouchevent, so we click ontouchevent:

A pile of code, which is not good at mathematics, looks really difficult. It is probably to calculate an average value of the X axis according to the X coordinates of multiple touch points, and then the Y axis is the same, and then through math. Hypot (Spanx, spany); Calculate the oblique edge length, which is the distance between two points, and then save the current span and the moved span.

Finally, when we call getscalefactor to obtain the scaling scale, that is, use the current span / previous span:

This mathematical scum is really a hard wound ~ ~

With Android's own scalegestuuredetector as a reference, can we implement scalegestuuredetector ourselves?? Of course, the great God on GitHub has been realized, but if not, can you write it?

I'll write this first. It's not over yet.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>