Android-事件分发机制

本文传达的内容:

通过一个实例结合源码分析MotionEvent对象的传递过程。

实验:

自定义两个ViewGroup重写它们的dispatchTouchEvent(),onInterceptTouchEvent(),onTouchEvent()方法,和一个View重写它的dispatchTouchEvent(),onTouchEvent()方法。

MyGroupViewA:

 1 public class MyGroupViewA extends LinearLayout {
 2
 3     public MyGroupViewA(Context context) {
 4         super(context);
 5     }
 6
 7     public MyGroupViewA(Context context, AttributeSet attrs) {
 8         super(context, attrs);
 9     }
10
11     public MyGroupViewA(Context context, AttributeSet attrs,
12                         int defStyleAttr) {
13         super(context, attrs, defStyleAttr);
14     }
15
16     @Override
17     public boolean dispatchTouchEvent(MotionEvent ev) {
18         Log.d("dhn", "ViewGroupA dispatchTouchEvent" + ev.getAction());
19         return super.dispatchTouchEvent(ev);
20     }
21
22     @Override
23     public boolean onInterceptTouchEvent(MotionEvent ev) {
24         Log.d("dhn", "ViewGroupA onInterceptTouchEvent" + ev.getAction());
25         return super.onInterceptTouchEvent(ev);
26     }
27
28     @Override
29     public boolean onTouchEvent(MotionEvent event) {
30         Log.d("dhn", "ViewGroupA onTouchEvent" + event.getAction());
31         return super.onTouchEvent(event);
32     }
33 }

MyGroupViewB:

 1 public class MyGroupViewB extends LinearLayout {
 2
 3     public MyGroupViewB(Context context) {
 4         super(context);
 5     }
 6
 7     public MyGroupViewB(Context context, AttributeSet attrs) {
 8         super(context, attrs);
 9     }
10
11     public MyGroupViewB(Context context, AttributeSet attrs,
12                         int defStyleAttr) {
13         super(context, attrs, defStyleAttr);
14     }
15
16     @Override
17     public boolean dispatchTouchEvent(MotionEvent ev) {
18         Log.d("dhn", "ViewGroupB dispatchTouchEvent" + ev.getAction());
19         return super.dispatchTouchEvent(ev);
20     }
21
22     @Override
23     public boolean onInterceptTouchEvent(MotionEvent ev) {
24         Log.d("dhn", "ViewGroupB onInterceptTouchEvent" + ev.getAction());
25         return super.onInterceptTouchEvent(ev);
26     }
27
28     @Override
29     public boolean onTouchEvent(MotionEvent event) {
30         Log.d("dhn", "ViewGroupB onTouchEvent" + event.getAction());
31         return true;
32     }
33 }

MyView:

 1 public class MyView extends View {
 2     public MyView(Context context) {
 3         super(context);
 4     }
 5
 6     public MyView(Context context, AttributeSet attrs) {
 7         super(context, attrs);
 8     }
 9
10     public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
11         super(context, attrs, defStyleAttr);
12     }
13
14     @Override
15     public boolean onTouchEvent(MotionEvent event) {
16         Log.d("dhn", "View onTouchEnent" + event.getAction());
17         return super.onTouchEvent(event);
18     }
19
20     @Override
21     public boolean dispatchTouchEvent(MotionEvent event) {
22         Log.d("dhn", "View dispatchTouchEvent" + event.getAction());
23         return super.dispatchTouchEvent(event);
24     }
25 }

布局文件:activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent">
 4
 5     <com.dhn.touchevnettest.MyGroupViewA
 6         android:layout_width="match_parent"
 7         android:layout_height="match_parent"
 8         android:background="@android:color/holo_blue_bright">
 9
10         <com.dhn.touchevnettest.MyGroupViewB
11             android:layout_width="300dp"
12             android:layout_height="300dp"
13             android:background="@android:color/holo_green_dark">
14
15             <com.dhn.touchevnettest.MyView
16                 android:id="@+id/myView"
17                 android:layout_width="100dp"
18                 android:layout_height="100dp"
20                 android:background="@android:color/darker_gray"/>
21
22
23         </com.dhn.touchevnettest.MyGroupViewB>
24
25     </com.dhn.touchevnettest.MyGroupViewA>
26
27 </RelativeLayout>

