Android graphic analysis of event interception mechanism

Source: Internet
Author: User

When the Android system captures the user's various input events, how exactly does it pass to the real control that needs the event? Android provides a complete set of event delivery, processing mechanism to help developers to complete the accurate event allocation and processing, here I do not analyze the source code, simple point, the graphical distribution process, easy to understand.

When we click on a button, it usually produces two or three events---pressed, slid (possibly none), lifted. Android encapsulates a class for touch events----motionevent, where if we rewrite a view of the Ontouchevent event the argument is a motionevent. Because Android's view structure is a tree structure, that is, view can be placed inside the viewgroup, with different combinations to achieve different styles. The view can be placed in a viewgroip, and this viewgroup is placed in another viewgroup, and may even continue nesting. Perhaps the same event, sub-view, and ViewGroup are likely to be processed, so the question of how to "distribute" and "intercept" arises.

Suppose there is a view, a viewgroupa, nested inside another VIEWGROUPB, and VIEWGROUPB has a view inside it. The overall layout structure is as follows:


The layout file is as follows:

< Relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context        = "Com.example.viewdispatch.MainActivity" > <com.example.views.myviewgroupa android:layout_width= "260DP"            android:layout_height= "360DP" android:background= "#f42c61" > <COM.EXAMPLE.VIEWS.MYVIEWGROUPB            Android:layout_width= "200DP" android:layout_height= "300DP" android:background= "#042c61" > <com.example.views.myview android:layout_width= "160DP" android:layout_height= " 260DP "android:background=" #cccccc "/> </com.example.views.MyViewGroupB> </com.exampl E.views.myviewgroupa></relativelayout> 

The overall activity consists of 3 custom view, the project structure is MyView, MYVIEWGROUPB, Myviewgroupa. So the main character of the overall touch event is view and ViewGroup, while the touch event associated with view has 2 dispatchtouchevent and ontouchevent The touch event associated with ViewGroup has 3 dispatchtouchevent,onintercepttouchevent,ontouchevent. So after we've defined the view and the ViewGroup, let's get them to implement the methods separately.

The code is as follows:

Myview.java

Import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.view.view;public class MyView extends View {private String Tag = "MyView";p ublic MyView (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);} Public MyView (context context, AttributeSet Attrs) {Super (context, attrs); LOG.D (Tag, "----->myview");} @Overridepublic boolean ontouchevent (Motionevent event) {LOG.E (Tag, "----->ontouchevent"); return Super.ontouchevent (event);} @Overridepublic boolean dispatchtouchevent (Motionevent event) {LOG.E (Tag, "----->dispatchtouchevent"); return Super.dispatchtouchevent (event);}}
Myviewgroupa.java
Import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.view.viewgroup;import Android.widget.linearlayout;public Class Myviewgroupa extends LinearLayout {private String Tag = "Myviewgroupa";p ublic Myviewgroupa (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);//TODO auto-generated constructor stub}public Myviewgroupa (Context context, AttributeSet attrs) {Super (context, attrs); LOG.D (Tag, "----->myviewgroupa");} @Overridepublic boolean dispatchtouchevent (motionevent ev) {LOG.E (Tag, "----->dispatchtouchevent"); return Super.dispatchtouchevent (EV);} @Overridepublic boolean onintercepttouchevent (motionevent ev) {LOG.E (Tag, "----->onintercepttouchevent"); return Super.onintercepttouchevent (EV);} @Overridepublic boolean ontouchevent (Motionevent event) {LOG.E (Tag, "----->ontouchevent"); return Super.ontouchevent (event);}}
MYVIEWGROUPB and a, they don't stick.

Where the return value of the above event is passed, if true, indicates that view (ViewGroup) is blocked, does not continue to distribute the event, if return False, indicates not to intercept, continue to distribute downward. The default call to the parent method, super.xxx, means no interception.

Let's start by verifying it.

To run our project, take a look at the loading of our 3 view:


Can be seen from the outside to the nega load.

OK, let's start the touch event, and now we'll click on the outermost pink Viewgroupa and watch the log output:


Then click on the VIEWGROUPB in the blue area and observe the log output:


Finally click MyView to view the output


As a result, we can roughly draw out such a diagram as shown in the process.


Now if we modify the Onintercepttouchevent () event in Myviewgroupa and change its return value to True, click on any layer of view, now let's look at the log output:


It can be seen that only a 3 events were processed, and the event was intercepted by a. The flowchart is as follows:


Similarly, the return value of a is changed back, we modify the MYVIEWGROUPB onintercepttouchevent () method, also returns True, and then click MyView or MYVIEWGROUPB, try. The log output is as follows:


That is, the event is only distributed to MYVIEWGROUPB, not to MyView, the entire flowchart is as follows:


Finally, if the underlying myview want to deal with it, we'll just change the return value of Ontouchevent () to return True. Then click MyView to see the log output.


The flowchart is this:





Well, this concludes the article, the first simple on the distribution and interception have a general understanding, but the event distribution mechanism has not fully analyzed, waiting for the next one to analyze.


Finally, if there is any doubt about the article, welcome to point out, common progress!




Android graphic analysis of event interception mechanism

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.