Event interception mechanism analysis (Android Elite biography)

Source: Internet
Author: User

The content is Bo master according to the book knocked out, Bo main code word very hard, reproduced please indicate the source, after the subsequent content will code out.

When the Android system captures the user's various input events, how exactly does it pass to the control that really needs the event? Android provides us with a complete set of event delivery and processing mechanisms to help developers achieve accurate event allocation and processing.
To understand the interception mechanism of touch events, first understand what is a touch event? As the name implies, touch events are events that occur when a touch screen is captured. When a button is clicked, it usually produces two or three events-the button is pressed, this is the event one; if you accidentally swipe a bit, this is event two; When the hand is lifted, this is the event three. Android encapsulates a class--motionevent for touch events, and if you override the Ontouchevent () method, you'll find that the parameter to the method is such a motionevent. In fact, as long as the override touch-related methods, parameters generally contain motionevent, which can be seen its importance.
In the motionevent, a lot of good things, such as the coordinates of the touch point, can be Event.getx () method and the Event.getrawx () method to remove the coordinate points, such as the click of the event type, you can use different action ( such as Motionevent.action_down, Motionevent.action_move) to differentiate and implement different logic.
In this case, touching the event is still relatively simple, in fact, it is an action type plus coordinates. But we know that the view structure of Android is a tree structure, that is to say, view can be placed inside the viewgroup, through different combinations to achieve different styles. So the question comes, the view is placed in a viewgroup inside, this viewgroup again in another viewgroup inside, even possibly still may continue nesting, layers of layer of stacking up. But our touch event is one. The same event, both the child view and the parent viewgroup, may want to be processed. Therefore, this creates the "event interception" this "domineering" salutation.
Of course, event interception can be complex or simple. But beginners often "card" here do not know how to continue, so we do not want to pass too much source code to let everyone at a loss. We use the most intuitive log information, so that we first have a general understanding of the nature of event interception, and then everyone in the combination of source code learning, you can have the direction, purposeful to understand.
First, imagine a very common scenario in your life: Assume that your company has a general manager with the highest level; he has a minister below, and the lowest level is the one who is working, no level. Now the Board of directors to the general manager of a task, the general manager of the task assigned to the Minister, the minister has arranged the task to you. And when you finally finished the work, you put the task to the Minister, the Minister felt that the task was done well, so he signed his name to the general manager, the general manager looked also feel good, employment signed the name to the board. In this way, a task is completed successfully. If you can understand such a scenario very clearly, then for the event interception mechanism, you are more than 40% of the developers. Next, we'll go beyond the rest of the developers. In order to be able to easily understand the process of the entire event, we have designed such an instance as shown in.




A general manager--myviewgroupa, the outermost viewgroup (red).
A minister--myviewgroupb, the middle of viewgroup (green).
A working you--myview, at the bottom (blue).
The entire layout structure of this instance is shown.



The code is very simple, just rewrite a few methods of event interception and processing, and add some log to it.
For ViewGroup, rewrite the three methods shown below.

@Override Public Boolean dispatchtouchevent(Motionevent ev) {LOG.D ("BLANKJ","Viewgroupa dispatchtouchevent"+ ev.getaction ());return Super. dispatchtouchevent (EV);}@Override Public Boolean onintercepttouchevent(Motionevent ev) {LOG.D ("BLANKJ","Viewgroupa onintercepttouchevent"+ ev.getaction ());return Super. onintercepttouchevent (EV);}@Override Public Boolean ontouchevent(Motionevent event) {LOG.D ("BLANKJ","Viewgroupa ontouchevent"+ event.getaction ());return Super. Ontouchevent (event);}

