The transfer mechanism of touch events in Android

Source: Internet
Author: User

Because before the event delivery mechanism of Android not understand, today is not busy, hurriedly take the time to understand this knowledge, this article combined with demo, the event transmission mechanism of Android analysis.

In the event delivery process, the following three methods are inseparable:

1.dispatchTouchEvent distribution TouchEvent, when the return value is True, indicates that touchevent is being processed by the current view. Events are not passed down the layer (including subsequent onintercepttouchevent and ontouchevent),

Dispatchtouchevent will receive subsequent Action_move and ACTION_UP events.

2.onInterceptTouchEvent intercepts touchevent, returns True to indicate that the current view intercepts touchevent, and then gives the event to the current view's ontouchevent processing

3.onTouchEvent handles touchevent, which returns true to indicate that the current view consumes this event, and that subsequent events cannot be received until the previous event is consumed.

In order to figure out the event passing of Android in each layer of view, I wrote a small demo to analyze the event passing between Activity,viewgroup,view.

First look at the code, a custom view, draw a rectangle, in the dispatchtouchevent,ontouchevent to join the log for easy parsing.

1  Public classDrawrectviewextendsView {2 3     PrivatePaint Mpaint;4 5      PublicDrawrectview (Context context) {6         Super(context);7Mpaint =NewPaint (paint.anti_alias_flag);8     }9 Ten      PublicDrawrectview (context context, AttributeSet set) { One         Super(context, set); AMpaint =NewPaint (paint.anti_alias_flag); -     } -  the @Override -      Public Booleandispatchtouchevent (Motionevent event) { -LOG.V (Logutils.tag, "Drawrectview dispatchtouchevent action=" +event.getaction ()); -         return Super. Dispatchtouchevent (event); +     } -  +  A @Override at     protected voidOnDraw (canvas canvas) { -         Super. OnDraw (canvas); - Mpaint.setcolor (color.yellow); -Canvas.drawrect (0, 0, 300, 300, mpaint); -     } -  in @Override -      Public Booleanontouchevent (Motionevent event) { toLOG.V (Logutils.tag, "Drawrectview ontouchevent action=" +event.getaction ()); +         return Super. Ontouchevent (event); -     } the}

Custom layout, also include log in the relevant TouchEvent method

1  Public classMylayoutextendsRelativelayout {2 3      PublicMylayout (Context context, AttributeSet attrs) {4         Super(context, attrs);5     }6 7 @Override8      Public Booleandispatchtouchevent (motionevent ev) {9LOG.V (Logutils.tag, "Mylayout dispatchtouchevent action=" +ev.getaction ());Ten         return Super. dispatchtouchevent (EV); One     } A  - @Override -      Public Booleanonintercepttouchevent (motionevent ev) { theLOG.V (Logutils.tag, "Mylayout onintercepttouchevent action=" +ev.getaction ()); -         return Super. onintercepttouchevent (EV); -     } -  + @Override -      Public Booleanontouchevent (Motionevent event) { +LOG.V (Logutils.tag, "Mylayout ontouchevent event=" +event.getaction ()); A         return Super. Ontouchevent (event); at}

Next is the layout of activity and activity.

1  Public classTouchtestactivityextendsActivity {2 3     PrivateDrawrectview Mdrawrectview;4 5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.touch_test_activity);9 TenMdrawrectview =(Drawrectview) Findviewbyid (R.id.draw_rect_view); OneMdrawrectview.setontouchlistener (NewOntouchlistener () { A @Override -              Public BooleanOnTouch (View V, motionevent event) { -LOG.V (Logutils.tag, "Mdrawrectview ontouchlistener action=" +event.getaction ()); the                 return false; -             } -         }); -     } +  - @Override +      Public Booleandispatchtouchevent (motionevent ev) { ALOG.V (Logutils.tag, "touchtestactivity dispatchtouchevent action=" +ev.getaction ()); at         return Super. dispatchtouchevent (EV); -     } -  - @Override -      Public Booleanontouchevent (Motionevent event) { -LOG.V (Logutils.tag, "touchtestactivity ontouchevent action=" +event.getaction ()); in         return Super. Ontouchevent (event); -     } to}
  1  <com.yangy.test.custom_view.     Mylayout xmlns:android= "http://schemas.android.com/apk/res/android"  2  Android:layout_width= "Match_parent"  android:layout_height=" Match_parent " >  5  <com.yangy.test.custom_view. Drawrectview   6  android:id=" @+id/draw_rect_view " 7  android:layout_width= "300DP"  8  android:layout_height= "300d P " 9  android:layout_centerinparent=" true "/>10  11  </com.yangy.test.custom_view. Mylayout> 

