【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 七 )
Android 事件分發 系列文章目錄
【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 四 | View 事件傳遞機制 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 五 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 六 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 七 )
文章目錄
- Android 事件分發 系列文章目錄
- 總結
- 一、ViewGroup | dispatchTouchEvent 完整流程分析
- 二、ViewGroup | dispatchTransformedTouchEvent 完整流程分析
- 三、View | dispatchTouchEvent 完整流程分析
- 四、ViewGroup 事件分發相關源碼
- 五、View 事件分發相關源碼
總結
事件分發 主要是解決 事件沖突 問題 ,
父容器事件沖突問題主要解決方案是使用 ViewGroup 攔截事件 , 屏蔽后續的子組件事件分發 ;
子組件事件沖突 , 可以自己設置 OnTouchListener , 精準控制 按下 , 移動 , 抬起 , 取消 等觸摸事件 , 自己設計具體的業務邏輯 ;
一、ViewGroup | dispatchTouchEvent 完整流程分析
第一步 : 判斷是否是按下操作 ;
// 第一步 : 判斷是否是按下操作 // 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();}第二步 : 判斷是否需要攔截 , 用戶使用 requestDisallowInterceptTouchEvent 方法進行設置是否攔截事件 ;
// 第二步 : 判斷是否需要攔截 , 用戶使用 requestDisallowInterceptTouchEvent 方法進行設置是否攔截事件// 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;第四步 : 判定是否攔截事件 , 以及是否取消事件 , 如果都為否 ;
// 第四步 : 判定是否攔截事件 , 以及是否取消事件 , 如果都為否 // 即不攔截事件 , 該事件也不取消 , 則執行該分支 // 在該分支中 , 記錄該觸摸事件 // 此處判定 , 是否攔截 // 假定不取消 , 也不攔截 // canceled 和 intercepted 二者都是 false , 才不能攔截 ; if (!canceled && !intercepted) {第五步 : 判定是否是按下操作 , 如果是 , 則記錄該事件 , 如果不是 , 則不執行該分支 ;
// 第五步 : 判定是否是按下操作 , 如果是 , 則記錄該事件 , 如果不是 , 則不執行該分支// 判斷是否是按下操作 // 一個完整的動作 , 只有第一次按下 , 才執行下面的邏輯 // 第一次按下后 , 手指按著移動 , 屬于第2次以及之后的第n次動作 , 不再走該分支 // 直接執行該分支后面的代碼 if (actionMasked == MotionEvent.ACTION_DOWN|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {第六步 : 對子控件進行排序 ;
// 第六步 : 對子控件進行排序 // 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);第八步 : 判定當前事件是否可用 , 組件可見 , 不處于動畫階段 , 手指在組件范圍中 ;
// 第八步 : 判定當前事件是否可用 , 組件可見 , 不處于動畫階段 , 手指在組件范圍中// X 控件范圍 A , 如果手指按在 B 范圍 , 不會觸發 X 控件的事件 // 判定當前的組件是否可見 , 是否處于動畫過程中 // ① canViewReceivePointerEvents 判定組件是否可見 , 會否處于動畫 // ② isTransformedTouchPointInView 判定手指是否在控件上面 ; // 上述兩種情況 , 不觸發事件 if (!canViewReceivePointerEvents(child)|| !isTransformedTouchPointInView(x, y, child, null)) {ev.setTargetAccessibilityFocus(false);// 不觸發事件 continue;}第九步 : 正式向子組件分發觸摸事件 ;
- 如果分發事件被消耗掉 , 返回 true , 則記錄該事件 ; 記錄事件調用的 addTouchTarget 方法中 , 為 mFirstTouchTarget 成員變量賦值 ;
- 如果分發事件沒有被消耗掉 , 返回 false ;
第十步 : 判定當前是否有消費記錄 , 即 Down 按下事件是否執行完畢 ;
- 如果有事件消費記錄則 mFirstTouchTarget 成員不為空 , 此時從 TouchTarget 鏈表中取出相應的消費 Down 事件組件 , 直接將事件分發給該組件 ;
- 如果沒有事件消費記錄 , 則 mFirstTouchTarget 成員為空 , 此時調用 dispatchTransformedTouchEvent 方法消費自己 ;
二、ViewGroup | dispatchTransformedTouchEvent 完整流程分析
如果事件被攔截 , 或者沒有被消費掉 , 則不會對 mFirstTouchTarget 進行初始化 , mFirstTouchTarget 為空 ;
ViewGroup | dispatchTransformedTouchEvent 方法中 ,
- 傳入的子組件為空 , 表示事件被攔截了 , 或消費不成功 , 此時需要消費自己的觸摸事件 , 調用父類 View 的 dispatchTouchEvent 方法就是消費自己的觸摸事件 ;
- 傳入的子組件不為空 , 則將事件傳遞給子組件 ;
三、View | dispatchTouchEvent 完整流程分析
View | dispatchTouchEvent 方法中 , 會調用 View 的 OnTouchListener 方法 , OnTouchListener 是在 View 的 ListenerInfo mListenerInfo 成員中的 OnTouchListener mOnTouchListener 成員 ;
這是用戶設置的 觸摸監聽器 , 是開發時設置的組件的觸摸事件 , 返回 true / false ;
如果返回 true 則成功消費事件 , 事件分發到此結束 ;
如果返回 false , 則事件繼續向下傳遞 ;
public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean dispatchTouchEvent(MotionEvent event) {if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {result = true;}//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}} }
源碼路徑 : /frameworks/base/core/java/android/view/View.java
四、ViewGroup 事件分發相關源碼
@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();}// 第二步 : 判斷是否需要攔截 , 用戶使用 requestDisallowInterceptTouchEvent 方法進行設置是否攔截事件// 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;// 第五步 : 判定是否是按下操作 , 如果是 , 則記錄該事件 , 如果不是 , 則不執行該分支// 判斷是否是按下操作 // 一個完整的動作 , 只有第一次按下 , 才執行下面的邏輯 // 第一次按下后 , 手指按著移動 , 屬于第2次以及之后的第n次動作 , 不再走該分支 // 直接執行該分支后面的代碼 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 , 則記錄該事件// 記錄事件調用的 addTouchTarget 方法中 , 為 mFirstTouchTarget 成員變量賦值 // 如果分發事件沒有被消耗掉 , 返回 false // 正式開始分發觸摸事件// 處理以下兩種情況 : // ① 情況一 : 子控件觸摸事件返回 true // ② 情況二 : 子控件觸摸事件返回 false if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {// Child wants to receive touch within its bounds.// 如果返回值為 true , 說明該事件已經被消費了 // 此時記錄這個已經被消費的事件 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();}// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 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;}}}// 第十步 : 判定當前是否有消費記錄 , 即 Down 按下事件是否執行完畢// 如果有事件消費記錄則 mFirstTouchTarget 成員不為空 , 此時從 TouchTarget 鏈表中取出相應的消費 Down 事件組件 , 直接將事件分發給該組件// 如果沒有事件消費記錄 , 則 mFirstTouchTarget 成員為空 , 此時調用 dispatchTransformedTouchEvent 方法消費自己 ; // 上面的分支是只有第一次按下時才執行的 // 假如當前動作時按下以后的移動/抬起動作 // 則跳過上面的分支 , 直接執行后面的代碼邏輯 // 按下之后 mFirstTouchTarget 肯定不為空 // 如果事件被消費 , 事件分發方法 dispatchTransformedTouchEvent 返回 true // 就會創建 newTouchTarget 值 , 該值不會為空 , 同時 mFirstTouchTarget 不為空// 反之// 如果上述事件分發方法 dispatchTransformedTouchEvent 返回 false // 此時 newTouchTarget 值 , 就會為空 , 同時 mFirstTouchTarget 為空 // // 還有一個邏輯就是 , 如果該事件被父容器攔截 , mFirstTouchTarget 也是 null 值// 調用 dispatchTransformedTouchEvent , 但是傳入的子組件時 null // 在 dispatchTransformedTouchEvent 方法中觸發調用 if (child == null) 分支的 // handled = super.dispatchTouchEvent(event) 方法 , 調用父類的事件分發方法 // 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 {// TouchTarget 對象對應著一個完整的動作 , 該動作包含 1 個按下事件 , 若干 移動 事件 , 1 個抬起事件 ; // 第一次按下 , 負責構建 TouchTarget 鏈表 , 將消費事件的 View 組件封裝到 TouchTarget 中// 然后的移動/抬起操作 , 不再重復的創建 TouchTarget 對象了 // 直接使用第一次按下的 TouchTarget 對象作為當前動作的標識 // 直接向該 TouchTarget 對象中的 View 組件分發事件 // 這也是我們按下按鈕時 , 即使將手指按著移出邊界 , 按鈕也處于按下狀態 ; // 事件被消費的分支 , 事件消費成功 , 會走這個分支 // Dispatch to touch targets, excluding the new touch target if we already// dispatched to it. Cancel touch targets if necessary.TouchTarget predecessor = null;// 將當前所有的消費的事件以及消費的 View 組件做成了一個鏈表 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 , 開始分發觸摸事件 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);}// ViewGroup 的 事件分發方法執行完畢后 // 返回到 Activity 的 dispatchTouchEvent return handled;}/* Describes a touched view and the ids of the pointers that it has captured.** This code assumes that pointer ids are always in the range 0..31 such that* it can use a bitfield to track which pointer ids are present.* As it happens, the lower layers of the input dispatch pipeline also use the* same trick so the assumption should be safe here...* 可以理解成觸摸事件的消費目標 */private static final class TouchTarget {private static final int MAX_RECYCLED = 32;private static final Object sRecycleLock = new Object[0];private static TouchTarget sRecycleBin;private static int sRecycledCount;public static final int ALL_POINTER_IDS = -1; // all ones// The touched child view.// 當前 View 對象 public View child;// The combined bit mask of pointer ids for all pointers captured by the target.public int pointerIdBits;// The next target in the target list.// 鏈表操作 , 該引用指向下一個觸摸事件 public TouchTarget next;private TouchTarget() {}public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {if (child == null) {throw new IllegalArgumentException("child must be non-null");}final TouchTarget target;synchronized (sRecycleLock) {if (sRecycleBin == null) {target = new TouchTarget();} else {target = sRecycleBin;sRecycleBin = target.next;sRecycledCount--;target.next = null;}}target.child = child;target.pointerIdBits = pointerIdBits;return target;}public void recycle() {if (child == null) {throw new IllegalStateException("already recycled once");}synchronized (sRecycleLock) {if (sRecycledCount < MAX_RECYCLED) {next = sRecycleBin;sRecycleBin = this;sRecycledCount += 1;} else {next = null;}child = null;}}}@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 子組件 * 如果事件被攔截 , 或者沒有被消費掉 , 則不會對 mFirstTouchTarget 進行初始化 , mFirstTouchTarget 為空*/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) {// 傳入的子組件為空 // 表示事件被攔截了 / 或消費不成功// 消費自己的觸摸事件 , 調用父類 View 的 dispatchTouchEvent 方法就是消費自己的觸摸事件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;}/*** Adds a touch target for specified child to the beginning of the list.* Assumes the target child is not already present.*/private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);target.next = mFirstTouchTarget;mFirstTouchTarget = target;return target;}@Overridepublic void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {// 設置父容器是否要攔截子組件的觸摸事件if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) {// We're already in this state, assume our ancestors are tooreturn;}if (disallowIntercept) {mGroupFlags |= FLAG_DISALLOW_INTERCEPT;} else {mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;}// Pass it up to our parentif (mParent != null) {mParent.requestDisallowInterceptTouchEvent(disallowIntercept);}}}
五、View 事件分發相關源碼
public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {/*** Pass the touch screen motion event down to the target view, or this* view if it is the target.** @param event The motion event to be dispatched.* @return True if the event was handled by the view, false otherwise.*/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 gesturestopNestedScroll();}if (onFilterTouchEventForSecurity(event)) {if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {result = true;}//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}// 如果上面為 true ( 觸摸監聽器的觸摸事件處理返回 true ) , 就會阻斷該分支的命中 , 該分支不執行了 // 也就不會調用 View 組件自己的 onTouchEvent 方法 // 因此 , 如果用戶的 觸摸監聽器 OnTouchListener 返回 true // 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 // 如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener // 觸摸監聽器 OnTouchListener 返回 false , 點擊監聽器 OnClickListener 才能被調用到 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;}/*** Implement this method to handle touch screen motion events.* <p>* If this method is used to detect click actions, it is recommended that* the actions be performed by implementing and calling* {@link #performClick()}. This will ensure consistent system behavior,* including:* <ul>* <li>obeying click sound preferences* <li>dispatching OnClickListener calls* <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when* accessibility features are enabled* </ul>** @param event The motion event.* @return True if the event was handled, false otherwise.*/public boolean onTouchEvent(MotionEvent event) {final float x = event.getX();final float y = event.getY();final int viewFlags = mViewFlags;final int action = event.getAction();final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;if ((viewFlags & ENABLED_MASK) == DISABLED) {if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.return clickable;}if (mTouchDelegate != null) {if (mTouchDelegate.onTouchEvent(event)) {return true;}}if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {switch (action) {case MotionEvent.ACTION_UP:// 點擊事件 Click 是 按下 + 抬起 事件 // 如果要判定點擊 , 需要同時有 MotionEvent.ACTION_DOWN + MotionEvent.ACTION_UP 事件 mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;if ((viewFlags & TOOLTIP) == TOOLTIP) {handleTooltipUp();}if (!clickable) {removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;break;}boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {// take focus if we don't have it already and we should in// touch mode.boolean focusTaken = false;if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {focusTaken = requestFocus();}if (prepressed) {// The button is being released before we actually// showed it as pressed. Make it show the pressed// state now (before scheduling the click) to ensure// the user sees it.setPressed(true, x, y);}if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {// This is a tap, so remove the longpress checkremoveLongPressCallback();// Only perform take click actions if we were in the pressed stateif (!focusTaken) {// Use a Runnable and post this rather than calling// performClick directly. This lets other visual state// of the view update before click actions start.if (mPerformClick == null) {// 此處創建點擊對象 mPerformClick = new PerformClick();}// 調用點擊事件 if (!post(mPerformClick)) {performClickInternal();}}}if (mUnsetPressedState == null) {mUnsetPressedState = new UnsetPressedState();}if (prepressed) {postDelayed(mUnsetPressedState,ViewConfiguration.getPressedStateDuration());} else if (!post(mUnsetPressedState)) {// If the post failed, unpress right nowmUnsetPressedState.run();}removeTapCallback();}mIgnoreNextUpEvent = false;break;case MotionEvent.ACTION_DOWN:if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {mPrivateFlags3 |= PFLAG3_FINGER_DOWN;}mHasPerformedLongPress = false;if (!clickable) {checkForLongClick(0, x, y);break;}if (performButtonActionOnTouchDown(event)) {break;}// Walk up the hierarchy to determine if we're inside a scrolling container.boolean isInScrollingContainer = isInScrollingContainer();// For views inside a scrolling container, delay the pressed feedback for// a short period in case this is a scroll.if (isInScrollingContainer) {mPrivateFlags |= PFLAG_PREPRESSED;if (mPendingCheckForTap == null) {mPendingCheckForTap = new CheckForTap();}mPendingCheckForTap.x = event.getX();mPendingCheckForTap.y = event.getY();postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());} else {// Not inside a scrolling container, so show the feedback right awaysetPressed(true, x, y);checkForLongClick(0, x, y);}break;case MotionEvent.ACTION_CANCEL:if (clickable) {setPressed(false);}removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;break;case MotionEvent.ACTION_MOVE:if (clickable) {drawableHotspotChanged(x, y);}// Be lenient about moving outside of buttonsif (!pointInView(x, y, mTouchSlop)) {// Outside button// Remove any future long press/tap checksremoveTapCallback();removeLongPressCallback();if ((mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;}break;}return true;}return false;}/*** Entry point for {@link #performClick()} - other methods on View should call it instead of* {@code performClick()} directly to make sure the autofill manager is notified when* necessary (as subclasses could extend {@code performClick()} without calling the parent's* method).*/private boolean performClickInternal() {// Must notify autofill manager before performing the click actions to avoid scenarios where// the app has a click listener that changes the state of views the autofill service might// be interested on.notifyAutofillManagerOnClick();return performClick();}/*** Call this view's OnClickListener, if it is defined. Performs all normal* actions associated with clicking: reporting accessibility event, playing* a sound, etc.** @return True there was an assigned OnClickListener that was called, false* otherwise is returned.*/// NOTE: other methods on View should not call this method directly, but performClickInternal()// instead, to guarantee that the autofill manager is notified when necessary (as subclasses// could extend this method without calling super.performClick()).public boolean performClick() {// We still need to call this method to handle the cases where performClick() was called// externally, instead of through performClickInternal()notifyAutofillManagerOnClick();final boolean result;final ListenerInfo li = mListenerInfo;if (li != null && li.mOnClickListener != null) {playSoundEffect(SoundEffectConstants.CLICK);// 此處直接執行了 點擊監聽器 的點擊方法 li.mOnClickListener.onClick(this);result = true;} else {result = false;}sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);notifyEnterOrExitForAutoFillIfNeeded(true);return result;}}
源碼路徑 : /frameworks/base/core/java/android/view/View.java
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 七 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 事件分发】事件分发源码
- 下一篇: 【错误记录】NDK 配置错误 ( C/C