Android custom gesturedetector implements gesture ImageView

Stop talking nonsense and enter our topic today.

Paste the address of the previous content first:

Android gesture ImageView Trilogy (I)

Android gesture ImageView Trilogy (II)

Android gesture ImageView Trilogy (III)

We talked about the tool class scalegestuuredetector earlier. I was wondering why we created a scalegestuuredetector and did not rotate, move and do it? All right ~! Google definitely wants to leave some space for developers.

Following the example of scalegestuuredetector, we define a tool class called movegesturedetector (specifically used to detect sliding gestures). Before defining movegesturedetector, we also need to consider the following rotategestuuredetector and so on.. so we define a tool class called basegesturedetector to extract some public methods:

Then we define a class called movegesturedetector to inherit basegesturedetector, and then event two abstract methods:

How do we notify the caller if an event is detected? Yes, we need callbacks. Let's see how the callback interface of scalegestuuredetector is defined:

It defines an interface, an onscale gesturelistener, and a class, simpleonscalegestuurelistener. Simpleonscalegestuurelistener implements onscale gesturelistener, so the interface of movegesturedetector can be defined as follows:

All right! The frames are all set up. When we use them, we can use them like this:

1. Create a movegesturedetector

2. Send the event to movegesturedetector

3. Get callback value

What about? Is it the same as scalegestuuredetector? It's clear. The frame is set up. Let's implement its logic (that is, implement the handlestartprogressevent and handleinprogressevent methods):

Each line has comments, so I'll go straight to the code

All right!! After writing, let's use it:

All right ~!! You can play with just a few lines of code. I won't attach the effect picture. Let's run it by ourselves. Then we have realized the movegesturedetector. Presumably, the rotategestuuredetector will be realized soon, ha ha ~! I'll paste the code written by the foreign god directly:

Finally, we give you the code of scaling ImageView with a gesture of scaledetector, movedetector and rotatedetector:

OK ~ ~ ~ you can also download the code of this framework to study. I just share my learning experience with you. https://github.com/Almeros/android-gesture-detectors

Uh huh! Having said so much, let's finally see how the legendary photoview is implemented.

GitHub link of photoview: https://github.com/chrisbanes/PhotoViewary/

After reading our previous content, you may not be so confused if you go to photoview. Let's uncover its mystery:

First of all, the usage of photoview is very simple. Just use it like ImageView:

All right!! Now you can zoom, rotate and move the picture ~ isn't it great?

But note:

The zoom type of photoview is not supported, or you will directly report an error and exit:

android:scaleType="matrix"

Let's take a look at its source code:

It can be seen that there are not many lines of code, only more than 200 lines (ha ha!! there are less than 100 lines of matriximageview implemented by ourselves!! kidding, ha ha, the matriximageview we write is far less than that in photoview. The main processing is in the photoviewattacher class:

PhotoViewAttacher.java:

There is too much code. Let's look at its construction method

As you can see, it also creates an mscaledragdetector and an mgesturedetector to monitor gesture changes. Where is the event processing?

We also found a line of code in the construction method to set touch listening for the current ImageView:

imageView.setOnTouchListener(this);

The little buddy guessed it. Now it's time to give the event to the event listener:

Finally, after the event is processed, there will be a series of callbacks. After the callback, the matrix object should be reset for ImageView, such as zoom:

There are a lot of other similar ha ~ ~ ~ codes (considering a lot of situations). It is conceivable that it is not so simple to write a custom component, but come on!

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
分享
二维码
< <上一篇
下一篇>>