[Android]事件分发流程全解析(一)事件分发源码分析

参考资料

事件分发流程全解析(一)事件分发源码分析
事件分发流程全解析(二)事件分发源码分析

前言

  • 事件分发流程是我们解决页面复杂交互的基础,只要我们做的交互稍微复杂一些,就有可能用到事件分发和拦截,如果不能掌握,那就很难做好相关工作
  • 老张我做面试官也有些年头了,事件分发可以说是必问的知识点,但是很少有候选人能全面掌握,总是欠缺点什么
  • 关于事件分发的博客网上有很多,有一些重点在源码解析而缺少过程,有些重点在分发流程的分析而缺少代码分析,很多人看过后仍云山雾罩,不知其所以,本文期望通过源码和流程的分析,能够将事件分发讲明白一些

场景:

假设有一个Activity,它布局中有一个FrameLayout,FrameLayout有三个子FrameLayout,三个子FrameLayout又分别有三个子View,其结构如图所示:


image.png

我们用蓝色小圆代表dispatchTouchEvent的执行,红色小圆代表onInterceptTouchEvent的执行,黄色小圆代表onTouchEvent的执行。

事件分发流程

DOWN事件没有消费者的情况

DOWN事件的传递

DOWN时间可以说是事件分发过程中最重要的事件之一,它决定了这个事件是否被消费、事件传递链条的建立等等

首先假设上面的场景中没有任何View拦截事件,也没有任何View消费事件
下面看一下DOWN事件的分发过程:
我们从事件分发的起点Activity#dispatchTouchEvent开始说起:

public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

代码很简单,直接执行了Window#superDispatchTouchEvent,注意,这里的Window是且只能值PhoneWindow,跟下去:

    @Override
    public boolean superDispatchTouchEvent(MotionEvent event) {
        return mDecor.superDispatchTouchEvent(event);
    }

这里执行了DecoreView#superDispatchTouchEvent:

    public boolean superDispatchTouchEvent(MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }

decoreView是一个ViewGroup,这里调用的是ViewGroup#dispatchTouchEvent,这里是dispathTouchEvent过程的一个重头戏,我们不在这里分析,只需要知道它在几次执行后会调用到图示中v1的dispatchTouchEvent,具体为什么,看完本篇文章自然会明白。到这里场景中的事件的阶段如下图:


image.png

v1的dispathTouchEvent方法的实现在ViewGroup:

public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
        }

        // If the event targets the accessibility focused view and this is it, start
        // normal event dispatch. Maybe a descendant is what will handle the click.
        if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
            ev.setTargetAccessibilityFocus(false);
        }

        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;
            }

            // If intercepted, start normal event dispatch. Also if there is already
            // a view that is handling the gesture, do normal event dispatch.
            if (intercepted || mFirstTouchTarget != null) {
                ev.setTargetAccessibilityFocus(false);
            }

            // Check for cancelation.
            final boolean canceled = resetCancelNextUpFlag(this)
                    || actionMasked == MotionEvent.ACTION_CANCEL;

            // Update list of touch targets for pointer down, if needed.
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;
            // 不是Cancel事件 且 事件没有被拦截
            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) {
                    // 获取Action的index
                    final int actionIndex = ev.getActionIndex(); // always 0 for down
                    // 获取Action的pointId
                    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 preorderedList = buildTouchDispatchChildList();
                        final boolean customOrder = preorderedList == null
                                && isChildrenDrawingOrderEnabled();
                        final View[] children = mChildren;
                        for (int i = childrenCount - 1; i >= 0; i--) {
                            final int childIndex = getAndVerifyPreorderedIndex(
                                    childrenCount, i, customOrder);
                            final View child = getAndVerifyPreorderedView(
                                    preorderedList, children, 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 a
                            // 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;
                            }
                            // 该子View不能接收事件,或者事件触发点不在子View的坐标范围内
                            if (!canViewReceivePointerEvents(child)
                                    || !isTransformedTouchPointInView(x, y, child, null)) {
                                ev.setTargetAccessibilityFocus(false);
                                continue;
                            }

                            // 获取child的TouchTarget
                            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.
                                // 更新pointerIdBits
                                newTouchTarget.pointerIdBits |= idBitsToAssign;
                                break;
                            }

                            resetCancelNextUpFlag(child);
                            // 分发变换过的TouchEvent
                            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();
                                // 生成TouchTarget
                                newTouchTarget = addTouchTarget(child, idBitsToAssign);
                                // 已经将事件分发给新的touchTarget的标记
                                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;
                    }
                }
            }

            // 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);
            } 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;
                while (target != null) {
                    final TouchTarget next = target.next;
                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                        handled = true;
                    } else {
                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
                                || intercepted;
                        // 如果在事件在本View这里被拦截了, 生成Cancel事件并向子View传递
                        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;
                }
            }

            // 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) {
                final int actionIndex = ev.getActionIndex();
                final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);
                removePointersFromTouchTargets(idBitsToRemove);
            }
        }

        if (!handled && mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
        }
        return handled;
    }

