Android Touch event distribution process, androidtouch

Source: Internet
Author: User

Android Touch event distribution process, androidtouch

This article mainly introduces the Android Touch event distribution process, describes the main processing process of Android Touch events in detail, and helps you understand Android Program Design in depth. For more information, see


Step 1: What are the events?


UI is a button


MainActivity file:

public class MainActivity extends Activity {     @Override  protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);       Button mBtn = (Button) findViewById(R.id.my_button);     mBtn.setOnTouchListener(new OnTouchListener() {         @Override      public boolean onTouch(View v, MotionEvent event) {         Log.d("", "### onTouch : " + event.getAction());         return false;       }     });     mBtn.setOnClickListener(new OnClickListener() {         @Override      public void onClick(View v) {         Log.d("", "### onClick : " + v);       }     });     }     @Override  public boolean dispatchTouchEvent(MotionEvent ev) {     Log.d("", "### activity dispatchTouchEvent");     return super.dispatchTouchEvent(ev);   } } 
When you click the button, the following Log is output:

### Activity dispatchTouchEvent
### OnTouch: 0
### Activity dispatchTouchEvent
### OnTouch: 1
### OnClick: android. widget. Button {52860d98 VFED... C... PH... 1080,144-# 7f05003d app: id/my_button}


First, you must understand:

A simple screen Touch action triggers a series of Touch events: ACTION_DOWN-> ACTION_MOVE-> ACTION_UP


When the screen contains a ViewGroup and the ViewGroup contains a sub-view, how does the android system handle Touch events? Is ViewGroup used to process Touch events or child views used to process Touch events? This is not necessarily true. Why? Check the following survey results to understand.


Each View subclass in the android system has the following three methods closely related to TouchEvent processing:

1) public boolean dispatchTouchEvent (MotionEvent ev) is used to distribute TouchEvent

2) public boolean onInterceptTouchEvent (MotionEvent ev) This method is used to intercept TouchEvent

3) The public boolean onTouchEvent (MotionEvent ev) method is used to process TouchEvent


When a TouchEvent occurs, the Activity first passes the TouchEvent to the top-level View, and the TouchEvent first reaches the dispatchTouchEvent of the top-level view.
Then the dispatchTouchEvent method is used for distribution. If the dispatchTouchEvent returns true, the onTouchEvent of the view is handed over to handle it. If the dispatchTouchEvent returns false, the interceptTouchEvent method assigned to the view is used to determine whether to intercept this event. If interceptTouchEvent returns true, that is, it intercepts the event, and the onTouchEvent is handed over to it to handle the problem. If interceptTouchEvent returns false, then it is passed to the sub-view, and the dispatchTouchEvent of the sub-view will start distributing the event. If the event is passed to the onTouchEvent of the subview of a certain layer, the method returns false, and the event will be passed up from this view, which is received by onTouchEvent. If false is returned when the onTouchEvent is passed to the top, the event will disappear and the next event will not be received.


Return to original article ----------------

We can see that the dispatchTouchEvent method in the Activity is executed first, and then the onTouch method is executed.

Then there is dispatchTouchEvent --> onTouch, and finally the Click Event of the execution button. Here we may have a question

Why does dispatchTouchEvent and onTouch both be executed twice, while onClick is executed once? Why are the actions of two Touch events different?

What exactly does action 0 and action 1 represent?


Friends who have overwritten onTouchEvent know that, in general, we will handle the touch type events in this method, such as ACTION_DOWN, ACTION_MOVE, and ACTION_UP.

However, the above example does not move, but simply press and lift. Therefore, our touch events only have to be pressed and lifted, so there are two touch events.

Action is 0 and 1 respectively. Let's take a look at some variable definitions in MotionEvent:

Public final class MotionEvent extends InputEvent implements Parcelable {// code omitting public static final int ACTION_DOWN = 0; // press the event public static final int ACTION_UP = 1; // lift event public static final int ACTION_MOVE = 2; // gesture move event public static final int ACTION_CANCEL = 3; // cancel // Code omitted}

Facts prove to be true

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.