Android的进阶学习(六)--理解View事件分发
http://www.jianshu.com/p/34cb396104a7
有些无奈,期末考试抱佛脚,还好没有挂,现在继续进阶。
好久以前就看到了View的事件分发,但是当时功底不够,源码也不敢深究,也就是个模模糊糊过了,现在在看一面,才发现以前许多理解都是错的,也怪不得当时自己都没有真正弄清楚。
理解之前
首先我们应该明白的是,当我们一个触摸事件来的时候,它是被包装成的一个MotionEvent,其中就包含了这个事件是 down,move,up其中的一种,还有这个触摸发生的地点(也就是坐标)等等。
其次,我们还需要知道的是,每一次的触摸事件都是最先把MotionEvent发送到Activity的dispatchTouchEvent方法中的。
有这两点基础,我们就可以去探索源码了。
源码探索
既然我们现在已经知道了,一个触摸事件最先就是包装成一个MotionEvent给发送到Activity的dispatchTounchEvent了,那么我们当然从这个方法看起走呀。
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
在这个方法中,传递了一个MotionEvent作为参数,也就是我们的触摸事件传递给了这个方法。然后进行了一点简单的逻辑,首先判断一下MotionEvent是否为down,如果是的话就调用 onUserInteraction()。而onUserInteraction()就是一个空方法,目的就是实现这个方法,可以更加方便管理一些notfication。
public void onUserInteraction() { }
所以和我们的事件分发并没有很大的关系,重要的是下面的几句。
这里调用了Activity所对应的Window的superDispatchTouchEvent(ev)方法来进行事件的分发。然后我们接着寻找这个方法,在Window这个抽象类中发现了这个抽象方法superDispatchTouchEvent(ev),有这个方法明我们也可以看出来,这里是调用的Window的实现类的方法啦。
于是我们就可以找到这个Window的唯一实现类PhoneWindow,在这个类中,我们找到了superDispatchTouchEvent(ev)方法。在这个方法中,也是相当的简单,就直接调用了mDecor.superDispatchTouchEvent,也就是这句话,我们的事件终于传到了View了。对,这里的mDecor就是我们Activity中setContent中所设置的View的父容器,也就是顶级容器了。
看到了这里,才真正的开始进行View的事件分发了,不过再之前,还是先理一下,以便后面好理解。
MotionEvent现在是传到Activity的顶级View的,我们的事件分发就是从这个顶级View向它的子View进行分发的。- 顶级
View所包含的子View,子View中又包含子View,形成一个View树。 - 事件分发就是把事件(
MotionEvent) 按照先序遍历所有节点,直到找到一个View消费掉这个事件。所谓的消费这个事件,就是相应的View的OntouchListener返回true或者OntouchEvent()返回为true。 - 事件分发主要由三个函数控制,分别是
dispatchTouchEvent分发事件,onInterceptTouchEvent拦截事件,onTouchEvent响应事件。
深入分发
事件传到顶级View(ViewGroup)中时,就会调用dispatchTouchEvent进行分发。
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
// Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
}
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action);
// restore action in case it was changed
} else {
intercepted = false;
}
}
else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
}
看上面的dispatchTouchEvent的逻辑也是很好理解的,首先会判断我们传来的TouchEvent是不是down,如果是的话,就会调用resetTouchSate方法,不过现在我们暂时不需要知道这个方法的具体作用,但是从方法名中我们也能得到一些提示,也就是每当遇到down就会重新设置一些状态。
然后,这里就会判断是否需要调用onInterceptTouchEvent方法,也就是注释中的 Check for interception。值得注意的是这里是两层判断,也就是有两个嵌套的if。
在第一个if中,会确定触摸事件是否为down和mFirstTouchTarget是不是为空。其中mFirstTouchTarget表示的是事件是不是又子View消费了的,如果已经被消费,就不会为null。在第二个if中就会判断是否设置了FLAG_DISALLOW_INTERCEPT这个 标记符,这个FLAG_DISALLOW_INTERCEPT标记符的作用就是子View干涉父容器对事件的分发。如果子View设置了这个标记符,就不会调用onInterceptTouchEvent方法,从而intercepted为false。
如果两层if都满足,就会调用onInterceptTouchEvent来对事件进行拦截。
接下来,我们就看看如果父容器不拦截,即intercepted为false。
if (!canceled && !intercepted) {
// If the event is targeting accessiiblity focus we give it to the
// view that has accessibility focus and if it does not handle it
// we clear the flag and dispatch the event to all children as usual.
// We are looking up the accessibility focused host to avoid keeping
// state since these events are very rare.
View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() ? findChildWithAccessibilityFocus() : null;
if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
final int actionIndex = ev.getActionIndex();
// always 0 for down
final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) : TouchTarget.ALL_POINTER_IDS;
// Clean up earlier touch targets for this pointer id in case they
// have become out of sync.
removePointersFromTouchTargets(idBitsToAssign);
final int childrenCount = mChildrenCount;
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
// Find a child that can receive the event.
// Scan children from front to back.
final ArrayList<View> preorderedList =buildOrderedChildList();
final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
final View child = (preorderedList == null) ? children[childIndex] : preorderedList.get(childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
newTouchTarget = getTouchTarget(child);
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
resetCancelNextUpFlag(child);
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
// The accessibility focus didn't handle the event, so clear
// the flag and do a normal dispatch to all children.
ev.setTargetAccessibilityFocus(false);
}
if (preorderedList != null) preorderedList.clear();
}
if (newTouchTarget == null && mFirstTouchTarget != null) {
// Did not find a child to receive the event.
// Assign the pointer to the least recently added target.
newTouchTarget = mFirstTouchTarget;
while (newTouchTarget.next != null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}}
代码有点多,不过抓重点看的话也就那几行。
这里主要是有一个for循环,对子View进行了遍历,然后判断是否能够接受触摸事件,可以接受的话就会调用dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)把MotionEvent给传给子View,这个方法的返回值就是表示是否消费了该事件,也就是OnTouchListener或者OntouchEvent是否返回了true。
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, View child, int desiredPointerIdBits) {
final boolean handled;
// Canceling motions is a special case. We don't need to perform any transformations
// or filtering. The important part is the action, not the contents.
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}
可以看到,这里dispatchTransformedTouchEvent就会让子View重复父容器类似的分发方式。
如果有子View消费的话就会跳出for循环,并且在addTouchTarget(child, idBitsToAssign);方法中给前面所说的mFirstTouchTarget赋值。
要是没有View消费该事件或者父容器拦截该事件的话,
// Dispatch to touch targets.
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);
}
可以看到,会调用一个和上面一样的方法,只是参数不同而已,因为第三个参数传的是null,所以就会调用super.dispatchTouchEvent(event)方法,这里需要注意的是,这里的super不是父容器,而是指的是本身ViewGroup的父类View的方法,其对象还是这个 ViewGroup。
接着我们再考虑一种情况,当我们的触摸事件不为down且mFirstTouchTarget != null的话,就会直接在我们TouchTarget中分发了,也就是 mFirstTouchTarget所保存中进行分发。
// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it.Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)) {
handled = true;
}
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}}
首先我们要知道的是,TouchTarget是一种单链表结构,保存了每一次我们不拦截所分发的View,所以满足上述情况的时候,就会遍历这个链表进行分发。
上面的所有基本上就是View的事件分发了,当然,当一MotionEvent从ViewGroup传到了View的时候,对应的就相当简单了,因为View并没有子View,而单纯的是对于MotionEvent事件的消费----OntouchListener和OnTouchEvent的返回值而已,不过值得注意的是OntouchListener的优先级比OnTouchEvent,这点从源码中很轻松就能发现。
总结
最后,还是通过这一张相同的图进行总结一下。
触摸事件最初是由Activity传给Window再传到顶级View mDercorView中,也就是这里的树根,然后按照前序遍历,把触摸事件向下传。当事件传到了ViewGroup1的时候,就会遍历它下面的三个子View,当这三个子View都没有消费这个事件的时候,就会调用ViewGruop1的父类View去试着消费这个事件,要是还是没有被消费,则ViewGroup2就会重复ViewGroup1,当然,如果ViewGroup2也没消费掉事件(包括它的子View),ViewGroup3还是会继续重复。要是这三个ViewGroup都没有消费掉的话,则又会传到ViewGroup0的父View去试着消费,如果也没有消费掉,最后就会传到Activity中进行消费。
原文链接:http://www.jianshu.com/p/34cb396104a7
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
Android的进阶学习(六)--理解View事件分发的更多相关文章
- Android View事件分发-从源码分析
View事件分发-从源码分析 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/details/78555397?lo ...
- Android高手进阶——Adapter深入理解与优化
Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...
- Android面试必问!View 事件分发机制,看这一篇就够了!
在 Android 开发当中,View 的事件分发机制是一块很重要的知识.不仅在开发当中经常需要用到,面试的时候也经常被问到. 如果你在面试的时候,能把这块讲清楚,对于校招生或者实习生来说,算是一块不 ...
- Android View框架总结(七)View事件分发机制
请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52282833 View布局告一段落,从本篇开始View事件相关分析, ...
- View 事件分发
View 事件分发 学习自 <Android开发艺术探索> 官方文档-MotionEvent 事件分发机制漫谈 View的事件分发机制,使我们了解View的工作原理继而学习如何自定义Vie ...
- Atitit View事件分发机制
1. Atitit View事件分发机制 1. Atitit View事件分发机制1 1.1. 三个关键方法 dispatchTouchEvent onInterceptTouchEvent onTo ...
- 【Android面试查漏补缺】之事件分发机制详解
前言 查漏补缺,查漏补缺,你不知道哪里漏了,怎么补缺呢?本文属于[Android面试查漏补缺]系列文章第一篇,持续更新中,感兴趣的朋友可以[关注+收藏]哦~ 本系列文章是对自己的前段时间面试经历的总结 ...
- 谈谈我对Android View事件分发的理解
写这篇博客的缘由.近期因为项目中用到相似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件.点击TextView能够触发该 ...
- Android View 事件分发机制详解
想必很多android开发者都遇到过手势冲突的情况,我们一般都是通过内部拦截和外部拦截法解决此类问题.要想搞明白原理就必须了解View的分发机制.在此之前我们先来了解一下以下三个非常重要的方法: di ...
随机推荐
- 手撕coreML之yolov2 object detection物体检测(含源代码)
一些闲话: 前面我有篇博客 https://www.cnblogs.com/riddick/p/10434339.html ,大致说了下如何将pytorch训练的.pth模型转换为mlmodel,部署 ...
- C# DataGrid 用法---极速入门测试
目标: 新手编程,只求DataGrid能运行起来,更多功能留在后面探讨. 步骤: 1.新建WPF文档 插入DataGrid控件. <Window x:Class="OASevl.Mai ...
- 深入浅出 JVM ClassLoader
# 前言 在 JVM 综述里面,我们说,JVM 做了三件事情,Java 程序的内存管理, Java Class 二进制字节流的加载(ClassLoader),Java 程序的执行(执行引擎).我们也说 ...
- [转]Using Angular in Visual Studio Code
本文转自:https://code.visualstudio.com/docs/nodejs/angular-tutorial Using Angular in Visual Studio Code ...
- Asp.net Core 打包发布 (Linux+Nginx)
如果你觉得如下这些文章对你有帮助,请点击链接支持作者原创 http://www.cnblogs.com/savorboard/ .Net Core SDK 命令介绍 前言 本篇主要介绍 asp.n ...
- Matlab 根号的输入
二次根号: sqrt(a)或a^0.5 三次根号: x^(1/3)或者x.^(1/3) 根据x的数据结构类型 矩阵.数组需要.^
- Linux常用基本命令:三剑客命令之-awk数组用法
AWK的数组用法跟javascript类似. 1,定义数组 awk 'BEGIN{a[0]="zhangsan";a[1]="lisi";print a[0]} ...
- Code::Blocks + GDAL
[root@server20 ~]# yum install cmake cmake-gui [root@server20 ~]# yum install cairo-devel libcurl-de ...
- 机器学习算法(SVM)公开课4月25日开讲
从深蓝到AlphaGo,聪明的人工智能一再“羞辱”人类大脑: 指纹识别.以图搜图.语音助手.无人驾驶···生活里它无孔不入 离不开智能手机的我们,是否已开始被人工智能的“奴役”? 或许,你不需要会运用 ...
- Android开发常用的一些功能列表(转)
文章来源:http://www.cnblogs.com/netsql/archive/2013/03/02/2939828.html 1.软件自动更新下载,并提示 2.软件登录注册,以及状态保存 3. ...