Android realizes image zooming and dragging
Today, let's write an ImageView with zoom effect. Many people on the Internet have talked about this. But many people use library files directly,
So what we do today is to write a drag and zoom ImageView directly from the code. See the effect picture for details
That's a simple analysis. What does it take to zoom and drag pictures on your phone? Fingers, right
So what events on the control are related to the mobile phone. View. Ontouchlistener, right.
OK, first create a new class ・ public class basedragzoomimageview extensions ImageView implements view OnTouchListener ・・・
Yes, inherit ImageView and add view Ontouchlistener event
Then let's look at the constructor
Yes, in the constructor, we set setontouchlistener. So how to do this setontouchlistener. Let's first analyze motionevent in ontouch. We all know that there are many finger operations, so andorid naturally divides this situation into many cases- MotionEvent. ACTION_ Down finger press screen - motionevent ACTION_ Move finger moves on screen - motionevent ACTION_ Up finger off screen - motionevent ACTION_ POINTER_ Up when the contact leaves the screen, but there is still a contact (finger) - motionevent on the screen ACTION_ POINTER_ Down when there are already contacts (fingers) on the screen, another contact presses down the screen
Obviously, these seem to be available.
OK, it's over. That's all. Let's take a look at the whole class
Then look at the layout file
It's that simple.
Code location: GitHub
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.