zoukankan      html  css  js  c++  java
  • Android的进阶学习(六)--理解View事件分发

    http://www.jianshu.com/p/34cb396104a7

    有些无奈,期末考试抱佛脚,还好没有挂,现在继续进阶。

    好久以前就看到了View的事件分发,但是当时功底不够,源码也不敢深究,也就是个模模糊糊过了,现在在看一面,才发现以前许多理解都是错的,也怪不得当时自己都没有真正弄清楚。


    理解之前

    首先我们应该明白的是,当我们一个触摸事件来的时候,它是被包装成的一个MotionEvent,其中就包含了这个事件是 downmoveup其中的一种,还有这个触摸发生的地点(也就是坐标)等等。
    其次,我们还需要知道的是,每一次的触摸事件都是最先把MotionEvent发送到ActivitydispatchTouchEvent方法中的。
    有这两点基础,我们就可以去探索源码了。

    源码探索

    既然我们现在已经知道了,一个触摸事件最先就是包装成一个MotionEvent给发送到ActivitydispatchTounchEvent了,那么我们当然从这个方法看起走呀。

    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所对应的WindowsuperDispatchTouchEvent(ev)方法来进行事件的分发。然后我们接着寻找这个方法,在Window这个抽象类中发现了这个抽象方法superDispatchTouchEvent(ev),有这个方法明我们也可以看出来,这里是调用的Window的实现类的方法啦。
    于是我们就可以找到这个Window的唯一实现类PhoneWindow,在这个类中,我们找到了superDispatchTouchEvent(ev)方法。在这个方法中,也是相当的简单,就直接调用了mDecor.superDispatchTouchEvent,也就是这句话,我们的事件终于传到了View了。对,这里的mDecor就是我们ActivitysetContent中所设置的View的父容器,也就是顶级容器了。

    看到了这里,才真正的开始进行View的事件分发了,不过再之前,还是先理一下,以便后面好理解。

    1. MotionEvent现在是传到Activity的顶级View的,我们的事件分发就是从这个顶级View向它的子View进行分发的。
    2. 顶级View所包含的子View,子View中又包含子View,形成一个View树。
    3. 事件分发就是把事件(MotionEvent) 按照先序遍历所有节点,直到找到一个View消费掉这个事件。所谓的消费这个事件,就是相应的ViewOntouchListener返回true或者OntouchEvent()返回为true
    4. 事件分发主要由三个函数控制,分别是dispatchTouchEvent分发事件,onInterceptTouchEvent拦截事件,onTouchEvent响应事件。

    View的事件分发.png

    深入分发

    事件传到顶级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中,会确定触摸事件是否为downmFirstTouchTarget是不是为空。其中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

    接着我们再考虑一种情况,当我们的触摸事件不为downmFirstTouchTarget != 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的事件分发了,当然,当一MotionEventViewGroup传到了View的时候,对应的就相当简单了,因为View并没有子View,而单纯的是对于MotionEvent事件的消费----OntouchListenerOnTouchEvent的返回值而已,不过值得注意的是OntouchListener的优先级比OnTouchEvent,这点从源码中很轻松就能发现。

    总结


    View的事件分发 (1).png

    最后,还是通过这一张相同的图进行总结一下。

    触摸事件最初是由Activity传给Window再传到顶级View mDercorView中,也就是这里的树根,然后按照前序遍历,把触摸事件向下传。当事件传到了ViewGroup1的时候,就会遍历它下面的三个子View,当这三个子View都没有消费这个事件的时候,就会调用ViewGruop1的父类View去试着消费这个事件,要是还是没有被消费,则ViewGroup2就会重复ViewGroup1,当然,如果ViewGroup2也没消费掉事件(包括它的子View),ViewGroup3还是会继续重复。要是这三个ViewGroup都没有消费掉的话,则又会传到ViewGroup0的父View去试着消费,如果也没有消费掉,最后就会传到Activity中进行消费。



    文/MathiasLuo(简书作者)
    原文链接:http://www.jianshu.com/p/34cb396104a7
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 相关阅读:
    MPLS TE 配置与各大属性调整
    Net学习日记_基础提高_9
    Net学习日记_基础提高_8
    Net学习日记_基础提高_7
    Net学习日记_基础提高_6
    Net学习日记_基础提高_5
    Net学习日记_基础提高_4
    Net学习日记_基础提高_3
    Net学习日记_基础提高_2
    Net学习日记_基础提高_1
  • 原文地址:https://www.cnblogs.com/feng9exe/p/5712337.html
Copyright © 2011-2022 走看看