Android implements the method of controlling the size and scaling of pictures through gestures

This paper describes the method of controlling the size and zoom of pictures through gestures in Android. Share with you for your reference, as follows:

The program realizes the scaling of the picture through gestures. When waving the picture from left to right, the picture is enlarged, and when waving the picture from right to left, the picture is reduced. The faster the waving speed is, the larger the scaling ratio is. The program idea is as follows: define an ImageView in the interface to display the picture, use a gesturedetector to detect the user's gesture, and scale the picture according to the horizontal speed of the user's gesture.

Before introducing this example, let's introduce the general framework for handling gesture touch events in Android.

1、 Add a statement to implement ongesturelistener gesture listener. The code is as follows:

2、 Define a global instance of gesture listener and initialize it in oncreate function. The code is as follows:

3、 Rewrite the ontouchevent function and submit the touch event of this activity to the gesturedetector for processing. The code is as follows:

4、 Rewrite the function of the gesture you need to monitor. By default, it includes the following gestures:

Boolean nondown (motionevent E): press. Boolean nonfling (motionevent E1, motionevent E2, float velocityx, float velocityy): drag and slide. Abstract void onlongpress (motionevent E): press and hold. Boolean nonscroll (motionevent E1, float velocityy): scroll. Void onshowpress (motionevent E): pressed and not moved or released. Boolean nonsingletapup (motionevent E): tap.

These gestures are provided by the system by default. According to the description, you may not be clear about these gestures. The best way is that you can implement a simple program and understand it by experiment. Of course, in addition to these default gestures, you can also add gestures by yourself, which will not be repeated due to limited space.

Next, an example of image zooming through sliding is given. Compared with the basic framework given above, onfling function is rewritten, in which how to deal with sliding events is defined.

First, define some global objects other than the gesture listener, and initialize them in the oncreate function:

1、 Touch time binding gesture listener, which is the same as the previous one, and no code will be posted.

2、 Override onfling function:

The layout file only adds an ImageView control. Please draw it yourself. There is no screenshot here, because the screenshot can't see the effect. Let's try it ourselves. Well, so far, we have realized the picture zoom through gesture sliding. The above content is learned from the crazy Android book.

More readers interested in Android related content can view the special topics of this site: summary of Android graphics and image processing skills, summary of Android view skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.) Usage Summary of Android basic components, Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

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