分段解析:
第18行起的这段if,表示如果DOWN事件传递进来,会将之前的保留的传递链条重置(如果有的话),至于传递链条是什么,后面会讲到

第28起的这段if,因为我们目前是DOWN,因此一定是符合actionMasked == MotionEvent.ACTION_DOWN这个条件的,会进入分支内部,在内部,如果本ViewGroup没有被禁止拦截事件,会执行本ViewGroup的onInterceptTouchEvent,ViewGroup的onInterceptTouchEvent方法比较简单,主要作用还是给子类继承来实现各种炫酷的效果,这里不多做分析,看下代码:

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
                && ev.getAction() == MotionEvent.ACTION_DOWN
                && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
                && isOnScrollbarThumb(ev.getX(), ev.getY())) {
            return true;
        }
        return false;
    }

到这里,场景中的事件分发阶段如下图:


image.png

接着分析ViewGroup#dispatchTouchEvent,因为我们假设onInterceptTouchEvent没有拦截事件,因此其返回值intercepted是false,接着看dispatchTouchEvent方法的59行,因为目前是DOWN事件且intercepted为false,代买进入该分支执行, actionIndexidBitsToAssign主要是处理多点触摸,这里我们无需过多关注

看代码的第92行,这里从后向前遍历该VP的子View,我们看看这个分支处理的什么:
第110行,这句话判定该子View是否可以接收事件,且点击事件是否落该子View的范围内,如果两者都满足贼继续执行逻辑,否则继续遍历下一个子View

第117行,因为我们第一次进入,getTouchTarget会返回null,第130行,执行了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();
        // 如果是Cancel事件,则传递cancel
        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);
                    // 执行子View的dispatchTouchEvent
                    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的第45行,执行了子View的dispatchTouchEvent,因为我们是从后向前遍历的子View,因此这个子View一定是场景中的V4.到目前为止,事件分发到了下图的阶段:


image.png

V4也是一个ViewGroup,它的分发会重复上面的过程,在经历上面的阶段之后,调用到了V13的dispatchTouchEvent:


image.png

V13是一个View,它的dispatchTouchEvent如下:

public boolean dispatchTouchEvent(MotionEvent event) {
        // If the event should be handled by accessibility focus first.
        if (event.isTargetAccessibilityFocus()) {
            // We don't have focus or no virtual descendant has it, do not handle the event.
            if (!isAccessibilityFocusedViewOrHost()) {
                return false;
            }
            // We have focus and got the event, then use normal event dispatch.
            event.setTargetAccessibilityFocus(false);
        }

        boolean result = false;

        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(event, 0);
        }

        final int actionMasked = event.getActionMasked();
        if (actionMasked == MotionEvent.ACTION_DOWN) {
            // Defensive cleanup for new gesture
            stopNestedScroll();
        }

        if (onFilterTouchEventForSecurity(event)) {
            if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
                result = true;
            }
            //noinspection SimplifiableIfStatement
            ListenerInfo li = mListenerInfo;
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

            if (!result && onTouchEvent(event)) {
                result = true;
            }
        }

        if (!result && mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
        }

        // Clean up after nested scrolls if this is the end of a gesture;
        // also cancel it if we tried an ACTION_DOWN but we didn't want the rest
        // of the gesture.
        if (actionMasked == MotionEvent.ACTION_UP ||
                actionMasked == MotionEvent.ACTION_CANCEL ||
                (actionMasked == MotionEvent.ACTION_DOWN && !result)) {
            stopNestedScroll();
        }

        return result;
    }