效果图:

实验一:点击最小的方块

结果:

/com.dhn.touchevnettest D/dhn﹕ ViewGroupA dispatchTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ ViewGroupA onInterceptTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ ViewGroupB dispatchTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ ViewGroupB onInterceptTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ View dispatchTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ View onTouchEnent0
/com.dhn.touchevnettest D/dhn﹕ ViewGroupB onTouchEvent0
/com.dhn.touchevnettest D/dhn﹕ ViewGroupA onTouchEvent0

分析:

首先事件从上层传递到MyGroupViewA对象,其dispatchTouchEvent()被调用,打印出ViewGroupA dispatchTouchEvent0。然后调用super.dispatchTouchEvent()即ViewGroup.dispatchTouchEvent(),该方法部分如下:

code1:

 1  final boolean intercepted;
 2             if (actionMasked == MotionEvent.ACTION_DOWN
 3                     || mFirstTouchTarget != null) {
 4                 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
 5                 if (!disallowIntercept) {
 6                     intercepted = onInterceptTouchEvent(ev);
 7                     ev.setAction(action); // restore action in case it was changed
 8                 } else {
 9                     intercepted = false;
10                 }
11             } else {
12                 // There are no touch targets and this action is not an initial down
13                 // so this view group continues to intercept touches.
14                 intercepted = true;
15             }

如果是ACTION_DOWN事件,或mFirstTouchTarget != null()则会调用onInterceptionTouchEvent(),这里显然会进入该方法,在该方法中,我们打印ViewGroupA onInterceptTouchEvent0,然后调用surper.onInterceptTouchEvent(),即ViewGroup.onInterceptTouchEvent(),我们来看下它的源码:

code2:

1     public boolean onInterceptTouchEvent(MotionEvent ev) {
2         return false;
3     }

该方法返回false,所以intercepted被赋值为false(code1第6行),接着往下执行ViewGroup.dispatchTouchEvent():