When we press the rectangle Drawrectview, we can see the printed log information as follows, the Android touch event is passed from top to bottom, Activity-->viewgroup-->view

11-24 15:19:40.659:v/--debug--(32570): touchtestactivity dispatchtouchevent action=action_down
11-24 15:19:40.659:v/--debug--(32570): Mylayout dispatchtouchevent action=action_down
11-24 15:19:40.659:v/--debug--( 32570): Mylayout onintercepttouchevent action=action_down
11-24 15:19:40.659:v/--debug--(32570): DrawRectView Dispatchtouchevent action=action_down
11-24 15:19:40.659:v/--debug--(32570): Mdrawrectview OnTouchListener Action=action_down
11-24 15:19:40.669:v/--debug--(32570): Drawrectview ontouchevent Action=ACTION_DOWN
11-24 15:19:40.669:v/--debug--(32570): Mylayout ontouchevent event=action_down
11-24 15:19:40.669:v/--debug--(32570): Touchtestactivity ontouchevent action=action_down
11-24 15:19:40.689:v/--debug--(32570): TouchTestActivity Dispatchtouchevent action=action_up
11-24 15:19:40.689:v/--debug--(32570): Touchtestactivity onTouchEvent Action =ACTION_UP

According to the log information, we also know the entire view of the event delivery process, can be expressed, it is worth noting that no view consumes the Action_down event,

So the subsequent action_move and ACTION_UP events do not pass down, as can be seen from the log.

When you return the Drawrectview Ontouchevent method to True, what will happen and then look at the log

11-24 16:04:03.159:v/--debug--(3037): touchtestactivity dispatchtouchevent Action=action_down
11-24 16:04:03.159:v/--debug--(3037): Mylayout dispatchtouchevent Action=action_down
11-24 16:04:03.159:v/--debug--(3037): Mylayout onintercepttouchevent Action=action_down
11-24 16:04:03.159:v/--debug--(3037): Drawrectview dispatchtouchevent Action=action_down
11-24 16:04:03.159:v/--debug--(3037): Mdrawrectview ontouchlistener Action=action_down
11-24 16:04:03.159:v/--debug--(3037): Drawrectview ontouchevent Action=action_down


11-24 16:04:03.219:v/--debug--(3037): touchtestactivity dispatchtouchevent action=action_move
11-24 16:04:03.219:v/--debug--(3037): Mylayout dispatchtouchevent action=action_move
11-24 16:04:03.219:v/--debug--( 3037): Mylayout onintercepttouchevent action=action_move
11-24 16:04:03.219:v/--debug--(3037): DrawRectView Dispatchtouchevent action=action_move
11-24 16:04:03.219:v/--debug--(3037): Mdrawrectview OnTouchListener Action =action_move
11-24 16:04:03.219:v/--debug--(3037): Drawrectview ontouchevent action=action_move


11-24 16:04:03.249:v/--debug--(3037): touchtestactivity dispatchtouchevent action=action_up
11-24 16:04:03.249:v/--debug--(3037): Mylayout dispatchtouchevent action=action_up
11-24 16:04:03.249:v/--debug--(3037): Mylayout onintercepttouchevent action=action_up
11-24 16:04:03.249:v/--debug--(3037): Drawrectview dispatchtouchevent action=action_up
11-24 16:04:03.249:v/--debug--(3037): Mdrawrectview ontouchlistener action=action_up
11-24 16:04:03.249:v/--debug--(3037): Drawrectview ontouchevent action=action_up

It seems that the Drawrectview Ontouchevent method consumes Action_down event, Action_move and Action_up are passed over, and because the consumption of events, so ontouchevent will not be passed up

What if the TouchEvent event was intercepted in the ViewGroup, the origin of the explanation:

After the instructions in this article, I believe you know more about the event delivery mechanism of Android.

The transfer mechanism of touch events in Android

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.