Android触摸事件的传递(七)-ViewGroup

了解更多,移步Android触摸事件传递机制系列详解
Android触摸事件的传递(六)-ViewRootImpl-->Activity
1 activity的dispatchTouchEvent方法 activityDialog都是Callback接口的具体实现,主要看activitydispatchTouchEvent方法,

public boolean dispatchTouchEvent(MotionEvent ev) { ??? if (getWindow().superDispatchTouchEvent(ev)) { return true; } return onTouchEvent(ev); }

首先调用PhoneWindowsuperDispatchTouchEvent方法,如果未处理才继续调用onTouchEvent方法
2 DecorView的superDispatchTouchEvent WindowsuperDispatchTouchEvent是个抽象方法
public abstract boolean superDispatchTouchEvent(MotionEvent event);

找到window的实现类---PhoneWindow(wingdow的唯一实现)
publicboolean superDispatchTouchEvent(MotionEvent event){ return mDecor.superDispatchTouchEvent(event); };

直接调用DecorView的superDispatchTouchEvent
3 ViewGoup--dispatchTouchEvent
  • DecorView继承自FrameLayout且是父view,所以事件最终传递给了ViewGroup
  • DecorView一般就是当前底层容器(即setContentView所设置的View的父容器)
  • 通过Activity.getWindow().getDecorView();获得
    通过下面可以获得activity所设置的viewsetContentView设置的view是其一个子view
((ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);

public boolean dispatchTouchEvent(MotionEvent ev) { //验证事件是否连续 if (mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onTouchEvent(ev, 1); } //这个变量用于记录事件是否被处理完 boolean handled = false; //过滤掉一些不合法的事件:当前的View的窗口被遮挡了。 if (onFilterTouchEventForSecurity(ev)) { //如果事件发生的View在的窗口,没有被遮挡 final int action = ev.getAction(); //重置前面为0 ,只留下后八位,用于判断相等时候,可以提高性能。 final int actionMasked = action & MotionEvent.ACTION_MASK; //判断是不是Down事件,如果是的话,就要做初始化操作 if (actionMasked == MotionEvent.ACTION_DOWN) { //如果是down事件,就要清空掉之前的状态,比如,重置手势判断什么的。 //比如,之前正在判断是不是一个单点的滑动,但是第二个down来了,就表示,不可能是单点的滑动,要重新开始判断触摸的手势 //清空掉mFirstTouchTarget // 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(); } //检查是否拦截事件 final boolean intercepted; //如果当前是Down事件,或者已经有处理Touch事件的目标了 if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) { //判断允不允许这个View拦截 //使用与运算作为判断,可以让我们在flag中,存储好几个标志 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; //如果说允许拦截事件 if (!disallowIntercept) { //确定是不是拦截了 intercepted = onInterceptTouchEvent(ev); //重新恢复Action,以免action在上面的步骤被人为地改变了 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. //如果说,事件已经初始化过了,并且没有子View被分配处理,那么就说明,这个ViewGroup已经拦截了这个事件 intercepted = true; } // Check for cancelation. //如果viewFlag被设置了PFLAG_CANCEL_NEXT_UP_EVENT ,那么就表示,下一步应该是Cancel事件 //或者如果当前的Action为取消,那么当前事件应该就是取消了。 final boolean canceled = resetCancelNextUpFlag(this) || actionMasked == MotionEvent.ACTION_CANCEL; // Update list of touch targets for pointer down, if needed. //如果需要(不是取消,也没有被拦截)的话,那么在触摸down事件的时候更新触摸目标列表 //split代表,当前的ViewGroup是不是支持分割MotionEvent到不同的View当中 final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; //新的触摸对象, TouchTarget newTouchTarget = null; //是否把事件分配给了新的触摸 boolean alreadyDispatchedToNewTouchTarget = false; //事件不是取消事件,也没有拦截那么就要判断 if (!canceled && !intercepted) { //如果是个全新的Down事件 //或者是有新的触摸点 //或者是光标来回移动事件(不太明白什么时候发生) if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { //这个事件的索引,也就是第几个事件,如果是down事件就是0 final int actionIndex = ev.getActionIndex(); // always 0 for down //获取分配的ID的bit数量 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; //如果新的触摸对象为null(这个不是铁定的吗)并且当前ViewGroup有子元素 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 View[] children = mChildren; //是否使用自定义的顺序来添加控件 final boolean customOrder = isChildrenDrawingOrderEnabled(); for (int i = childrenCount - 1; i >= 0; i--) { //如果是用了自定义的顺序来添加控件,那么绘制的View的顺序和mChildren的顺序是不一样的 //所以要根据getChildDrawingOrder取出真正的绘制的View //自定义的绘制,可能第一个会画到第三个,和第四个,第二个画到第一个,这样里面的内容和Children是不一样的 final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; final View child = children[childIndex]; //如果child不可以接收这个触摸的事件,或者触摸事件发生的位置不在这个View的范围内 if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { continue; } //获取新的触摸对象,如果当前的子View在之前的触摸目标的列表当中就返回touchTarget //子View不在之前的触摸目标列表那么就返回null 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. //如果新的触摸目标对象不为空,那么就把这个触摸的ID赋予它,这样子, //这个触摸的目标对象的id就含有了好几个pointer的ID了 newTouchTarget.pointerIdBits |= idBitsToAssign; break; } //如果子View不在之前的触摸目标列表中,先重置childView的标志,去除掉CACEL的标志 resetCancelNextUpFlag(child); //调用子View的dispatchTouchEvent,并且把pointer的id 赋予进去 //如果说,子View接收并且处理了这个事件,那么就更新上一次触摸事件的信息, //并且为创建一个新的触摸目标对象,并且绑定这个子View和Pointer的ID if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { // Child wants to receive touch within its bounds. mLastTouchDownTime = ev.getDownTime(); mLastTouchDownIndex = childIndex; mLastTouchDownX = ev.getX(); mLastTouchDownY = ev.getY(); //这里给mFirstTouchTarget赋值 newTouchTarget = addTouchTarget(child, idBitsToAssign); alreadyDispatchedToNewTouchTarget = true; break; } } } //如果newTouchTarget为null,就代表,这个事件没有找到子View去处理它, //那么,如果之前已经有了触摸对象(比如,我点了一张图,另一个手指在外面图的外面点下去) //那么就把这个之前那个触摸目标定为第一个触摸对象,并且把这个触摸(pointer)分配给最近添加的触摸目标 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; } } } // Dispatch to touch targets. //如果没有触摸目标 if (mFirstTouchTarget == null) { // No touch targets so treat this as an ordinary view. //那么就表示我们要自己在这个ViewGroup处理这个触摸事件了 handled = dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS); } else { // 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; //遍历TouchTargt树.分发事件,如果我们已经分发给了新的TouchTarget那么我们就不再分发给newTouchTarget while (target != null) { final TouchTarget next = target.next; if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { handled = true; } else { //是否让child取消处理事件,如果为true,就会分发给child一个ACTION_CANCEL事件 final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted; //派发事件 if (dispatchTransformedTouchEvent(ev, cancelChild, target.child, target.pointerIdBits)) { handled = true; } //cancelChild也就是说,派发给了当前child一个ACTION_CANCEL事件, //那么就移除这个child if (cancelChild) { //没有父节点,也就是当前是第一个TouchTarget //那么就把头去掉 if (predecessor == null) { mFirstTouchTarget = next; } else { //把下一个赋予父节点的上一个,这样当前节点就被丢弃了 predecessor.next = next; } //回收内存 target.recycle(); //把下一个赋予现在 target = next; //下面的两行不执行了,因为我们已经做了链表的操作了。 //主要是我们不能执行predecessor=target,因为删除本节点的话,父节点还是父节点 continue; } } //如果没有删除本节点,那么下一轮父节点就是当前节点,下一个节点也是下一轮的当前节点 predecessor = target; target = next; } } // Update list of touch targets for pointer up or cancel, if needed. //遇到了取消事件、或者是单点触摸下情况下手指离开,我们就要更新触摸的状态 if (canceled || actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { resetTouchState(); } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { //如果是多点触摸下的手指抬起事件,就要根据idBit从TouchTarget中移除掉对应的Pointer(触摸点) final int actionIndex = ev.getActionIndex(); final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); removePointersFromTouchTargets(idBitsToRemove); } } if (!handled && mInputEventConsistencyVerifier != null) { mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); } return handled; }

3.1 解析一:检查是否拦截事件
  • viewgroup会在两种情况下会判断是否要拦截当前事件:事件类型为ACTION_DOWN或者 mFirstTouchTarget != null
  • 当事件交由viewgroup的子元素处理时,mFirstTouchTarget被赋值并指向子元素,事件传递给子元素时才不为null。
通常
  • ACTION_DOWN的时候mFirstTouchTarget 还是null
  • ACTION_DOWN后面的事件mFirstTouchTarget不为null
  • ACTION_DOWN后面的事件mFirstTouchTargetnullviewgroup默认拦截 。
  • onInterceptTouchEvent默认返回false,viewgroup`默认不拦截
//检查是否拦截事件 final boolean intercepted; //如果当前是Down事件,或者已经有处理Touch事件的目标了 if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null) { //判断允不允许这个View拦截 //使用与运算作为判断,可以让我们在flag中,存储好几个标志 final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; //如果说允许拦截事件 if (!disallowIntercept) { //确定是不是拦截了 intercepted = onInterceptTouchEvent(ev); //重新恢复Action,以免action在上面的步骤被人为地改变了 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. //如果说,事件已经初始化过了,并且没有子View被分配处理,那么就说明,这个ViewGroup已经拦截了这个事件 intercepted = true; }

onInterceptTouchEvent默认返回false
public boolean onInterceptTouchEvent(MotionEvent ev) { return false; }

3.2 检查子view并传递事件
  1. 判断事件不是取消事件,也没有拦截if (!canceled && !intercepted) {***}
  2. 判断child可不可以接收这个触摸的事件,或者触摸事件发生的位置在不在这个View的范围内
  3. 条件通过该子view 则赋值给newTouchTarget
  4. dispatchTransformedTouchEvent调用子View的dispatchTouchEvent,并且把pointer的id 赋予进去.如果说,子View接收并且处理了这个事件。如果返回true,则 alreadyDispatchedToNewTouchTarget = true; //已经消费事件并跳出循环。否则继续遍历循环。
//事件不是取消事件,也没有拦截那么就要判断 if (!canceled && !intercepted) { //如果是个全新的Down事件 //或者是有新的触摸点 //或者是光标来回移动事件(不太明白什么时候发生) if (actionMasked == MotionEvent.ACTION_DOWN || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { //这个事件的索引,也就是第几个事件,如果是down事件就是0 final int actionIndex = ev.getActionIndex(); // always 0 for down //获取分配的ID的bit数量 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; //如果新的触摸对象为null(这个不是铁定的吗)并且当前ViewGroup有子元素 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 View[] children = mChildren; //是否使用自定义的顺序来添加控件 final boolean customOrder = isChildrenDrawingOrderEnabled(); for (int i = childrenCount - 1; i >= 0; i--) { //如果是用了自定义的顺序来添加控件,那么绘制的View的顺序和mChildren的顺序是不一样的 //所以要根据getChildDrawingOrder取出真正的绘制的View //自定义的绘制,可能第一个会画到第三个,和第四个,第二个画到第一个,这样里面的内容和Children是不一样的 final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; final View child = children[childIndex]; //如果child不可以接收这个触摸的事件,或者触摸事件发生的位置不在这个View的范围内 if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { continue; } //获取新的触摸对象,如果当前的子View在之前的触摸目标的列表当中就返回touchTarget //子View不在之前的触摸目标列表那么就返回null 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. //如果新的触摸目标对象不为空,那么就把这个触摸的ID赋予它,这样子, //这个触摸的目标对象的id就含有了好几个pointer的ID了 newTouchTarget.pointerIdBits |= idBitsToAssign; break; } //如果子View不在之前的触摸目标列表中,先重置childView的标志,去除掉CACEL的标志 resetCancelNextUpFlag(child); //调用子View的dispatchTouchEvent,并且把pointer的id 赋予进去 //如果说,子View接收并且处理了这个事件,那么就更新上一次触摸事件的信息, //并且为创建一个新的触摸目标对象,并且绑定这个子View和Pointer的ID if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { // Child wants to receive touch within its bounds. mLastTouchDownTime = ev.getDownTime(); mLastTouchDownIndex = childIndex; mLastTouchDownX = ev.getX(); mLastTouchDownY = ev.getY(); //这里给mFirstTouchTarget赋值 newTouchTarget = addTouchTarget(child, idBitsToAssign); alreadyDispatchedToNewTouchTarget = true; break; } } } //如果newTouchTarget为null,就代表,这个事件没有找到子View去处理它, //那么,如果之前已经有了触摸对象(比如,我点了一张图,另一个手指在外面图的外面点下去) //那么就把这个之前那个触摸目标定为第一个触摸对象,并且把这个触摸(pointer)分配给最近添加的触摸目标 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; } } }

3.3 自己处理事件
  1. 如果没有触摸目标那么就表示我们要自己在这个ViewGroup处理这个触摸事件了
    handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);
  • 注:如果检查子view没有消费掉事件
  1. 如果有 则遍历TouchTargt树分发事件,如果我们已经分发给了新的TouchTarget那么我们就不再分发给newTouchTarget if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {handled = true; }否则是否让child取消处理事件,如果为true,就会分发给child一个ACTION_CANCEL事件
// Dispatch to touch targets. //如果没有触摸目标 if (mFirstTouchTarget == null) { // No touch targets so treat this as an ordinary view. //那么就表示我们要自己在这个ViewGroup处理这个触摸事件了 handled = dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS); } else { // 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; //遍历TouchTargt树.分发事件,如果我们已经分发给了新的TouchTarget那么我们就不再分发给newTouchTarget while (target != null) { final TouchTarget next = target.next; if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { handled = true; } else { //是否让child取消处理事件,如果为true,就会分发给child一个ACTION_CANCEL事件 final boolean cancelChild = resetCancelNextUpFlag(target.child) || intercepted; //派发事件 if (dispatchTransformedTouchEvent(ev, cancelChild, target.child, target.pointerIdBits)) { handled = true; } //cancelChild也就是说,派发给了当前child一个ACTION_CANCEL事件, //那么就移除这个child if (cancelChild) { //没有父节点,也就是当前是第一个TouchTarget //那么就把头去掉 if (predecessor == null) { mFirstTouchTarget = next; } else { //把下一个赋予父节点的上一个,这样当前节点就被丢弃了 predecessor.next = next; } //回收内存 target.recycle(); //把下一个赋予现在 target = next; //下面的两行不执行了,因为我们已经做了链表的操作了。 //主要是我们不能执行predecessor=target,因为删除本节点的话,父节点还是父节点 continue; } } //如果没有删除本节点,那么下一轮父节点就是当前节点,下一个节点也是下一轮的当前节点 predecessor = target; target = next; } }

3.4 dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,View child, int desiredPointerIdBits)
  • 通过以上代码可知
  1. 对于目标子view调用如dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)
  2. ViewGroup自己处理事件调用 dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);
  3. 当让child取消处理事件,如果为true,就会分发给child一个ACTION_CANCEL事件。调用dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits)
    可以看出 在ViewGroup自己处理时child为null。其他child不为null。
    dispatchTransformedTouchEvent
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; }// Calculate the number of pointers to deliver. final int oldPointerIdBits = event.getPointerIdBits(); final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; // If for some reason we ended up in an inconsistent state where it looks like we // might produce a motion event with no pointers in it, then drop the event. if (newPointerIdBits == 0) { return false; }// If the number of pointers is the same and we don't need to perform any fancy // irreversible transformations, then we can reuse the motion event for this // dispatch as long as we are careful to revert any changes we make. // Otherwise we need to make a copy. final MotionEvent transformedEvent; if (newPointerIdBits == oldPointerIdBits) { if (child == null || child.hasIdentityMatrix()) { if (child == null) { handled = super.dispatchTouchEvent(event); } else { final float offsetX = mScrollX - child.mLeft; final float offsetY = mScrollY - child.mTop; event.offsetLocation(offsetX, offsetY); handled = child.dispatchTouchEvent(event); event.offsetLocation(-offsetX, -offsetY); } return handled; } transformedEvent = MotionEvent.obtain(event); } else { transformedEvent = event.split(newPointerIdBits); }// Perform any necessary transformations and dispatch. if (child == null) { handled = super.dispatchTouchEvent(transformedEvent); } else { final float offsetX = mScrollX - child.mLeft; final float offsetY = mScrollY - child.mTop; transformedEvent.offsetLocation(offsetX, offsetY); if (! child.hasIdentityMatrix()) { transformedEvent.transform(child.getInverseMatrix()); }handled = child.dispatchTouchEvent(transformedEvent); }// Done. transformedEvent.recycle(); return handled; }

  • 通过查看dispatchTransformedTouchEvent可以看出当childnull时调用super.dispatchTouchEvent(event); child不为null时调用child.dispatchTouchEvent(event)
  • 由此可以看出ViewGroup或子view自身真正处理事件时都是通过View的dispatchTouchEvent分发下去的。
参考 【Android触摸事件的传递(七)-ViewGroup】View的事件分发机制——dispatchTouchEvent详解

    推荐阅读