日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/6/17 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 三 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 事件分發 系列文章目錄


【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )


文章目錄

  • Android 事件分發 系列文章目錄
  • 前言
  • 一、獲取子組件
  • 二、當前遍歷的子組件的事件分發
  • 三、ViewGroup 事件分發相關源碼

前言

接上一篇博客 【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 ) , 繼續分析 ViewGroup 的事件分發機制后續代碼 ;





一、獲取子組件



之前已經按照 Z 軸深度 , 將組件進行排序 , 放在集合中 ;

倒序遍歷排列好的組件 , 按照 Z 軸的上下順序 , 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 ;

for (int i = childrenCount - 1; i >= 0; i--) {

先獲取組件索引 , 然后獲取索引對應的子組件 ;

// 獲取索引final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);// 獲取索引對應組件 final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);

然后判定組件是否符合要求 :
調用 canViewReceivePointerEvents 方法 , 判定組件是否可見 , 會否處于動畫中 ;

/*** Returns true if a child view can receive pointer events.* 判定控件是否可見 / 是否處于動畫中 * @hide*/private static boolean canViewReceivePointerEvents(@NonNull View child) {return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE|| child.getAnimation() != null;}

調用 isTransformedTouchPointInView 判定手指是否在控件上面 ;

/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;}

如果上面 333 個條件只要存在 111 個不滿足 , 則不傳遞觸摸事件 , 在遍歷時直接 continue , 越過該控件 , 遍歷下一個控件 ;

// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}

ViewGroup | dispatchTouchEvent 方法相關源碼 :

@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 倒序遍歷 按照 Z 軸的上下順序 , 排列好的組件 // 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 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;}// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}// 截止到此處 , 可以獲取子組件進行操作 ...}/*** Returns true if a child view can receive pointer events.* 判定控件是否可見 / 是否處于動畫中 * @hide*/private static boolean canViewReceivePointerEvents(@NonNull View child) {return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE|| child.getAnimation() != null;}/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;} }

源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java





二、當前遍歷的子組件的事件分發



首先提取當前的子組件 , 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的 , 此時第一次返回 null ;

newTouchTarget = getTouchTarget(child);

getTouchTarget 方法 , 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View ;
如果是 , 則返回該 TouchTarget ;
如果不是 , 則返回空 ;
此時默認返回 null ;

調用 dispatchTransformedTouchEvent 正式開始分發觸摸事件 , 返回值只有兩種情況 ,
① 情況一 : 子控件觸摸事件返回 true
② 情況二 : 子控件觸摸事件返回 false

dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)

dispatchTransformedTouchEvent 方法 , 是正式分發觸摸事件的方法 , 注意參數中傳入了當前正在被遍歷的 child 子組件 ;

首先處理取消狀態 , 暫時不用分析 ;

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

如果被遍歷的子組件為空 , 則調用父類的分發方法 , handled = super.dispatchTouchEvent(event); , 該分支很少執行 ;
如果被遍歷的子組件不為空 , 則調用子組件的分發方法 , handled = child.dispatchTouchEvent(event); , 子組件分發觸摸事件 , 此處調用的是 View 組件的 dispatchTouchEvent 方法 ;

// 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) {// 被遍歷的 child 子組件為空 // 調用父類的分發方法 handled = super.dispatchTouchEvent(event);} else {// 被遍歷的 child 子組件不為空 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);}

ViewGroup | dispatchTouchEvent | dispatchTransformedTouchEvent 方法相關源碼 :

@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 提取當前的子組件 // 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的// 此時第一次返回 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.newTouchTarget.pointerIdBits |= idBitsToAssign;break;}resetCancelNextUpFlag(child);// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { ...}/*** Gets the touch target for specified child view.* Returns null if not found.* */private TouchTarget getTouchTarget(@NonNull View child) {// 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View // 如果是 , 則返回該 TouchTarget // 如果不是 , 則返回空for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {if (target.child == child) {return target;}}return null;}/*** Transforms a motion event into the coordinate space of a particular child view,* filters out irrelevant pointer ids, and overrides its action if necessary.* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.* 該方法是正式分發觸摸事件的方法 * 注意參數中傳入了當前正在被遍歷的 child 子組件 */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) {// 被遍歷的 child 子組件為空 // 調用父類的分發方法 handled = super.dispatchTouchEvent(event);} else {// 被遍歷的 child 子組件不為空 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;} }