For view, rewrite the two methods shown below.

 @Override  public  boolean   Ontouchevent  (Motionevent event) {log.d ( "blankj" ,     "View ontouchevent"  + event.getaction ()); return  super . Ontouchevent (event);  @Override  public  boolean  dispatchtouchevent  (Motionevent event) {LOG.D ( "blankj" ,  "View dispatchtouchevent"  +    Event.getaction ()); return  super . Dispatchtouchevent (event);}  

As you can see from the code above, the ViewGroup level is higher and a method--onintercepttouchevent () is more than view. This method can be guessed by name to be the core method of event interception. We don't change any of the return values, just click on view and see how log logs our actions and program responses. After clicking on the view, log is shown below.

D/blankj:viewgroupa dispatchTouchEvent0
D/blankj:viewgroupa onInterceptTouchEvent0
D/BLANKJ:VIEWGROUPB dispatchTouchEvent0
D/BLANKJ:VIEWGROUPB onInterceptTouchEvent0
D/blankj:view dispatchTouchEvent0
D/blankj:view onTouchEvent0
D/BLANKJ:VIEWGROUPB onTouchEvent0
D/blankj:viewgroupa onTouchEvent0

You can see, normally, the order in which time is passed is:
General Manager (Myviewgroupa) → minister (MYVIEWGROUPB) → you (View). When an event is passed, the Dispatchtouchevent () method is executed before the Onintercepttouchevent () method is executed.
The order in which the events are processed is:
You (View) → minister (MYVIEWGROUPB) → general Manager (Myviewgroupa). Event handling is the execution of the Ontouchevent () method.
The return value of the event pass is very easy to understand: true, intercept, do not continue, false, do not intercept, continue the process.
The return value of the event handler is similar: true, handled, not audited, false, for superiors.
In the initial case, the return value is false.
In order to facilitate understanding of the event interception process, we only care about onintercepttouchevent () in event delivery, while the Dispatchtouchevent () method is the first step in event distribution, but in general, we are not going to rewrite this method , so for the time being no matter how. The entire event process can be organized into a diagram as shown.




I believe that as long as you think of MyView as their own, you can fully understand the event distribution, interception, processing of the entire process.
Let's change it a little bit, assuming the general manager (MYVIEWGROUPA) found the task too simple, feel that they are done, there is no need to find subordinates. Thus time is used by the general Manager (Myviewgroupa) to intercept the event using the Onintercepttouchevent () method, that is, to let Myviewgroupa's Onintercepttouchevent () method return True, Let's look at log again.

D/blankj:viewgroupa dispatchTouchEvent0
D/blankj:viewgroupa onInterceptTouchEvent0
D/blankj:viewgroupa onTouchEvent0

As we had imagined, the general Manager (Myviewgroupa) had done all the work, and there was no one behind it. Similarly, we let the Minister (MYVIEWGROUPB) also be a good person, that is, let the Minister (MYVIEWGROUPB) use the Onintercepttouchevent () method to return true, to intercept the event, log will make the following.

D/blankj:viewgroupa dispatchTouchEvent0
D/blankj:viewgroupa onInterceptTouchEvent0
D/BLANKJ:VIEWGROUPB dispatchTouchEvent0
D/BLANKJ:VIEWGROUPB onInterceptTouchEvent0
D/BLANKJ:VIEWGROUPB onTouchEvent0
D/blankj:viewgroupa onTouchEvent0

As you can see, this time the Minister (MYVIEWGROUPB) was a good man and you (MyView) didn't have to work.
In either case, it can be sorted into a diagram similar to the one shown.
The General Manager (Myviewgroupa) intercepts the event as shown.




The Minister (MYVIEWGROUPB) intercepts the incident as shown.



The distribution of the incident, interception, and now we should be more clear, let us look at the handling of the incident. First look at the bottom people-you (MyView). At the very beginning, when you finish the task, you will be reported to the superior, need the confirmation of the superior, so your event processing returns FALSE. Then you suddenly one day can not bear the boss's oppression, the strike is not done, then your task will not be done, you do not have to report on the machine, so the direct return to true. Now look at log again, as shown below.

D/blankj:viewgroupa dispatchTouchEvent0
D/blankj:viewgroupa onInterceptTouchEvent0
D/BLANKJ:VIEWGROUPB dispatchTouchEvent0
D/BLANKJ:VIEWGROUPB onInterceptTouchEvent0
D/blankj:view dispatchTouchEvent0
D/blankj:view onTouchEvent0

As you can see, event passing is the same as before, but event handling, to You (MyView) This ends, because you return true, indicating that you do not have to report to the superior. At this point, we will also tidy up the diagram, as shown below.




You (MyView) finally turned over to be the Lord and decided his own destiny. But if the minister (MYVIEWGROUPB) sees your report and feels too ashamed to show it to the manager, he secretly returns to true, and the whole event ends, that is, the Minister (MYVIEWGROUPB) returns his ontouchevent true, The log is shown below.

D/blankj:viewgroupa dispatchTouchEvent0
D/blankj:viewgroupa onInterceptTouchEvent0
D/BLANKJ:VIEWGROUPB dispatchTouchEvent0
D/BLANKJ:VIEWGROUPB onInterceptTouchEvent0
D/blankj:view dispatchTouchEvent0
D/blankj:view onTouchEvent0
D/BLANKJ:VIEWGROUPB onTouchEvent0

The diagram between them is as shown.




Through the analysis of the previous cases, I believe that we can easily understand the distribution of events, interception, processing of the event process. In the back of the study, combined with the source code, you will be more in-depth understanding, why the process will be like this? Beginners in learning, it is best to have a general understanding of the process, and then to contact the source code, so that it will not confused, touch the brain, thereby losing interest in learning.
Project Address →eventintercept

Analysis of the original address event interception mechanism (Android Elite biography)
My self-media blog blankj Small station (OJ, Leetcode, Android Development), welcome to stroll.

Event interception mechanism analysis (Android Elite biography)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.