The distribution and consumption mechanism of Touch events

Source: Internet
Author: User

Touch Event-related methods Method functions
ViewGroup
Activity
public boolean dispatchtouchevent (motionevent ev) Event distribution Yes Yes
public boolean onintercepttouchevent (motionevent ev) Event interception Yes No
public boolean ontouchevent (motionevent ev) Incident response Yes Yes

From this table we can see that the ViewGroup and its subclasses respond to the three methods associated with the Touch event, and the Activity does not respond to onintercepttouchevent (motionevent ev), which is event interception. It is also important to note that the response of the view to the Dispatchtouchevent (motionevent ev) and onintercepttouchevent (motionevent ev) is premised on the ability to add a child view to the view if The previous view is already a minimal unit view (such as TextView), so it is not possible to add a sub-view to this minimum view, nor to distribute and intercept the event to the child view, so it has no dispatchtouchevent ( Motionevent ev) and onintercepttouchevent (motionevent ev), only ontouchevent (Motionevent ev).

First, Touch incident analysis

? 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:
    1. If the current View gets events directly from the activity, the event is returned to the activity's ontouchevent for consumption;
    2. 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 you return the system's default Super. Dispatchtouchevent (EV), 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 to Superonintercepttouchevent (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.

Second, Touch case introduction

Also before I start the case analysis, I need to explain the structure of the test case, because all the tests are done for this case, and the test only reflects different situations by modifying the return values of the three methods in each control that are related to the Touch event. Take a look at the picture first:

The above diagram shows the layout file UI for the test case, and the layout file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><cn.sunzn.tevent.toucheventfather xmlns:android= "/http Schemas.android.com/apk/res/android "    android:layout_width=" fill_parent "    android:layout_height=" Fill_ Parent "    android:background=" #468AD7 "    android:gravity=" center "    android:orientation=" vertical ">    <cn.sunzn.tevent.toucheventchilds        android:id= "@+id/childs"        android:layout_width= "200DP        " android:layout_height= "200DP"        android:layout_gravity= "center"        android:background= "#E1110D"        android:text= "@string/hello"/></cn.sunzn.tevent.toucheventfather>

The blue background is a custom control Toucheventfather, which is an outer View and inherits from LinearLayout, implementing the following code:

Package Cn.sunzn.tevent;import Android.content.context;import Android.util.attributeset;import android.util.Log; Import Android.view.motionevent;import Android.widget.linearlayout;public class Toucheventfather extends    LinearLayout {public Toucheventfather (context context) {super (context);    } public Toucheventfather (context context, AttributeSet Attrs) {Super (context, attrs); } public boolean dispatchtouchevent (motionevent ev) {LOG.E ("Sunzn", "Toucheventfather |        Dispatchtouchevent "+ toucheventutil.gettouchaction (Ev.getaction ()));    return super.dispatchtouchevent (EV); } public boolean onintercepttouchevent (motionevent ev) {log.i ("Sunzn", "Toucheventfather |        Onintercepttouchevent "+ toucheventutil.gettouchaction (Ev.getaction ()));    return super.onintercepttouchevent (EV); } public boolean ontouchevent (motionevent ev) {LOG.D ("Sunzn", "Toucheventfather | Ontouchevent "+ Toucheventutil.gettouChaction (Ev.getaction ()));    return super.ontouchevent (EV); }}

The red background is a custom control toucheventchilds, which is the inner view, the Toucheventfather child view, also inherits from LinearLayout, the implementation code is as follows:

Package Cn.sunzn.tevent;import Android.content.context;import Android.util.attributeset;import android.util.Log; Import Android.view.motionevent;import Android.widget.linearlayout;public class Toucheventchilds extends    LinearLayout {public Toucheventchilds (context context) {super (context);    } public Toucheventchilds (context context, AttributeSet Attrs) {Super (context, attrs); } public boolean dispatchtouchevent (motionevent ev) {LOG.E ("Sunzn", "Toucheventchilds |        Dispatchtouchevent "+ toucheventutil.gettouchaction (Ev.getaction ()));    return super.dispatchtouchevent (EV); } public boolean onintercepttouchevent (motionevent ev) {log.i ("Sunzn", "Toucheventchilds |        Onintercepttouchevent "+ toucheventutil.gettouchaction (Ev.getaction ()));    return super.onintercepttouchevent (EV); } public boolean ontouchevent (motionevent ev) {LOG.D ("Sunzn", "Toucheventchilds | Ontouchevent "+ Toucheventutil.gettouChaction (Ev.getaction ()));    return super.ontouchevent (EV); }}

The code for the activity is then implemented, because all the events of the control are distributed through the activity's dispatchtouchevent, and the Ontouchevent method of the activity needs to be rewritten, because if a control is directly from the Ac Tivity gets the event that the event is first passed to the control's Dispatchtouchevent method, and if this method returns false, the event is returned to the Activity in a bubbling manner Ontouchevent for consumption. The implementation code is as follows:

Package Cn.sunzn.tevent;import Android.app.activity;import Android.os.bundle;import android.util.log;import Android.view.motionevent;public class Toucheventactivity extends Activity {public    void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);    }    public boolean dispatchtouchevent (motionevent ev) {        LOG.W ("Sunzn", "toucheventactivity | Dispatchtouchevent "+ toucheventutil.gettouchaction (Ev.getaction ()));        return super.dispatchtouchevent (EV);    }    public boolean ontouchevent (Motionevent event) {        LOG.W ("Sunzn", "toucheventactivity | Ontouchevent "+ toucheventutil.gettouchaction (Event.getaction ()));        Return Super.ontouchevent (event);}    }

Finally, attach the Toucheventutil code, Toucheventutil did not do much, but the above 2 custom controls in each method of the Motionevent concentrated into a tool class and its corresponding action is returned as a String, This process makes it easier to observe the events of the control in real time. The code is as follows:

Package Cn.sunzn.tevent;import Android.view.motionevent;public class Toucheventutil {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;    }    }

The distribution and consumption mechanism of Touch events

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.