View#dispatchTouchEvent的第30行,是View的onTouchListener的执行,从这部分逻辑可以看出,如果onTouchListener#onTouch返回了true,则后面的onTouchEvent就不再执行了,如果不是,则会执行View#onTouchEvent

View#onTouchEvent的具体实现和本文关系不大,就不贴代码了,它的默认返回值是false.值得注意的是,View#setOnClickListener的回调是在onTouchEvent的UP事件中触发的

面试的时候经常有面试官会问onTouch onTouchEvent onClick的关系,读到这里我想大家就了然了

至此,我们场景中的事件分发阶段走到了如下所示:


image.png

言归正传接着看View#dispatchTouchEvent:
View#onTouchEvent的返回值false决定了View#dispatchTouchEvent的返回值也为false,View#dispatchTouchEvent的返回值false决定了ViewGroup#dispatchTransformedTouchEvent的返回值为false,
我们的调用流程回到了v4的子View遍历中,也就是ViewGroup#dispatchTouchEvent第92行开始的那部分逻辑继续执行循环,因为ViewGroup#dispatchTransformedTouchEvent为false,因此129后面那部分是不会执行的,继续循环会执行v12的dispatchTouchEvent和onTouchEvent以及v13的dispatchTouchEvent和onTouchEvent
这时场景中的事件分发阶段如下图:


image.png

此时V4的ViewGroup#dispatchTouchEvent中的子View遍历的逻辑执行完毕,逻辑来到了172行,这个mFirstTouchTarget的赋值是在129的if为true的情况,因为129行的判断为false,因此mFirstTouchTarget没有被赋值,这里会进入if分支执行174行执行V4的ViewGroup#dispatchTransformedTouchEvent,该参数child传值为null,在ViewGroup#dispatchTransformedTouchEvent中会调用V4的super.dispatchTouchEvent,V4的super就是View,因此会调用View#dispatchTouchEvent和View#onTouchEvent

此时,场景中的事件分发阶段如下图:

image.png

看到这里我想大家应该都看明白了,这不就是变相的多叉树的后序遍历么!!!! 后面的流程就不一一分析了,我们用数字来标记各个方法的执行顺序:


image.png

这时候有朋友会说,光凭一张嘴我怎么相信你说的就是对的呢,万一你在信口开河,那我不上了鬼子的大当了么?俗话说 talking is cheap show me your code , 没图你说个** 我们就写个demo验证一下:

public class MotionEventActivity extends AppCompatActivity {

    private RelativeLayout contentLayout;
    private boolean onTouchEvent = false;
    private boolean intercept = false;
    private String viewName = "MotionEventActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motion_event);
        contentLayout = findViewById(R.id.contentLayout);
        addView();
    }

    private void addView() {
        MotionEventLayout view1 = new MotionEventLayout(this);
        view1.setViewName("view1");
        contentLayout.addView(view1);
        view1.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        MotionEventLayout view2 = new MotionEventLayout(this);
        view2.setViewName("view2");

        MotionEventLayout view3 = new MotionEventLayout(this);
        view3.setViewName("view3");

        MotionEventLayout view4 = new MotionEventLayout(this);
        view4.setViewName("view4");
        view1.addView(view2);
        view2.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view1.addView(view3);
        view3.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view1.addView(view4);
        view4.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        MotionEventView view5 = new MotionEventView(this);
        view5.setViewName("view5");

        MotionEventView view6 = new MotionEventView(this);
        view6.setViewName("view6");

        MotionEventView view7 = new MotionEventView(this);
        view7.setViewName("view7");
        view2.addView(view5);
        view5.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view2.addView(view6);
        view6.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view2.addView(view7);
        view7.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        MotionEventView view8 = new MotionEventView(this);
        view8.setViewName("view8");

        MotionEventView view9 = new MotionEventView(this);
        view9.setViewName("view9");

        MotionEventView view10 = new MotionEventView(this);
        view10.setViewName("view10");
        view3.addView(view8);
        view8.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view3.addView(view9);
        view9.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view3.addView(view10);
        view10.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        MotionEventView view11 = new MotionEventView(this);
        view11.setViewName("view11");

        MotionEventView view12 = new MotionEventView(this);
        view12.setViewName("view12");

        MotionEventView view13 = new MotionEventView(this);
        view13.setViewName("view13");
        view4.addView(view11);
        view11.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view4.addView(view12);
        view12.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view4.addView(view13);
        view13.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (EventSwitch.LOG_DOWN_EVENT) {
                    Log.d("zyl", viewName + "     dispatchTouchEvent    ACTION_DOWN");
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (EventSwitch.LOG_MOVE_EVENT) {
                    Log.d("zyl", viewName + "        dispatchTouchEvent    ACTION_MOVE");
                }
                break;

            case MotionEvent.ACTION_UP:
                if (EventSwitch.LOG_UP_EVENT) {
                    Log.d("zyl", viewName + "        dispatchTouchEvent    ACTION_UP");
                }
                break;

            case MotionEvent.ACTION_CANCEL:
                if (EventSwitch.LOG_CANCEL_EVENT) {
                    Log.d("zyl", viewName + "        dispatchTouchEvent    ACTION_CANCEL");
                }
                break;

            default:
                break;
        }
        boolean dispatch = super.dispatchTouchEvent(ev);
        if (EventSwitch.LOG_RESULT) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (EventSwitch.LOG_DOWN_EVENT) {
                        Log.d("zyl", viewName + "                      dispatchTouchEvent = " + dispatch);
                    }
                    break;

                case MotionEvent.ACTION_MOVE:
                    if (EventSwitch.LOG_MOVE_EVENT) {
                        Log.d("zyl", viewName + "                      dispatchTouchEvent = " + dispatch);
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    if (EventSwitch.LOG_UP_EVENT) {
                        Log.d("zyl", viewName + "                      dispatchTouchEvent = " + dispatch);
                    }
                    break;

                case MotionEvent.ACTION_CANCEL:
                    if (EventSwitch.LOG_CANCEL_EVENT) {
                        Log.d("zyl", viewName + "                      dispatchTouchEvent = " + dispatch);
                    }
                    break;

                default:
                    break;
            }
        }
        return dispatch;
    }

