Android Touch Event Distribution mechanism learning

Source: Internet
Author: User

Events in Android are divided into keystroke events and touch events.
The touch event consists of a action_down,n action_move, a action_up composed of onclick,onlongclick,onscroll and other events.
The methods associated with Touch events in Android and their corresponding effects:
Dispatchtouchevent (motionevent EV) event distribution
Onintercepttouchevent (motionevent EV) event interception
Ontouchevent (motionevent EV) event response

Activity:dispatchtouchevent (motionevent ev), ontouchevent (motionevent ev)
View:dispatchtouchevent (motionevent ev), ontouchevent (motionevent ev)
ViewGroup inherited view, the above method is public, so he has all the above methods, ViewGroup class inside the new method onintercepttouchevent (Motionevent ev).

For a clearer understanding of the execution process, I did a little demo:

Here we need to know one thing, there are two kinds of controls: one is to inherit the control that the view cannot contain other controls, and the other is to inherit a control that ViewGroup can contain other controls, such as a container control, such as Listview,gridview,linearlayout.
The button above the demo is inherited from the view, the light green area is a horizontal linearlayout, and he is inherited from the ViewGroup.

Let's summarize the demo production:
There are three classes in it:
MyActivity extends Activity
MyButton extends Button
Mylinearlayout extends LinearLayout
In the myactivity:

 Public  class myactivity extends Activity{    Private Static FinalString TAG = MyActivity.class.getSimpleName ();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_main); } Public Boolean dispatchtouchevent(Motionevent ev) {log.i (TAG, Motionevent.actiontostring (Ev.getaction ()) +"Dispatchtouchevent ()");return Super. dispatchtouchevent (EV); } Public Boolean ontouchevent(Motionevent event) {log.i (TAG, Motionevent.actiontostring (Event.getaction ()) +"Ontouchevent ()");return Super. Ontouchevent (event); }}

MyButton and Mylinearlayout very similar, mylinearlayout only one more Onintercepttouchevent method, where the MyButton code is posted, If you need to mylinearlayout the code can be downloaded directly later, see the full code.
MyButton

 Public  class MyButton extends Button{    Private Static FinalString TAG = MyButton.class.getSimpleName (); Public MyButton(Context context) {Super(context); } Public MyButton(context context, AttributeSet attrs) {Super(context, attrs); } Public MyButton(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr);//TODO auto-generated constructor stub} Public Boolean dispatchtouchevent(Motionevent ev) {log.i (TAG, Motionevent.actiontostring (Ev.getaction ()) +"Dispatchtouchevent ()");return Super. dispatchtouchevent (EV); } Public Boolean ontouchevent(Motionevent event) {log.i (TAG, Motionevent.actiontostring (Event.getaction ()) +"Ontouchevent ()");return Super. Ontouchevent (event); }}

Java code is very simple.
Because we want to use our own defined controls, this is the case in the XML layout code:

<per. Lee. Eventdispatchtest. MylinearlayoutXmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Wrap_content"Android:background="#00FF00"> <per. Lee. Eventdispatchtest. MyButtonAndroid:id="@+id/button1"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text="button"/></per. Lee. Eventdispatchtest. Mylinearlayout>

Log display as tag display: Specific class, text display: Specific action + Call method name

Click MyButton (Dark green area) and the result is:

Click Mylinearlayout Non-mybutton area (light green area) and the result is:

Click myactivity Non-mylinearlayout area (blank area) and the result is:

From the above log can be seen:
A touch event is distributed from the outer layer to the inner layer, and until the event is processed (return true), the analysis of the surface is finished, and later I find the opportunity to study the source code, the internal specific exactly how to deal with.

This little demo code: Eventdispatchtest.zip

Android Touch Event Distribution mechanism learning

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.