An explanation of the Android event process
There are many blogs on the web about the Android event distribution mechanism and processing mechanism, but seen times, always feel a little bit vaguely, so specifically to take out a day of events to pro-test, to like me, the vast number of entry programmers in detail about the story behind the Android event, say not much, on dry.
The entire Android event process is mainly involved in dispatchtouchevent (), Onintercepttouchevent (),
Ontouchevent () These three methods, the following table illustrates the functions and distribution scenarios of these three methods:
Table 1:
| Method name |
Explanation of work Solution |
Activity |
ViewGroup |
View |
| Dispatchtouchevent () |
Event distribution |
YES |
YES |
YES |
| Onintercepttouchevent () |
Event interception |
NO |
YES |
NO |
| Ontouchevent () |
Event handling |
YES |
YES |
YES |
First analyze the touch event: When all touch events occur, the dispatchtouchevent () Party of the current activity is called
method to distribute the event, the activity's Dispatchtouchevent () method will eventually call the Phonewindow class
Superdispatchtouchevent method, the final logic is that the activity calls the ViewGroup class.
Dispatchtouchevent () for tunnel distribution events (distributed externally by layout elements), such as the distribution process in this case
For Touchtraining, Touchviewgroup, TouchView, it's important to note that when you're in Activit
Dispatchtouchevent () directly returns the specific Boolean value (either True or false), the touch event is directly
Consumption in this method, the event distribution process will not be carried out again, so must be in the activity of the Dispatchtouchevrent
Returns Super.dispatchtouchevent () for the event distribution process. The following example illustrates the analysis:
First in defining your own view and viewgroup, rewrite table 1 the event flow methods that they each support, and I'll customize the viewgroup here
Inherit the LinearLayout (as long as you inherit the same viewgroup), and then add the custom in the activity's layout file, respectively
The event flow method that it supports in the activity.
Activity_touchtrain.xml
650) this.width=650; "src="%5c "title=" \ "qq20151023142613.png\" "alt=" \ "wkiol1yp00zxdmxqaaekgdwormw917.jpg\"/"/ >
TouchView (Custom view Class)
@Overridepublic boolean dispatchtouchevent (Motionevent event) {LOG.E (TAG, "Dispatchtouchevent distribution Event" + Toucheventutil.gettouchaction (Event.getaction ())); Return Super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (motionevent ev) {LOG.E (TAG, "ontouchevent handling Event" + toucheventutil.gettouchaction ( Ev.getaction ())); return super.ontouchevent (EV);}
Touchviewgroup (Custom ViewGroup Class)
@Overridepublic boolean dispatchtouchevent (motionevent ev) {LOG.E (TAG, "Dispatchtouchevent distribution event" + Toucheventutil.gett Ouchaction (Ev.getaction ())); return super.dispatchtouchevent (EV);} @Overridepublic boolean onintercepttouchevent (motionevent ev) {LOG.E (TAG, "onintercepttouchevent intercept event" + Toucheventutil.gettouchaction (Ev.getaction ())); return super.onintercepttouchevent (EV);} @Overridepublic boolean ontouchevent (motionevent ev) {LOG.E (TAG, "ontouchevent handling Event" + toucheventutil.gettouchaction ( Ev.getaction ())); return super.ontouchevent (EV);}
Touchtraining (Activity)
@Overridepublic boolean dispatchtouchevent (motionevent ev) {LOG.E (TAG, "Dispatchtouchevent distribution Event" + Toucheventutil.gettouchaction (Ev.getaction ())); return super.dispatchtouchevent (EV);} @Overridepublic boolean ontouchevent (motionevent ev) {LOG.E (TAG, "ontouchevent handling Event" + toucheventutil.gettouchaction ( Ev.getaction ())); return super.ontouchevent (EV);}
Toucheventutils (tool class, getting the current event type)
Public static string gettouchaction (Int actionid) { string actionName = "unknow:id=" + actionId; switch (ActionId) { case MotionEvent.ACTION_DOWN: actionName = "Action_down"; break; case motionevent.action_move: actionname = "Action_move"; break; case motionevent.action_up: actionName = "Action_up"; break; case motionevent.action_cancel: actionName = "Action_cancel" ; break; case MotionEvent.ACTION_OUTSIDE: actionName = "Action_outside"; break; } return actionname;}
Case analysis
Scenario 1
Conditions
|
| Control Name |
Dispatchtouchevent return value |
Onintercepttouchevent return value |
Ontouchevent return value |
Touchtraining
|
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
| Touchviewgroup |
False |
Super.onintercepttouchevent (EV) |
Super.ontouchevent (EV) |
| TouchView |
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
Operation Result:
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_down
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_up
Result Analysis: The touch event begins, calling Touchtraining's dispatchtouchevent to distribute the event Touchviewgroup
The Dispatchtouchevent,touchviewgroup Dispatchtouchevent returns false, the event stops passing down, the same
When the event is not consumed, it is returned to Touchtraining because the event is from Touchtraining (Activity)
Ontouchevent for consumption.
Scenario 2
Conditions
|
| Control Name |
Dispatchtouchevent return value |
Onintercepttouchevent return value |
Ontouchevent return value |
Touchtraining
|
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
| Touchviewgroup |
True |
Super.onintercepttouchevent (EV) |
Super.ontouchevent (EV) |
| TouchView |
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
Operation Result:
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_up
Results analysis: The touch event was continuously distributed to Touchviewgroup by Touchtraining's dispatchtouchevent,
Touchviewgroup's dispatchtouchevent return True,touchviewgroup in Dispatchtouchevent
Fees from Touchtraining's dispatchtouchevent distribution of events.
Scenario 3
Conditions
|
| Control Name |
Dispatchtouchevent return value |
Onintercepttouchevent return value |
Ontouchevent return value |
Touchtraining
|
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
| Touchviewgroup |
Super.onintercepttouchevent (EV) |
True |
Super.ontouchevent (EV) |
| TouchView |
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
Operation Result:
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchviewgroup:onintercepttouchevent Intercept Event Action_down
Com.training.cj.mytraining e/touchviewgroup:ontouchevent Handling Event Action_down
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_down
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_up
Result Analysis: Touch event occurs, call Touchtraining dispatchtouchevent distribution event to Touchviewgroup
The dispatchtouchevent of the Dispatchtouchevent,touchviewgroup return super.onintercepttouchevent (EV) into
The row event is distributed, and the event is passed down to Touchviewgroup's Onintercepttouchevent,touchviewgroup onintercepttouchevent returns True, The event was intercepted and passed on to Touchviewgroup's ontouchevent for consumption, Touchviewgroup's ontouchevent returned to Super.dispatchtouchevent (EV), The ontouchevent that the touch event was not consumed and returned to the parent control was consumed because Touchviewgroup's touch event came from Touchtraining, so the last Consumed by the ontouchevent of Touchtraining.
Scenario 4
condition |
| control name |
dispatchtouchevent return value |
onintercepttouchevent return value |
ontouchevent return value |
touchtraining |
super.dispatchtouchevent (EV) |
---- |
super.ontouchevent (EV) |
| Tou Chviewgroup |
super.onintercepttouchevent (EV) |
false |
super.ontouchevent (EV) |
| TouchView |
super.dispatchtouchevent (EV) /td> |
---- |
super.ontouchevent (EV) |
Operation Result:
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchviewgroup:onintercepttouchevent Intercept Event Action_down
Com.training.cj.mytraining E/touchview:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchview:ontouchevent Handling Event Action_down
Com.training.cj.mytraining e/touchviewgroup:ontouchevent Handling Event Action_down
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_down
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining e/touchtraining:ontouchevent Handling Event Action_up
Result Analysis: Touch event occurred, call touchtraining dispatchtouchevent distribution event to Touchviewgroup Dispatchtouchevent, Touchviewgroup's dispatchtouchevent returns to Super.onintercepttouchevent (EV), Continue distributing down passing events to Touchviewgroup's Onintercepttouchevent,touchviewgroup Onintercepttouchevent returns false, Continue distributing the Dispatchtouchevent return super.dispatchtouchevent (EV) of the Dispatchtouchevent,touchview that passed the event down to Touchview, Continuing to distribute the Ontouchevent return super.ontouchevent (EV) of the Ontouchevent,touchview that passed the event down to Touchview, the event is not consumed, Returned to the higher Touchviewgroup ontouchevent for consumption, touchviewgroup
Ontouchevent returns to Super.ontouchevent (EV) and continues to return to the ontouchevent of the upper touchtraining for consumption.
Scenario 5:
Conditions
|
| Control Name |
Dispatchtouchevent return value |
Onintercepttouchevent return value |
Ontouchevent return value |
Touchtraining
|
Super.dispatchtouchevent (EV) |
---- |
Super.ontouchevent (EV) |
| Touchviewgroup |
Super.onintercepttouchevent (EV) |
False |
Super.ontouchevent (EV) |
| TouchView |
True |
---- |
Super.ontouchevent (EV) |
Operation Result:
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchviewgroup:onintercepttouchevent Intercept Event Action_down
Com.training.cj.mytraining E/touchview:dispatchtouchevent Distributing Events Action_down
Com.training.cj.mytraining e/touchview:ontouchevent Handling Event Action_down
Com.training.cj.mytraining E/touchtraining:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining E/touchviewgroup:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining e/touchviewgroup:onintercepttouchevent Intercept Event action_up
Com.training.cj.mytraining E/touchview:dispatchtouchevent Distributing Events Action_up
Com.training.cj.mytraining e/touchview:ontouchevent Handling Event Action_up
Result Analysis: Touch event occurred, call touchtraining dispatchtouchevent distribution event to Touchviewgroup Dispatchtouchevent, The method returns Super.dispatchtouchevent (EV), continuing to distribute the event to Touchviewgroup's Onintercepttouchevent, which returns false, Continue distributing events to Touchview Dispatchtouchevent, the method returns Super.dispatchtouchevent (EV), continuing to distribute events to ontouchevent, because Ontouchevent returns true , which indicates a consumption event, and the touch event terminates.
Well, the touch event analysis for this issue is over here, and there's a little detail to note that the Ontouchevent method in view and view group is returned by default False,view The onintercepttouchevent in group also returns false by default. So the above 5 scenarios describe all of the touch event delivery possibilities. Understanding the distribution and consumption mechanisms of the touch event is better for our custom controls, although we should try not to rewrite the Dispatchtouchevent method when customizing the control.
This post reference blog: http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html
This article is from the "Programmer's Life" blog, be sure to keep this source http://geektraining.blog.51cto.com/10814565/1706173
An explanation of the Android event process