源碼路徑 : /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;@Overridepublic 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 downfinal 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);// 計算 ViewGroup 父容器下面有多少個子 View 組件 ; final int childrenCount = mChildrenCount;// TouchTarget newTouchTarget = null; 在上面聲明為空 , 此處肯定為 null ; // childrenCount 子組件個數不為 0 // 如果子組件個數為 0 , 則不走下一段代碼 , 如果子組件個數大于 0 , 則執行下一段代碼 ; // 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發功能 ; if (newTouchTarget == null && childrenCount != 0) {// 獲取單個手指的 x,y 坐標 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.// 子組件排序 , 按照 Z 軸排列的層級 , 從上到下進行排序 , // 控件會相互重疊 , Z 軸的排列次序上 , // 頂層的組件優先獲取到觸摸事件 final ArrayList<View> preorderedList = buildTouchDispatchChildList();final boolean customOrder = preorderedList == null&& isChildrenDrawingOrderEnabled();final View[] children = mChildren;// 倒序遍歷 按照 Z 軸的上下順序 , 排列好的組件 // 先遍歷的 Z 軸方向上 , 放在最上面的組件 , 也就是頂層組件 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;}// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}// 截止到此處 , 可以獲取子組件進行操作 // 提取當前的子組件 // 第一次執行 getTouchTarget 代碼時 , 是沒有 mFirstTouchTarget 的// 此時第一次返回 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.newTouchTarget.pointerIdBits |= idBitsToAssign;break;}resetCancelNextUpFlag(child);// 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false 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 indexfor (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;}}}// 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;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;}@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// disallowIntercept 存在一個默認值 , 如果值為默認值 , 直接退出 if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {// We're already in this state, assume our ancestors are tooreturn;}// 如果不是默認值 , 則進行相應更改 // 最終的值影響 mGroupFlags 是 true 還是 false if (disallowIntercept) {mGroupFlags |= FLAG_DISALLOW_INTERCEPT;} else {mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;}// Pass it up to our parentif (mParent != null) {mParent.requestDisallowInterceptTouchEvent(disallowIntercept);}}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;}/*** Provide custom ordering of views in which the touch will be dispatched.* 按照事件傳遞的順序進行組件排序 ** This is called within a tight loop, so you are not allowed to allocate objects, including* the return array. Instead, you should return a pre-allocated list that will be cleared* after the dispatch is finished.* @hide*/public ArrayList<View> buildTouchDispatchChildList() {return buildOrderedChildList();}/*** Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children,* sorted first by Z, then by child drawing order (if applicable). This list must be cleared* after use to avoid leaking child Views.** Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated* children.*/ArrayList<View> buildOrderedChildList() {final int childrenCount = mChildrenCount;if (childrenCount <= 1 || !hasChildWithZ()) return null;if (mPreSortedChildren == null) {mPreSortedChildren = new ArrayList<>(childrenCount);} else {// callers should clear, so clear shouldn't be necessary, but for safety...mPreSortedChildren.clear();mPreSortedChildren.ensureCapacity(childrenCount);}final boolean customOrder = isChildrenDrawingOrderEnabled();// 下面的組件排序的核心邏輯 // 獲取當前所有組件的子組件的 Z 軸的深度 // 按照 Z 軸深度進行排序 // Z 軸方向上 , 對于事件傳遞 , 上面的組件優先級高于被覆蓋的下面的組件優先級for (int i = 0; i < childrenCount; i++) {// add next child (in child order) to end of listfinal int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);final View nextChild = mChildren[childIndex];final float currentZ = nextChild.getZ();// insert ahead of any Views with greater Z// 計算當前遍歷的組件應該被放到的索引位置int insertIndex = i;while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) {insertIndex--;}// 將當前遍歷的組件插入到指定索引位置上 mPreSortedChildren.add(insertIndex, nextChild);}return mPreSortedChildren;}// 獲取排序后的子組件的索引值private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) {final int childIndex;if (customOrder) {final int childIndex1 = getChildDrawingOrder(childrenCount, i);if (childIndex1 >= childrenCount) {throw new IndexOutOfBoundsException("getChildDrawingOrder() "+ "returned invalid index " + childIndex1+ " (child count is " + childrenCount + ")");}childIndex = childIndex1;} else {childIndex = i;}return childIndex;}// 獲取索引值對應的組件 private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children,int childIndex) {final View child;if (preorderedList != null) {child = preorderedList.get(childIndex);if (child == null) {throw new RuntimeException("Invalid preorderedList contained null child at index "+ childIndex);}} else {child = children[childIndex];}return child;}/*** Returns true if a child view can receive pointer events.* 判定控件是否可見 / 是否處于動畫中 * @hide*/private static boolean canViewReceivePointerEvents(@NonNull View child) {return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE|| child.getAnimation() != null;}/*** Returns true if a child view contains the specified point when transformed* into its coordinate space.* Child must not be null.* 判定手指是否觸摸到了組件 , 是否在組件區域范圍內 * @hide*/protected boolean isTransformedTouchPointInView(float x, float y, View child,PointF outLocalPoint) {// 獲取當前坐標 final float[] point = getTempPoint();point[0] = x;point[1] = y;transformPointToViewLocal(point, child);final boolean isInView = child.pointInView(point[0], point[1]);if (isInView && outLocalPoint != null) {outLocalPoint.set(point[0], point[1]);}return isInView;}/*** Gets the touch target for specified child view.* Returns null if not found.* */private TouchTarget getTouchTarget(@NonNull View child) {// 判斷 mFirstTouchTarget 中的 child 字段 , 是否是當前遍歷的 子組件 View // 如果是 , 則返回該 TouchTarget // 如果不是 , 則返回空for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {if (target.child == child) {return target;}}return null;}/*** Transforms a motion event into the coordinate space of a particular child view,* filters out irrelevant pointer ids, and overrides its action if necessary.* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.* 該方法是正式分發觸摸事件的方法 * 注意參數中傳入了當前正在被遍歷的 child 子組件 */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) {// 被遍歷的 child 子組件為空 // 調用父類的分發方法 handled = super.dispatchTouchEvent(event);} else {// 被遍歷的 child 子組件不為空 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;}}

