Detailed explanation of Android gesture operation programming

Gesture operation offers a different experience in the process of using smart devices. Android development is bound to carry out gesture operation programming. So what is its principle? How do we program gesture manipulation?

Principle of gesture operation

First, in the Android system, each gesture interaction will be executed in the following order.

1. Touch the touch screen and trigger a motionevent event.

2. The event is monitored by ontouchlistener and the motionevent object is obtained in its ontouch () method.

3. Forward the secondary motionevent object to the ongesturelistener through the gesturedetector.

4. Ongesturelistener obtains the object, listens to the information encapsulated by the object and makes appropriate feedback.

This order can be said to be the principle of gesture operation.

Gesture operation classes and interfaces

Let's take a look at motionevent, gesturedetector and onggesturelistener.

Motionevent: this class is used to encapsulate action events such as gestures, touch pens, trackballs, etc. It encapsulates two important attributes X and y, which are used to record the coordinates of the horizontal axis and the vertical axis respectively.

Gesturedetector: recognize various gestures.

Ongesturelistener: This is a gesture interaction monitoring interface, which provides multiple abstract methods and calls the corresponding methods according to the gesture recognition results of gesturedetector.

Gesture operation example

Next, I will demonstrate the implementation of gesture interaction through a code example of switching beauty pictures, so that everyone can have a deeper understanding and memory of the above execution sequence and the distinction of each gesture action.

First, provide a layout file with only ImageView -- main. XML.

XML / HTML code

Then, complete our activity. Because we want to monitor the touch event and gesture time of the touch screen, the activity must implement two interfaces, ontouchlistener and ongesturelistener, and rewrite the methods therein. The specific codes are as follows:

Java code

Meaning of gesture operation methods

At the beginning of learning Android, I felt that Google's documents were not very good. When studying gestures, I felt that Google's documents were too poor. Many constants, properties and methods don't even have a description. No description is enough, but there are so many gestures in ongesturelistener, and it doesn't have an introduction. Who can understand the relationship and difference between onlongpress and onshowpress, onscroll and onfling before continuous attempts? Google really needs a major operation on documents. But fortunately, after my repeated attempts. These gestures are defined from a personal perspective.

In addition to these definitions, I have also summarized some experience that can be regarded as experience. I'll share it with you here.

Any gesture action will perform an ondown action first.

The above is the data sorting of Android gesture operation. We will continue to supplement relevant knowledge in the future. Thank you for your support to this site!

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