运行结果:


image.png

和我们分析的过程一模一样,毫无PS痕迹 完美

没有View消费事件时MOVE和UP事件的传递

MOVE和UP事件传递给Activity的第一层ViewGroup,触发ViewGroup#dispatchTouchEvent,这里不是DOWN事件,mFirstTouchTarget也为空,会触发ViewGroup#dispatchTouchEvent第174行逻辑
dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS)
我们通过前面的分析知道如果child传值为null,会触发自身的onTouchEvent,因为onTouchEvent返回值为false,dispatchTransformedTouchEvent肯定也为false,因此Activity#dispatchTouchEvent也为false,继而调用Activity#onTouchEvent

许多博客会说如果没有View去处理事件,该事件最终会被Activity的onTouchEvent消费,这是不准确的,其实事件是尝试向下传递后发现传不下去才交还给Activity#onTouchEvent执行

DOWN事件传递有View消费事件的情况:

DOWN事件的传递

现在我们将V11的onTouchEvent返回值设置为true 现在看下DOWN事件的传递过程

在v1 -> v4 -> v13 -> v12 阶段的传递过程跟前面并无二致,直接跳过

上面我们讲过 View#onTouchEvent View#dispatchTouchEvent ViewGroup#dispatchTransformedTouchEvent的返回值是相关的,所以在DOWN事件传递到V11节点时,因为它的onTouchEvent返回了true, 在ViewGroup#dispatchTouchEvent方法的第
129行处dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign也为true,这里会进入if分支执行, 在ViewGroup#dispatchTouchEvent方法的第146行,这里生产出一个新的TouchTarget:
newTouchTarget = addTouchTarget(child, idBitsToAssign)
看下addTouchTarget的实现:

    private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
        final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
        target.next = mFirstTouchTarget;
        mFirstTouchTarget = target;
        return target;
    }

在设置完TouchTargetalreadyDispatchedToNewTouchTarget后,ViewGroup#dispatchTouchEvent跳出了遍历子View的循环,在183行,因为两个条件都满足,handled 会被置为true,继续执行其他一些逻辑后,ViewGroup#dispatchTouchEvent返回了handled 即true

此时V4的mFirstTouchTarget.child指向了V11
如图:


image.png

因为V4的dispatchTouchEvent是在V1的dispatchTransfromedTouchEvent中执行,其返回值也是强相关,因此,在V1节点重复了上述过程后, V1生成了mFirstTouchTarget并且将mFirstTouchTarget.child指向了V4

