In depth understanding of Android gesture recognition

For the touch screen, its native messages are nothing more than press, lift and move. We only need to simply reload ontouch or set the touch listener setontouchlistener to process them. However, in order to improve the user experience of our app, sometimes we need to recognize users' gestures. The gesture recognition tool gesturedetector provided by Android can be of great help.

Basics

The working principle of gesturedetector is that when we receive a user touch message, we give the message to gesturedetector for processing. We set a listener to obtain the gesture processed by gesturedetector.

Gesturedetector provides two listener interfaces. Ongesturelistener handles click class messages and ondoubletaplistener handles double-click class messages.

The interfaces of ongesturelistener are as follows:

Sometimes we don't need to handle all the above gestures. For convenience, Android provides another class simpleongesturelistener, which implements the above interface. We just need to inherit simpleongesturelistener and overload the gestures of interest.

application

Step 1: create gesture listening object

Step 2: set gesture recognition

We can set gesture recognition in activity:

You can also set gesture recognition in the custom view:

trap

For custom views, there are two pitfalls in using gesture recognition, which may waste a lot of your time.

1: The view must set longclickable to true, otherwise gesture recognition will not work correctly and will only return down, show and long gestures

2: we must call gesture recognition in the onTouchListener of View instead of overloading onTouchEvent like Activity. Otherwise, gesture recognition can not work correctly.

test result

The following is the gesture sequence returned by various operations. The value 0 indicates that the touch screen is pressed and 1 indicates that it is lifted

Click: down 0, single up 1, single conf 0

Short press: down 0, show 0, single up 1

Long press: down 0, long 0

Double click: down 0, double 0, double event 0, down 0, double event 1

Scroll: down 0, (show 0), scroll 2

Sliding: down 0, scrool 2..., flying 1

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