Android Touch Event Flow (i)

Source: Internet
Author: User

For Android touch events, it has been a bit blurry, so decided to figure it out. Here are three parts to solve the problem: the first part: how touch events originate. Part Two: How the view handles touch events. Part Three: ViewGroup is how to distribute and handle touch events.
this time see first part: How touch events originate.

To understand this problem, you should first know the following three points:

    • Every view that needs to be displayed on the phone is always implemented by Windowmanager.addview (), such as our usual activity/popwindow/status bar/Lock screen/alarm interface, etc.
    • In the Windowmanager.addview () process is actually called Windowmanagerglobal.addview (), in this method is created by creating a Viewrootimpl object, Finally, the view that needs to be displayed is added to this viewrootimpl. That is, the displayed view originates from the Viewrootimpl.
    • A view of activity originates from a decorview:decorview is actually a framelayout, we are in activity through Setcontentview () The added display view will eventually be added to this decorview.
Here's a look at the touch event flow to see how the touch event flows into the view:
(1)Each of the displayed Windows needs to be added via Windowmanager.addview (), This process creates a viewrootimpl that the view interacts with the system for the view that is displayed. The touch event here begins with the Viewrootimpl.
(2)A windowinputeventreceiver is defined in Viewrootimpl to receive touch events. Then pass the touch event to Mview. Mview is decorview in activity
(3)The view's Dispatchpointerevent (Motionevent event) was finally called through Viewrootimpl. So the touch event is passed to the displayed view.
We know that for activity, Viewrootimpl first passes the touch event to Decorview's Dispatchpointerevent (). Let's take a look at Decorview's Dispatchpointerevent source code:
public boolean dispatchtouchevent (Motionevent ev) {
Final Callback cb = Getcallback ();
return CB! = null &&!isdestroyed () && Mfeatureid < 0? Cb.dispatchtouchevent (EV)//will first call callback's Dispatchtouchevent method.
: Super.dispatchtouchevent (EV);
}
This is where the callback object comes from. Take a look at the source code of Getcallback (), which is defined in window as follows:
Public final Callback Getcallback () {
Return mcallback;//See, actually very simple, directly return to Mcallback can.
}
So when is mcallback assigned? We know that creating an activity will create his window for the activity, and in the activity you will find that the activity has implements this callback
With the activity code you will find that the mcallback of the window is actually assigned when the window is created:
Mwindow = Policymanager.makenewwindow (this);
Mwindow.setcallback (this);//This is the current activity
So the final Decorview call to dispatchtouchevent calls into the activity's Dispatchtouchevent () method.
(4) Take a look at what the dispatchtouchevent of activity does:
public boolean dispatchtouchevent (Motionevent ev) {
if (ev.getaction () = = Motionevent.action_down) {
Onuserinteraction ();
}
if (GetWindow (). superdispatchtouchevent (EV)) {//Will call the Superdispatchtouchevent Method of window and, if true, ends, Otherwise, call this activity's Ontouchevent method
return true;
}
return ontouchevent (EV);
}
(5)So take a look at window's Superdispatchtouchevent method. Window is a virtual class whose implementation class is Phonewindow
public boolean superdispatchtouchevent (Motionevent event) {
Return Mdecor.superdispatchtouchevent (event);//is actually the Superdispatchtouchevent method that calls Decorview
}
(6)Look at Decorview's superdispatchtouchevent.
public boolean superdispatchtouchevent (Motionevent event) {
Return Super.dispatchtouchevent (event);//See it, and finally call the Decorview Dispatchtouchevent method
}

(7) in summary: The touch event is passed from Viewrootimpl, and eventually the dispatchtouchevent of the view's most parent (the last/oldest) view is called. If you return False, The activity's Ontouchevent () method is called.


As you can see from the above analysis, touch events start with dispatchtouchevent, so the process of understanding touch events begins with the dispatchtouchevent of the view.

Android Touch Event Flow (i)

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.