The article will use some network resources, first of all the original author's link is attached.
However, there is a large number of their own thinking about this part of the content.
------------------------------------------------------------------------------------Write by Mcdullsin
Attach the original author's link to the search engine to evaluate the original author's articles to provide their own ability to help, but also to share their own technology of the predecessors of a kind of gratitude.
1. The distribution and consumption mechanism of touch events under Android programming
2, the Android principle of the secret series of view, ViewGroup
3. Touch Events in Android
First, View,viewgroup (the following red name components must be skilled)
As Friends of Android app Development know, Android UI interface is composed of view and ViewGroup and its derived classes. Where view is the base class for all UI components, ViewGroup is the container that holds the components, which itself is derived from view. The general structure of the Androidui interface can be found in the following:
It can be seen that the viewgroup as a container may contain a view as a leaf node, or it can be included as a lower-level sub-viewgroup, and the child ViewGroup can contain the view and viewgroup of the next layer of leaf nodes. In fact, this flexible view hierarchy can create a very complex UI layout, where developers can design and develop very sophisticated UI interfaces.
In general, the UI interface for developing Android applications is not directly practical for view and ViewGroup, but instead uses the derived classes of these two large base classes.
The immediate subclasses derived from view are: Analogclock,imageview,keyboardview, progressbar,surfaceview,textview,viewgroup,viewstub
The indirect subclasses derived from view are: Abslistview,absseekbar, Absspinner, absolutelayout, adapterview<t extends Adapter> Adapterviewanimator, Adapterviewflipper, Appwidgethostview, Autocompletetextview,button,calendarview, CheckBox, Checkedtextview, chronometer, Compoundbutton,
The direct subclasses derived from ViewGroup are: Absolutelayout,adapterview<t extends Adapter>,fragmentbreadcrumbs,framelayout, Linearlayout,relativelayout,slidingdrawer
The indirect subclasses derived from ViewGroup are: Abslistview,absspinner, Adapterviewanimator, Adapterviewflipper, Appwidgethostview, CalendarView, DatePicker, Dialerfilter, Expandablelistview, Gallery, Gestureoverlayview,gridview, Horizontalscrollview, Imageswitcher,listview,
The classes marked red in the above view, the direct subclass of ViewGroup, and the indirect subclass are the classes that we use more frequently in our re-application development, requiring beginners to be familiar with and mastering the details of the API and usage, as described in the SDK. It is noted here that ImageView is a common class for layouts with graphic effects, and Surfaceview is a very important class for game development compared to General view (further analysis of the theory), and Absolutelayout, Framelayout,linearlayout, relativelayout the direct subclasses of these viewgroup are the most basic layout elements in the Android UI layout.
It is worth mentioning that all of the above base classes, derived classes are the Android framework layer integration of standard system classes, developers in the application development can directly reference these system classes and their APIs in the SDK. But in fact, in many scenarios of UI development, the direct use of these system classes does not meet the needs of application development. For example, we want to use ImageView to load a picture by default, but want to click on the view when the view transform a variety of image processing effect, this time directly using ImageView is not possible, at this time we can overload ImageView, The new derived child controls are overloaded with OnDraw and other methods to achieve our custom effects. This subclass method derived from the system class is commonly referred to as a custom control. Custom controls can be laid out and instantiated in XML and in our Java files just like Standard view controls, but must indicate their full package name and class name at layout time. In fact, the use of custom controls is an essential basic usage of our Android development and a basic skill that must be mastered.
Ii. methods and controls that can respond to touch events.
The methods associated with Touch events in Android include: dispatchtouchevent (motionevent ev), onintercepttouchevent (motionevent ev), ontouchevent ( Motionevent ev); the controls that respond to these methods include: ViewGroup, View, Activity. The corresponding relationship between the method and the control is shown in the following table:
| Touch Event-related methods |
Method functions |
ViewGroup |
View |
Activity |
| public boolean dispatchtouchevent (motionevent ev) |
Event distribution |
Yes |
Yes |
Yes |
| public boolean onintercepttouchevent (motionevent ev) |
Event interception |
Yes |
Yes |
No |
| public boolean ontouchevent (motionevent ev) |
Incident response |
Yes |
Yes |
Yes |
Event Distribution: Public boolean dispatchtouchevent (motionevent ev)
When the Touch event occurs, the Dispatchtouchevent (motionevent ev) method of the Activity passes the event to the outermost layer by tunneling (passing from the root element down to the topmost child element or in an intermediate element because a condition stops passing). View's dispatchtouchevent (motionevent ev) method, and the event is distributed by the view's dispatchtouchevent (Motionevent ev) method. The event distribution logic for Dispatchtouchevent is as follows:
- If return true, the event is distributed to the current View and consumed by the Dispatchtouchevent method, while the event stops passing down;
- If return is false, event distribution is divided into two scenarios:
- If the current View gets events directly from the activity, the event is returned to the activity's ontouchevent for consumption;
- If the current view gets an event from the outer parent control, the event is returned to the parent view's ontouchevent for consumption.
- If the system default super.dispatchtouchevent (EV) is returned, the event is automatically distributed to the current View's Onintercepttouchevent method.
Event interception: Public boolean onintercepttouchevent (motionevent ev)
When the dispatchtouchevent (motionevent ev) method of the outer view returns to the system's default super.dispatchtouchevent (EV) case, the event is automatically distributed to the current view's Onintercepttouchevent method. The Onintercepttouchevent event interception logic is as follows:
- If Onintercepttouchevent returns True, the event is intercepted and the intercepted event is referred to the current View's ontouchevent for processing;
- If Onintercepttouchevent returns FALSE, the event is released, the event on the current view is passed to the child view, and then the dispatchtouchevent of the view begins the distribution of the event;
- If Onintercepttouchevent returns super.onintercepttouchevent (EV), the event is intercepted by default and the intercepted event is referred to the current View's ontouchevent for processing.
Event Response: Public boolean ontouchevent (motionevent ev)
The Dispatchtouchevent returns super.dispatchtouchevent (EV) and onintercepttouchevent returns TRUE or returns Super.onintercepttouchevent ( EV), the ontouchevent will be called. The event response logic for Ontouchevent is as follows:
- If the event is passed to the current view's Ontouchevent method, and the method returns False, the event is passed up from the current view and is received by the ontouchevent of the upper view, if passed to the ontouchevent above Also returns false, the event "disappears" and the next event is not received.
- If True is returned, the event is received and consumed.
- If the return super.ontouchevent (EV) default handles the logic of the event and returns false, the same.
Here, the three methods associated with the Touch event have been analyzed. The following content validates the handling logic of the events in the three above methods through a variety of different test cases.
Three, Summary: in a word to summarize the words are: tunnel-type distribution, bubbling processing. When distributing, from the top down, when processing from the bottom can accept the touchevent of the event's child view to the next can receive TouchEvent's parent view. First, this page has two controls, parent view and child view. The touchevent processing of both the parent view and the child view is return false. Both can respond to touchevent, here is to say that response to the incident response is not necessarily the only one. The event response order is displayed through toast, as in the following two graphs: respond to child first, then respond father.
The above about Dispathtouchevent, Onintercepttouchevent said a lot, but in general, in their own custom view when the two events are rarely used, but generally to the ontouchevent processing, But the individual thinks that the complete system is still set up, if use can immediately find out the framework of the overall understanding.
And this is just a touch event, and a gesture event. But for general development, the touch event is sufficient.
The next article writes the custom component.
Android view, ViewGroup event response interception processing mechanism