How to deliver touch events in view in Android

Source: Internet
Author: User

First look at a picture


Copyright statement: This is the csdn on the other people's map, I feel useful, took over,

Then simply explain:

In general, the touch event is passed from the outermost viewgroup to the first level.

The callback method for each view associated with this is three, dispatchtouchevent,onintercepttouchevent, and the familiar outouchevent

The event is passed to a view (ViewGroup), which is first recalled to the Dispatchtouchevent () method, where, if not super, the current code is executed directly, super, to the Onintercepttouchevent () method, Here is the key, return false, the description does not intercept, continue to pass, true, the description of interception, direct truncation of the transfer chain, and then go to the view of the Outouchevent method, the whole is over.

Continue to pass, if there is no interception, from the child View Ontouchevent method has been passed up to the top view, if the transfer chain in the middle of a view ontouchevent return True,

This view has already handled the event, so that's it.


An analysis of the Android touch event transfer mechanism


The subclasses of each view in the Android system have the following three methods that are closely related to TouchEvent processing:

1 public boolean dispatchtouchevent (motionevent ev) This method is used to distribute TouchEvent
2 public boolean onintercepttouchevent (motionevent ev) This method is used to intercept touchevent
3 public boolean ontouchevent (motionevent ev) This method is used to process touchevent


Test Program Interface

The following 3 layout include relationships as shown in the following interface diagram.



Status 1: Handle Touch Events by center

The XML is as follows:

<?xml version= "1.0"  encoding= "Utf-8"?> <linearlayout xmlns:android= "http://" Schemas.android.com/apk/res/android "    android:layout_width=" Fill_parent "     android:layout_height= "fill_parent"     android:orientation= "vertical"   >     <dk.touch.mylayout         android:id= "@ +id/out "        android:layout_width=" Fill_parent "         android:layout_height= "Fill_parent"          android:gravity= "Center"             android: Background= "#ff345600"          >          <dk.touch.mylayout              android:id= "@+id/midDle "            android:layout_width=" 200DP "             android:layout_height= "200DP"              android:gravity= "Center"              android:background= "#ff885678"              >              <dk.touch.mylayout                  android:id= "@+id/center"                  android:layout_width= "50DP"                  android:layout_height= "50DP"          &nbSp;       android:background= "#ff345678"                  android:focusable= "true"                  android:focusableintouchmode= " True "                android: Clickable= "true"                   >             </dk.touch.mylayout >         </dk.touch.MyLayout>     </ Dk.touch.mylayout> </LinearLayout>


Note: Only the center section will handle/consume touch events.



Event-passing record results as shown above.

Because down, move, up event processing flow slightly different, so separate analysis.


Action_down Event Handling Process:




First touch event occurs (Action_down), the system calls the activity's Dispatchtouchevent method, and the event is distributed. According to the coordinates of the touch event, this event is passed to the out dispatchtouchevent processing, and out calls the onintercepttouchevent to determine whether the event is handled by itself or continues to be distributed to the child view. Because out does not handle touch events here, the event is passed to the direct child view of out (i.e. middle) according to the coordinates of the event.

Middle and center event handling process ibid. But since the center component is clickable that it can handle touch events, the Onintercepttouchevent method in center passes events to center's own ontouchevent method processing. At this point, this touch event has been processed and does not continue to be delivered.

The move and up event processing flows are similar, but the Dispatchtouchevent method within the center is directly assigned to ontouchevent processing without onintercepttouchevent judgment. This is because, in the Android system, 1 down events, n move events, 1 up events as a logical touch operation, the down event has determined the object that handles the event, then the subsequent move and up event also determines the object that handles the event.


State 2: None of the events are processed

The XML is as follows:

<?xml version= "1.0"  encoding= "Utf-8"?> <linearlayout xmlns:android= "http://" Schemas.android.com/apk/res/android "    android:layout_width=" Fill_parent "     android:layout_height= "fill_parent"     android:orientation= "vertical"   >     <dk.touch.mylayout         android:id= "@ +id/out "        android:layout_width=" Fill_parent "         android:layout_height= "Fill_parent"          android:gravity= "Center"             android: Background= "#ff345600"          >          <dk.touch.mylayout              android:id= "@+id/midDle "            android:layout_width=" 200DP "             android:layout_height= "200DP"              android:gravity= "Center"              android:background= "#ff885678"              >              <dk.touch.mylayout                  android:id= "@+id/center"                  android:layout_width= "50DP"                  android:layout_height= "50DP"          &nbSp;       android:background= "#ff345678"                   >              </dk.touch.MyLayout>          </dk.touch.MyLayout>     </dk.touch.MyLayout> </LinearLayout>




