Android gesture ImageView trilogy part I

I've been studying photoview and gestureimageview on GitHub these days. I found that they are very good. I've read the code for a long time, so I plan to summarize some of the things I've seen. There are still a lot of contents, but they are very valuable

First attach links to two open source projects:

GestureImageView: https://github.com/jasonpolites/gesture-imageview

PhotoView: https://github.com/chrisbanes/PhotoView

It's a little boring to say so. Let's take a look at the effect we want to achieve today:

When a finger presses the picture, the effect is drag. When two fingers press and hold the picture, the picture rotates when the finger rotates, and the picture zooms when the finger zooms. The effect picture is roughly (my simulator is not good at simulating rotation):

OK, let's implement this gesture to zoom the ImageView:

First, we create a class called matriximageview to inherit ImageView, and then override its construction method (I won't consider the case of direct new):

Then we need to define several current view states: mode_ None (initial state); MODE_ Drag (drag status); MODE_ Zoom (two finger zoom state)

We mainly use the method of ImageView to rotate, scale and displace ImageView:

The matrix class is used (for children's shoes that don't understand this class, check the data, ha ~), and then obtain the current gesture operation by listening to our ontouchevent method, and then operate the matrix accordingly to change the state of the picture.

The code is relatively short, and I annotate every line, so I directly give it to the code:

MatrixImageView.java:

Then add our layout file:

Last run code:

Well, although we have realized the basic function of our gesture ImageView, if we want to deal with multi-point (> two fingers) touch and some complex operations, the code written in our ontouchevent may be more than that (it is still a little complex and too many factors are considered), But if the processing of an event can be separated into many branches, will it be so complex?? If our code can be like this, don't you think it's great?

There are only a few lines of code left in the ontouchevent of our ImageView. After each detector processes the event, we only need to get the processed value:

Does it feel good? Yes, I also accidentally saw this open source project. First attach the GitHub link of the framework: https://github.com/Almeros/android-gesture-detectors

In the next section, we will learn more about the detector and the gesture processing tool class gesturedetector provided by the system. Please continue to pay attention to it if you are interested.

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