源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java

總結

以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 三 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄页免费网站 | 成人福利院 | 久久手机看片 | 日本国产精品视频 | 色婷婷精品国产一区二区三区 | 久久久天堂| 精品少妇人妻AV无码专区在线 | 国产毛片儿 | 天天爱夜夜操 | 欧美激情综合色综合啪啪五月 | 污污av| 国产视频最新 | 成人合集 | 边添小泬边狠狠躁视频 | 亚洲精品粉嫩小泬 | 免费看黄色一级片 | 99视频免费在线观看 | 丁香婷婷一区二区三区 | 黄色国产毛片 | 国产精品一区二区三区四区 | 国产成人av一区二区三区在线观看 | 蜜桃久久久aaaa成人网一区 | 欧美精品一区二区三区久久 | 精品无码久久久久久久久 | 在线不卡免费视频 | 中字幕视频在线永久在线观看免费 | 草草视频在线免费观看 | 中文天堂在线观看 | 人人曰 | 国产男女在线 | 精品国产乱码一区二区三区99 | 日韩第四页 | 久久666 | 国产调教打屁股xxxx网站 | 视色影视 | 无码人妻精品一区二区50 | 三级三级久久三级久久18 | 黑人操日本女优 | 日韩欧美激情 | 91在线视频导航 | 中文在线观看免费高清 | 一级全黄毛片 | 人人妻人人澡人人爽精品 | 国产少妇一区二区 | 日韩免费高清视频网站 | 欧美综合久久 | 天堂影视av | 亚洲情se | 国产91白丝在一线播放 | 欧美色图在线播放 | 91人人爱| 国产在线观看免费播放 | 999国产| 丰满少妇影院 | 亚洲精品字幕在线 | 国产精品111 | 宝贝乖h调教灌尿穿环 | 久久精品网址 | www中文字幕 | 国产精品无码专区av免费播放 | 色妞综合网 | 亚洲s码欧洲m码国产av | 欧美日韩不卡一区二区三区 | 纯爱无遮挡h肉动漫在线播放 | 日本精品视频在线观看 | 岛国av免费观看 | 无码一区二区三区免费视频 | 97国产成人无码精品久久久 | 亚洲中文在线一区 | 深夜福利免费在线观看 | 女攻总攻大胸奶汁(高h) | 亚洲av综合色区无码另类小说 | 国产精品第一 | av网在线 | 久久久久久久国产视频 | 国产午夜一级片 | 日欧美女人 | 亚洲播放| 日本熟伦人妇xxxx | 欧美理伦片在线播放 | 亚洲视频四区 | 小视频在线| 日韩中文一区二区三区 | 日韩av网站在线 | 在线观看免费视频国产 | jizz在亚洲| 日本爱爱免费视频 | 亚洲男人天堂2024 | 美女国产一区 | 9l视频自拍蝌蚪9l视频成人 | 国产精品理论片 | 神马久久久久久久久久久 | 麻豆视频在线 | 久久久久久av | 精品欧美视频 | 91禁国产网站 | 午夜激情免费 | 少妇导航av | 黄色激情视频在线观看 |