您当前的位置: 首页 >  android

韩曙亮

暂无认证

  • 1浏览

    0关注

    1068博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 二 )

韩曙亮 发布时间:2021-06-28 23:31:26 ,浏览量:1

Android 事件分发 系列文章目录

【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 ) 【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup ) 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 一 ) 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 二 )

文章目录
  • Android 事件分发 系列文章目录
  • 前言
  • 一、获取触摸索引值
  • 二、按照 Z 轴深度排序组件
  • 三、ViewGroup 事件分发相关源码

前言

接上一篇博客 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 一 ) , 继续分析 ViewGroup 的事件分发机制后续代码 ;

一、获取触摸索引值

首先在 动作事件不是取消操作 , 且不拦截事件 , 的前提下 , 才能执行后续操作 , 判定代码如下 :

			// 此处判定 , 是否拦截 
			// 假定不取消 , 也不拦截 
			// canceled 和 intercepted 二者都是 false , 才不能拦截 ; 
            if (!canceled && !intercepted) {

凡是涉及到 Accessibility 功能的 , 直接忽略 , 与当前分析的事件分发无关 ;

                View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
                        ? findChildWithAccessibilityFocus() : null;

再次判定是否是按下操作 ;

				// 判断是否是按下操作 
                if (actionMasked == MotionEvent.ACTION_DOWN
                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {

如果是按下操作 , 则获取触摸索引值 , 从 0 开始计数 ;

                    // 获取触摸索引值 
                    final int actionIndex = ev.getActionIndex(); // always 0 for down

ViewGroup | dispatchTouchEvent 方法相关源码 :

@UiThread
public abstract class ViewGroup extends View implements ViewParent, ViewManager {
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
		...
			// 此处判定 , 是否拦截 
			// 假定不取消 , 也不拦截 
			// canceled 和 intercepted 二者都是 false , 才不能拦截 ; 
            if (!canceled && !intercepted) {

                // If the event is targeting accessibility 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 = childrenCount) {
                throw new IndexOutOfBoundsException("getChildDrawingOrder() "
                        + "returned invalid index " + childIndex1
                        + " (child count is " + childrenCount + ")");
            }
            childIndex = childIndex1;
        } else {
            childIndex = i;
        }
        return childIndex;
    }
}

源码路径 : /frameworks/base/core/java/android/view/ViewGroup.java

三、ViewGroup 事件分发相关源码

ViewGroup 事件分发相关源码 : 下面的代码中 , 逐行注释分析了 ViewGroup 的 dispatchTouchEvent 事件分发操作 ;

@UiThread
public abstract class ViewGroup extends View implements ViewParent, ViewManager {

    // First touch target in the linked list of touch targets.
    private TouchTarget mFirstTouchTarget;

    @Override
    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) {
                // 判断是否需要拦截 , 可以使用 requestDisallowInterceptTouchEvent 方法进行设置
                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
                if (!disallowIntercept) {
                	// 进行事件拦截 
                	// 该 onInterceptTouchEvent 方法只返回是否进行事件拦截 , 返回一个布尔值 , 没有进行具体的事件拦截 
                	// 是否进行拦截 , 赋值给了 intercepted 局部变量 
                	// 该值决定是否进行拦截 
                    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.
            // 检查是否取消操作 , 手指是否移除了组件便捷 ; 
            // 一般情况默认该值是 false ; 
            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;
            // 注意此处 newTouchTarget 为空 
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;

			// 此处判定 , 是否拦截 
			// 假定不取消 , 也不拦截 
			// canceled 和 intercepted 二者都是 false , 才不能拦截 ; 
            if (!canceled && !intercepted) {

                // If the event is targeting accessibility 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             
关注
打赏
1663594092
查看更多评论
0.0415s