Light Touch Center part logcat output result




Action_down Event Handling Process:



The event handling process is roughly the same as the difference is that all components do not handle events in this state, and events are not "consumed" by the Ontouchevent method of the center, and the events are passed back to the activity, and if the activity does not handle the event, This event is equivalent to disappearing (no effect).


For subsequent move and up events, as the first down event has been determined by the activity to handle the event, the up thing has been directly distributed by the dispatchtouchevent of the activity to its own Ontouchevent method processing.

Source Code :


package dk.touch;
import android.app.activity;
import android.os.bundle;
import android.view.motionevent; public class mainactivity extends activity{        @ Override     protected void oncreate (bundle savedinstancestate)  { 
       super.oncreate (savedinstancestate);
        setcontentview (R.layout.main);                 mylayout out= (
Mylayout)  findviewbyid (r.id.out);
        out.setname ("Out");                 mylayout middle
= (mylayout)  findviewbyid (R.id.middle);
        middle.setname ("Middle");         &NBSP;&NBsp;      mylayout center= (mylayout)  findviewbyid (R.id.center);
        center.setname ("center"); &NBSP;&NBSP;&NBSP;&NBSP}      @Override     public boolean  Dispatchtouchevent (Motionevent ev)  {        int action=
Ev.getaction ();
        string actionname= "";         switch (Action)          {        case motionevent.action_down:     
        actionname= "Action_down";
            break;         case motionevent.action_move:           &Nbsp;  actionname= "Action_move";
            break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;MOTIONEVENT.ACTION_UP:      
       actionname= "Action_up";
            break;         }          System.out.println ("activity" + "|")
+actionname+ ":d ispatchtouchevent");
        return super.dispatchtouchevent (EV); &NBSP;&NBSP;&NBSP;&NBSP}      @Override     public boolean  Ontouchevent (motionevent event)  {        int action=
Event.getaction ();
        string actionname= "";         switch(action)         {        case  motionevent.action_down:             
Actionname= "Action_down";
            break;         case motionevent.action_move:     
        actionname= "Action_move";
            break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;MOTIONEVENT.ACTION_UP:      
       actionname= "Action_up";
            break;         }                  system.out.println ("ActiVity "+" | "
+actionname+ ": Ontouchevent");
        return super.ontouchevent (event);
&NBSP;&NBSP;&NBSP;&NBSP}        } package dk.touch;
import android.content.context;
import android.util.attributeset;
import android.view.motionevent;
import android.widget.linearlayout; public class mylayout extends linearlayout {    private 
String name= "";     public mylayout (context context, attributeset attrs)  { 
       super (Context, attrs); &NBSP;&NBSP;&NBSP;&NBSP}      @Override     public boolean  Ontouchevent (motionevent event)  {        int action=
Event.getaction ();
        string actionname= "";          switch (action)         {         case motionevent.action_down:      
       actionname= "Action_down";
            break;         case motionevent.action_move:     
        actionname= "Action_move";
            break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;MOTIONEVENT.ACTION_UP:      
       actionname= "Action_up";
            break;         }          System.out.println (name+"|"
+actionname+ ": Ontouchevent");
        return super.ontouchevent (event); &NBSP;&NBSP;&NBSP;&NBSP}      @Override     public boolean  Dispatchtouchevent (Motionevent ev)  {        int action=
Ev.getaction ();
        string actionname= "";         switch (Action)          {        case motionevent.action_down:     
        actionname= "Action_down";
            break;         case motionevent.action_move:     
        actionname= "Action_move";             break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;MOTIONEVENT.ACTION_UP:      
       actionname= "Action_up";
            break;         }          System.out.println (name+ "|")
+actionname+ ":d ispatchtouchevent");
        return super.dispatchtouchevent (EV); &NBSP;&NBSP;&NBSP;&NBSP}      @Override     public boolean  Onintercepttouchevent (Motionevent ev)  {        int 
Action=ev.getaction ();
        string actionname= "";         switch (Action)          {&NBsp;       case motionevent.action_down:      
       actionname= "Action_down";
            break;         case motionevent.action_move:     
        actionname= "Action_move";
            break; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;MOTIONEVENT.ACTION_UP:      
       actionname= "Action_up";
            break;         }          System.out.println (name+ "|")
+actionname+ ": Onintercepttouchevent");         return suPer.onintercepttouchevent (EV); &NBSP;&NBSP;&NBSP;&NBSP}     public string getname ()  {   
     return name; &NBSP;&NBSP;&NBSP;&NBSP}     public void setname (String name)  { 
       this.name = name; &NBSP;&NBSP;&NBSP;&NBSP}    }


Related Article

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.