这里是我们编程思想中回溯算法的完美提现,在经过不断回溯后,最终形成了一条 DecoreView -> ContentView -> V1 -> V4 -> V11的链条

image.png
MOVE 和 UP 的传递

因为不是DOWN且mFirstTouchTarget不为空,ViewGroup#dispatchTouchEvent会执行到第186行的逻辑处,因为我们没有做任何拦截,因此会执行第189行的dispatchTransformedTouchEvent(ev, cancelChild, target.child, target.pointerIdBits)接下来的一切又非常熟悉了,这里不做赘述

因此 MOVE和UP的传递链条就是
v1 -> v4 -> v11

验证一下,将V11的onTouchEvent返回值设为ture,打印下结果:


down事件.png
UP事件.png

OK 和我们的分析一模一样 童叟无欺

有View消费事件和没有View消费事件时分发策略的区别

  • 当有View消费事件时,会在DOWN事件被消费后形成一条传递链,MOVE和UP事件会沿着传递链传递
  • 当有View消费事件时,DOWN事件被消费后传递链条中止,不会再回溯触发父布局的事件传递
  • 当没有View消费事件时,DOWN事件会对View树进行深度遍历,MOVE和UP则是尝试向ViewGroup进行事件分发失败后转给Activity的onTouchEvent处理

当上层ViewGroup拦截事件时事件分发的处理

现在我们设想一个场景:V11处理onTouchEvent 但是当滑动的Y值累计大于500时,v1将该事件拦截、

那么这种场景中事件处理是如何分发的呢?

首先看DOWN事件,其实DOWN事件在前面的场景中已经分析过了,这里就不说了

这个场景的重头戏是MOVE事件,当我们在V1中判断累计MOVE的Y值大于500时,v1的onInterceptTouchEvent会返回true,而v1的onInterceptTouchEvent方法调用是在v1的dispatchTouchEvent中:

看下ViewGroup#dispatchTouchEvent的第33行附近,这里的执行有一个前提条件,就是当前时间为DOWN事件或者mFirstTouchTarget != null

原因也比较好理解,DOWN事件是事件传递的起点,如果mFirstTouchTarget == null 则说明该ViewGroup没有后代消费这个事件,所谓拦截也就无从谈起了

当满足要求的MOVE事件传来时,intercepted会接收到true的返回值,接着执行逻辑,在一系列操作后会执行到ViewGroup#dispatchTouchEvent方法的第186行,这cancel是一个true值

当当我们将cancel的值置为ture来调用dispatchTransformedTouchEvent时,会执行ViewGroup#dispatchTransformedTouchEvent方法的第9行区块,
也就是递归执行子类的ViewGroup#dispatchTouchEvent,因为传入event事件类型是Cancel,ViewGroup#dispatchTouchEvent逻辑又会执行到186行区域,最终会调用View的onTouchEvent:

View#onTouchEvent

case MotionEvent.ACTION_CANCEL:
                    if (clickable) {
                        setPressed(false);
                    }
                    removeTapCallback();
                    removeLongPressCallback();
                    mInContextButtonPress = false;
                    mHasPerformedLongPress = false;
                    mIgnoreNextUpEvent = false;
                    mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;
                    break;

这里是一些重置和清理工作

而在执行过dispatchTransformedTouchEvent后,会将mFirstTouchTarget进行重置:

while (target != null) {
                    final TouchTarget next = target.next;
                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                        handled = true;
                    } else {
                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
                                || intercepted;
                        // 如果在事件在本View这里被拦截了, 生成Cancel事件并向子View传递
                        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;
                }

因为是递归调用,因此重置也是自下而上的

回到上面的案例中,如果拦截事件触发,Cancel事件的传递顺序应该是
v4.dispatchTouchEvent -> v4.onInterceptTouchEvent -> v11.dispatchTouchEvent -> v11.onTouchEvent

而在Cancel事件传递后,v4及其子View v11便不会再接收事件了

看下demo的运行结果:


image.png

在Cacnel事件发生之前,V4和V11一直在接收事件,但是Cancel后,便只剩下V1了,符合我们的得出的结论

以上就是事件分发流程源码的分析,听懂掌声

你可能感兴趣的:([Android]事件分发流程全解析(一)事件分发源码分析)