code3:

  1 if (!canceled && !intercepted) {
  2
  3                 // If the event is targeting accessiiblity focus we give it to the
  4                 // view that has accessibility focus and if it does not handle it
  5                 // we clear the flag and dispatch the event to all children as usual.
  6                 // We are looking up the accessibility focused host to avoid keeping
  7                 // state since these events are very rare.
  8                 View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
  9                         ? findChildWithAccessibilityFocus() : null;
 10
 11                 if (actionMasked == MotionEvent.ACTION_DOWN
 12                         || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
 13                         || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
 14                     final int actionIndex = ev.getActionIndex(); // always 0 for down
 15                     final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
 16                             : TouchTarget.ALL_POINTER_IDS;
 17
 18                     // Clean up earlier touch targets for this pointer id in case they
 19                     // have become out of sync.
 20                     removePointersFromTouchTargets(idBitsToAssign);
 21
 22                     final int childrenCount = mChildrenCount;
 23                     if (newTouchTarget == null && childrenCount != 0) {
 24                         final float x = ev.getX(actionIndex);
 25                         final float y = ev.getY(actionIndex);
 26                         // Find a child that can receive the event.
 27                         // Scan children from front to back.
 28                         final ArrayList<View> preorderedList = buildOrderedChildList();
 29                         final boolean customOrder = preorderedList == null
 30                                 && isChildrenDrawingOrderEnabled();
 31                         final View[] children = mChildren;
 32                         for (int i = childrenCount - 1; i >= 0; i--) {
 33                             final int childIndex = customOrder
 34                                     ? getChildDrawingOrder(childrenCount, i) : i;
 35                             final View child = (preorderedList == null)
 36                                     ? children[childIndex] : preorderedList.get(childIndex);
 37
 38                             // If there is a view that has accessibility focus we want it
 39                             // to get the event first and if not handled we will perform a
 40                             // normal dispatch. We may do a double iteration but this is
 41                             // safer given the timeframe.
 42                             if (childWithAccessibilityFocus != null) {
 43                                 if (childWithAccessibilityFocus != child) {
 44                                     continue;
 45                                 }
 46                                 childWithAccessibilityFocus = null;
 47                                 i = childrenCount - 1;
 48                             }
 49
 50                             if (!canViewReceivePointerEvents(child)
 51                                     || !isTransformedTouchPointInView(x, y, child, null)) {
 52                                 ev.setTargetAccessibilityFocus(false);
 53                                 continue;
 54                             }
 55
 56                             newTouchTarget = getTouchTarget(child);
 57                             if (newTouchTarget != null) {
 58                                 // Child is already receiving touch within its bounds.
 59                                 // Give it the new pointer in addition to the ones it is handling.
 60                                 newTouchTarget.pointerIdBits |= idBitsToAssign;
 61                                 break;
 62                             }
 63
 64                             resetCancelNextUpFlag(child);
 65                             if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
 66                                 // Child wants to receive touch within its bounds.
 67                                 mLastTouchDownTime = ev.getDownTime();
 68                                 if (preorderedList != null) {
 69                                     // childIndex points into presorted list, find original index
 70                                     for (int j = 0; j < childrenCount; j++) {
 71                                         if (children[childIndex] == mChildren[j]) {
 72                                             mLastTouchDownIndex = j;
 73                                             break;
 74                                         }
 75                                     }
 76                                 } else {
 77                                     mLastTouchDownIndex = childIndex;
 78                                 }
 79                                 mLastTouchDownX = ev.getX();
 80                                 mLastTouchDownY = ev.getY();
 81                                 newTouchTarget = addTouchTarget(child, idBitsToAssign);
 82                                 alreadyDispatchedToNewTouchTarget = true;
 83                                 break;
 84                             }
 85
 86                             // The accessibility focus didn‘t handle the event, so clear
 87                             // the flag and do a normal dispatch to all children.
 88                             ev.setTargetAccessibilityFocus(false);
 89                         }
 90                         if (preorderedList != null) preorderedList.clear();
 91                     }
 92
 93                     if (newTouchTarget == null && mFirstTouchTarget != null) {
 94                         // Did not find a child to receive the event.
 95                         // Assign the pointer to the least recently added target.
 96                         newTouchTarget = mFirstTouchTarget;
 97                         while (newTouchTarget.next != null) {
 98                             newTouchTarget = newTouchTarget.next;
 99                         }
100                         newTouchTarget.pointerIdBits |= idBitsToAssign;
101                     }
102                 }
103             }

遍历每个子元素,65行调用dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)),我们看下这个方法:

code4:

1             if (child == null) {
2                 handled = super.dispatchTouchEvent(event);
3             } else {
4                 handled = child.dispatchTouchEvent(event);

里面有这么一段,所以会调用MyGroupViewB.dispatchTouchEvent()方法,成功将事件传递给子View。MyGroupViewB按同样的步骤将事件传递给MyView(期间打印了ViewGroupB dispatchTouchEvent0, ViewGroupB onInterceptTouchEvent0)。MyView的dispatchTouchEvent()方法被调用(打印View dispatchTouchEvent0),然后调用super.dispatchTouchEvent()方法即View.dispatchEvent()方法,我们看下该方法片段:

code5:

 1             ListenerInfo li = mListenerInfo;
 2             if (li != null && li.mOnTouchListener != null
 3                     && (mViewFlags & ENABLED_MASK) == ENABLED
 4                     && li.mOnTouchListener.onTouch(this, event)) {
 5                 result = true;
 6             }
 7
 8             if (!result && onTouchEvent(event)) {
 9                 result = true;
10             }

如果设置了OnTouchListeener则会优先调用onTouch()方法,该方法的返回这也会影响是否会调用onTouchEvent()。这里我们没有设置OnTouchListener则会调用第8行的onTouchEvent()方法(打印View onTouchEnent0),接着调用super.onTouchEvent方法,即View.onTouchEvent()方法,第8行看到该方法的返回值会影响result的值,若返回true则result为true从而导致dispatchTouchEvent()方法返回true,不改写的情况下返回false。该方法返回后回到MyGroupViewB.dispathTouchEvent()方法的65行,因为返回false,所以不进入for循环,跳出循环,进入如下代码片段:

code6:

1             if (mFirstTouchTarget == null) {
2                 // No touch targets so treat this as an ordinary view.
3                 handled = dispatchTransformedTouchEvent(ev, canceled, null,
4                         TouchTarget.ALL_POINTER_IDS);

mFirstTouchTarget==null成立,再次进入dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS)方法,因为传入的第三个参数是null,根据code4所以这次会调用super.dispatchTouchEvent()方法,即View.dispatchTouchEvent()方法,这里会打印:ViewGroupB onTouchEvent0。然后从MyGroupViewB.dispathTouchEvent()方法返回,进入MyGroupViewA.dispathTouchEvent()方法的65行往下执行(和MyGroupViewB类似)期间打印:ViewGroupA onTouchEvent0。这样就从MyGroupViewA.dispathTouchEvent()返回了,事件分发在这三个控件的部分也就结束了。

实验二:

将MyGroupViewA的onInterceptedTouchEvent()方法直接返回true。

结果:

01-19 18:00:02.768 23804-23804/com.dhn.touchevnettest D/dhn﹕ ViewGroupA dispatchTouchEvent0
01-19 18:00:02.828 23804-23804/com.dhn.touchevnettest D/dhn﹕ ViewGroupA dispatchTouchEvent2
01-19 18:00:02.838 23804-23804/com.dhn.touchevnettest D/dhn﹕ ViewGroupA dispatchTouchEvent1

分析:

MyGroupViewA.dispatchTouchEvent()方法中code1第九行intercepted = onInterceptTouchEvent(ev)直接将intercepted置为true,向下执行code3第一行if判断失败,就不会进入事件分发的for循环code3第32行,也就意味着不进行事件在MyGroupViewA这里被截断,不继续往MyGroupViewB分发。直接进入code6。

实验三:

MyView的onTouchEvent()直接返回true。

结果:

01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ ViewGroupA dispatchTouchEvent0
01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ ViewGroupA onInterceptTouchEvent0
01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ ViewGroupB dispatchTouchEvent0
01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ ViewGroupB onInterceptTouchEvent0
01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ View dispatchTouchEvent0
01-19 18:12:04.688 29980-29980/com.dhn.touchevnettest D/dhn﹕ View onTouchEnent0

分析:

该实验事件分发同实验一,不同的是当执行到MyView.onTouchEvent()方法时,直接返回true。正如code6第8行所示,result被设置为true,导致dispatchTouchEvent()返回true,返回到MyGroupViewB.dispatchTouchEvent()方法时会进入for循环(code3第65行),设置mFirstTouchTarget != null,这样code6第一行的判断就为假,不会调用dispatchTransformedTouchEvent(ev, canceled, TouchTarget.ALL_POINTER_IDS)也就不会调用MyGroupViewB.onTouchEvent(),接着MyGroupViewB.dispatchTouchEvent()返回true,返回进入MyGroupViewA.dipatchTouchEvent()时同样不会调用onTouchEvent()方法。

参考:《Android群英传》-徐宜生

时间: 2024-08-03 00:25:59

Android-事件分发机制的相关文章

Android事件分发机制详解:史上最全面、最易懂

前言 Android事件分发机制是每个Android开发者必须了解的基础知识 网上有大量关于Android事件分发机制的文章,但存在一些问题:内容不全.思路不清晰.无源码分析.简单问题复杂化等等 今天,我将全面总结Android的事件分发机制,我能保证这是市面上的最全面.最清晰.最易懂的 本文秉着"结论先行.详细分析在后"的原则,即先让大家感性认识,再通过理性分析从而理解问题: 所以,请各位读者先记住结论,再往下继续看分析: 文章较长,阅读需要较长时间,建议收藏等充足时间再进行阅读 目

Android事件分发机制

转载请注明出处:http://blog.csdn.net/chziroy/article/details/44401615 要理解Android事件分发机制,首先得了解几个概念,也算是总结,如果暂时看不懂也无妨,本文会讲解这几个问题. 1,点击屏幕,首先事件的传递从Activity的dispatchTouchEvent()方法开始. 2,关于Android事件分发机制,相关方法的方法有三个:onTouchEvent(),dispatchTouchEvent(),还有onInterceptTouc

图解 Android 事件分发机制

首发原文:http://mp.weixin.qq.com/s?__biz=MzI0MjE3OTYwMg==&mid=2649548149&idx=1&sn=709149df682c7d3a6e453c9ef0626a1f&chksm=f1180e08c66f871eb2e7e39e057a5b090214fd71adcd98aa36b3d7fcecf77ad5d08138c50131#rd 在Android开发中,事件分发机制是一块Android比较重要的知识体系,了解并熟

Android事件分发机制的学习

最近被Android事件分发机制折磨的很烦躁,网上各种博客资料看完觉得还是得自己写一篇,一方面加深理解,另一方面希望能帮助到也同样在学习相关知识的童鞋们. 话不多说,直接开整. 当用户的手指点击到屏幕,便是整个事件的开始. 首先获取到该事件的是view层的控制者Activity,具体怎么获得我们不得而知,在此也不追究,而继续我们的主题.Activity获得事件后便执行它自身的方法: public boolean dispatchTouchEvent(MotionEvent ev) { if (e

Android事件分发机制完全解析,带你从源码的角度彻底理解(上)

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客开始,就零零散散在好多地方使用到了Android事件分发的知识.也有好多朋友问过我各种问题,比如:onTouch和onTouchEvent有什么区别,又该如何使用?为什么给ListView引入了一个滑动菜单的功能,ListView就不能滚动了?为什么图片轮播器里的图片使用Button而不用Ima

android 事件分发机制(图文详解)

在Android开发中,事件分发机制是一块Android比较重要的知识体系,了解并熟悉整套的分发机制有助于更好的分析各种点击滑动失效问题,更好去扩展控件的事件功能和开发自定义控件,同时事件分发机制也是Android面试必问考点之一,如果你能把下面的一些事件分发图当场画出来肯定加分不少.废话不多说,总结一句:事件分发机制很重要. Android 事件分发流 关于Android 事件分发机制网上的博文很多,但是很多都是写个Demo然后贴一下输出的Log或者拿源码分析,然后一堆的注释和说明,如果用心的

Android事件分发机制详解(1)----探究View的事件分发

探究View的事件分发 在Activity中,只有一个按钮,注册一个点击事件 [java] view plaincopy button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "onClick execute"); } }); 如果在需要一个触摸事件 [java] view plaincopy button.setO

