Android touch event explanation and sample code

Event triggered by touch

Android events: onclick, onscroll, onfling, etc. are composed of many touch. The first state of touch must be action_ Down indicates that the screen is pressed. After that, the touch will have subsequent events, which may be:

ACTION_ Move / / indicates a move gesture

ACTION_ Up / / indicates leaving the screen

ACTION_ Cancel / / the cancel gesture is not generated by the user, but by the program

An action_ Down, n actions_ Move, 1 action_ Up constitutes many events in Android.

For the control of ViewGroup class, a very important method is onintercepttouchevent(), which is used to process events and change the transmission direction of events. Its return value is a Boolean value, which determines whether the touch event should continue to be transmitted to its child views. This method is passed from the parent view to the child view.

The method ontouchevent() is used to receive and process events. Its return value is also a Boolean value, which determines whether events and subsequent events continue to pass up. This method is passed from the child view to the parent view.

The transfer mechanism of touch event between onintercepttouchevent() and ontouchevent and each childview completely depends on the return values of onintercepttouchevent() and ontouchevent(). A return value of true indicates that the event has been correctly received and processed. A return value of false indicates that the event has not been processed and will continue to be delivered.

ACTION_ The down event will be passed to the onintercepttouchevent of a ViewGroup class. If false is returned, the down event will continue to be passed to the onintercepttouchevent of the child ViewGroup class. If the child view is not a control of the ViewGroup class, it will be passed to its ontouchevent.

If onintercepttouchevent returns true, the down event will be passed to its ontouchevent and will not continue to be passed, and subsequent events will also be passed to its ontouchevent.

If the ontouchevent of a view returns false, the down event continues to be passed to the ontouchevent of its parent ViewGroup class; If true is returned, subsequent events will be passed directly to its ontouchevent to continue processing. (subsequent events will only be passed to ontouchevent that returns true for the necessary event action_down)

To sum up, onintercepttouchevent can accept all touch events, but ontouchevent is not necessarily.

For the events of Android custom controls, Android provides a class of gesturedetector and gesturedetector Ongesturelistener interface to judge what actions the user makes on the interface.

There are two classes in Android

  android. view. GestureDetector   android. view. GestureDetector. Simpleongesturelistener (in addition, android.widget.gallery seems to be a better onggesturelistener)

1) create a new class that inherits simpleongesturelistener and hahagesturedetectorlistener

The following event events can be implemented. Boolean ondoubletap (motionevent E) explanation: the second touch down of double clicking triggers Boolean ondoubletap (motionevent E) explanation: the second touch down and up of double clicking trigger, which can be distinguished by e.getaction(). Boolean ondown (motionevent E) explanation: Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) is triggered when touching down. Explanation: it is triggered when touching up after sliding a little distance. Void onlongpress (motionevent E) explanation: the Boolean onscroll (motionevent E1, float distancex, float distancey) is triggered when touch does not move and touch down. Explanation: it is triggered when touch slides. Void onshowpress (motionevent E) explanation: triggered when touch does not slide (compared with ondown and onlongpress, onshowpress and onlongpress are triggered immediately when you touch down. However, onshowpress and onlongpress are triggered when you touch down. Therefore, you will not slide after touchdown. Ondown - > onshowpress - > onlongpress are triggered in this order.) Boolean onsingletapconfirmed (motionevent E) Boolean onsingletapup (motionevent E) explanation: the above two functions are triggered when touchup is triggered after touchdown without onscroll and onlongpress. Click the very fast (no sliding) touchup: ondown - > onsingletapup - > onsingletapconfirmed. Click the slightly slower (no sliding) touchup Touchup:   onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

2) create a new gesturedetector object in view.

Constructor

  gestureDetector = new GestureDetector(newHahaGestureDetectorListener());

Then use the following in the ontouchevent of view to write your own code in the event just 1).

Through this article, I hope to help develop Android applications and use touch events. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>