美文网首页
事件分发机制源码分析

事件分发机制源码分析

作者: 无良安生 | 来源:发表于2020-09-28 21:06 被阅读0次
布局.png

如图是布局的层次,点击View C 发生一系列调用:

  1. 首先先了解一些方法
fun onInterceptTouchEvent(ev: MotionEvent?): Boolean
fun dispatchTouchEvent(ev: MotionEvent?): Boolean 
fun onTouchEvent(event: MotionEvent?): Boolean
  1. 事件流程

A.dispatchTouchEvent
A.onInterceptTouchEvent
B.dispatchTouchEvent
B.onInterceptTouchEvent
C.dispatchTouchEvent
C.onTouchEven
C.onClick

  1. dispatchTouchEvent
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
           ...省略代码
            //是否拦截.
            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;
            }
              ...省略代码
            //根据onInterceptTouchEvent返回值和canceled去决定是否分发事件
            if (!canceled && !intercepted) {
                        //那到子控件然后倒置遍历 提高用户体验提高响应最上层view的事件
                        final View[] children = mChildren;
                        for (int i = childrenCount - 1; i >= 0; i--) {
                            ...省略代码
                            //  isTransformedTouchPointInView 判断点击的坐标点是否在 child 范围内 然后将不在的子控件过滤掉
                            if (!child.canReceivePointerEvents()
                                    || !isTransformedTouchPointInView(x, y, child, null)) {
                          ...省略代码
                            }
                             //调用dispatchTransformedTouchEvent方法将事件分发给子控件
                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
                              ...省略代码
                                } else {
                                    mLastTouchDownIndex = childIndex;
                                }
                                ...省略代码
                                break;
                            }
                               ...省略代码
                    }

                  ...省略代码
                }
            }

            ...省略代码
           //后面调用,传入view = null dispatchTransformedTouchEvent  执行onTouchEven
    }

                                   

  1. dispatchTransformedTouchEvent
 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
            View child, int desiredPointerIdBits) {
        final boolean handled;

       
        final int oldAction = event.getAction();
         // ACTION_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;
        }
       ...省略代码
        if (newPointerIdBits == oldPointerIdBits) {
            if (child == null || child.hasIdentityMatrix()) {
                if (child == null) {
                    handled = super.dispatchTouchEvent(event);
                } else {
                   ...省略代码
                    handled = child.dispatchTouchEvent(event);
                }
                return handled;
            }
            
        } else {
           
        }

        if (child == null) {
            handled = super.dispatchTouchEvent(transformedEvent);
        } else {
            handled = child.dispatchTouchEvent(transformedEvent);
        }

        return handled;
    }

相关文章

网友评论

      本文标题:事件分发机制源码分析

      本文链接:https://www.haomeiwen.com/subject/vnrjuktx.html