Android事件分发机制详解(2)----分析ViewGruop的事件分发

首先,我们需要 知道什么是ViewGroup,它和普通的View有什么区别? ViewGroup就是一组View的集合,它包含很多子View和ViewGroup,是Android 所有布局的父类或间接父类. 但ViewGroup也是一个View,只不过比起View,它可以包含子View和定义布局参数的功能. 现在,通过一个Demo演示Android中ViewGroup的事件分发机制. 首先我们来自定义一个布局,命名为MyLayout,继承自LinearLayout,如下 所示: public c

Android 事件分发机制具体解释

很多其它内容请參照我的个人网站: http://stackvoid.com/ 网上非常多关于Android事件分发机制的解释,大多数描写叙述的都不够清晰,没有吧来龙去脉搞清晰,本文将带你从Touch事件产生到Touch事件被消费这一全过程作全面的剖析. 产生Touch事件 这部分牵扯到硬件和Linux内核部分:我们简单讲述一下这部分内容,假设有兴趣的话能够參考这篇文章. 传递Touch事件 触摸事件是由Linux内核的一个Input子系统来管理的(InputManager),Linux子系统会在

[转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)

Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客开始,就零零散散在好多地方使用到了Android事件分发的知识. 也有好多朋友问过我各种问题,比如:onTouch和onTouchEvent有什么区别,又该如何使用?为什么给ListView引入了一个滑动菜单的 功能,ListView就不能滚动了?为什么图片轮播器里的图