【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 二 )
Android 事件分發(fā) 系列文章目錄
【Android 事件分發(fā)】事件分發(fā)源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發(fā)】事件分發(fā)源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發(fā)】事件分發(fā)源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發(fā)】事件分發(fā)源碼分析 ( ViewGroup 事件傳遞機制 二 )
文章目錄
- Android 事件分發(fā) 系列文章目錄
- 前言
- 一、獲取觸摸索引值
- 二、按照 Z 軸深度排序組件
- 三、ViewGroup 事件分發(fā)相關源碼
前言
接上一篇博客 【Android 事件分發(fā)】事件分發(fā)源碼分析 ( ViewGroup 事件傳遞機制 一 ) , 繼續(xù)分析 ViewGroup 的事件分發(fā)機制后續(xù)代碼 ;
一、獲取觸摸索引值
首先在 動作事件不是取消操作 , 且不攔截事件 , 的前提下 , 才能執(zhí)行后續(xù)操作 , 判定代碼如下 :
// 此處判定 , 是否攔截 // 假定不取消 , 也不攔截 // canceled 和 intercepted 二者都是 false , 才不能攔截 ; if (!canceled && !intercepted) {凡是涉及到 Accessibility 功能的 , 直接忽略 , 與當前分析的事件分發(fā)無關 ;
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 downViewGroup | dispatchTouchEvent 方法相關源碼 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic 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 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);...} }源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
二、按照 Z 軸深度排序組件
先計算 ViewGroup 父容器下面有多少個子 View 組件 ;
// 計算 ViewGroup 父容器下面有多少個子 View 組件 ; final int childrenCount = mChildrenCount;如果子組件個數為 0 , 則不走下一段代碼 , 如果子組件個數大于 0 , 則執(zhí)行下一段代碼 ; 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發(fā)功能 ;
在子組件個數不為 0 的情況下 , 繼續(xù)向后執(zhí)行 ;
獲取手指觸摸的 x, y 坐標值 ;
// 獲取單個手指的 x,y 坐標 final float x = ev.getX(actionIndex);final float y = ev.getY(actionIndex);子組件排序 : 按照 Z 軸排列的層級 , 從上到下進行排序 , 控件會相互重疊 , Z 軸的排列次序上 , 頂層的組件優(yōu)先獲取到觸摸事件 ;
// TouchTarget newTouchTarget = null; 在上面聲明為空 , 此處肯定為 null ; // childrenCount 子組件個數不為 0 // 如果子組件個數為 0 , 則不走下一段代碼 , 如果子組件個數大于 0 , 則執(zhí)行下一段代碼 ; // 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發(fā)功能 ; 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 軸的排列次序上 , // 頂層的組件優(yōu)先獲取到觸摸事件 final ArrayList<View> preorderedList = buildTouchDispatchChildList();ViewGroup | buildTouchDispatchChildList 方法是排序的核心方法 ;
獲取當前所有組件的子組件的 Z 軸的深度 , 按照 Z 軸深度進行排序 , Z 軸方向上 , 對于事件傳遞 , 上面的組件優(yōu)先級高于被覆蓋的下面的組件優(yōu)先級 ;
下面的代碼是組件遍歷排序的核心邏輯 :
// 下面的組件排序的核心邏輯 // 獲取當前所有組件的子組件的 Z 軸的深度 // 按照 Z 軸深度進行排序 // Z 軸方向上 , 對于事件傳遞 , 上面的組件優(yōu)先級高于被覆蓋的下面的組件優(yōu)先級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);}ViewGroup | dispatchTouchEvent / buildOrderedChildList 方法相關源碼 :
@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {...// 計算 ViewGroup 父容器下面有多少個子 View 組件 ; final int childrenCount = mChildrenCount;// TouchTarget newTouchTarget = null; 在上面聲明為空 , 此處肯定為 null ; // childrenCount 子組件個數不為 0 // 如果子組件個數為 0 , 則不走下一段代碼 , 如果子組件個數大于 0 , 則執(zhí)行下一段代碼 ; // 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發(fā)功能 ; 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 軸的排列次序上 , // 頂層的組件優(yōu)先獲取到觸摸事件 final ArrayList<View> preorderedList = buildTouchDispatchChildList();final boolean customOrder = preorderedList == null&& isChildrenDrawingOrderEnabled();final View[] children = mChildren;...}/*** 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 軸方向上 , 對于事件傳遞 , 上面的組件優(yōu)先級高于被覆蓋的下面的組件優(yōu)先級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;} }源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
三、ViewGroup 事件分發(fā)相關源碼
ViewGroup 事件分發(fā)相關源碼 : 下面的代碼中 , 逐行注釋分析了 ViewGroup 的 dispatchTouchEvent 事件分發(fā)操作 ;
@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.// 判斷產生事件的目標組件是可訪問性的 , 那么按照普通的事件分發(fā)進行處理 ; // 可能由其子類處理點擊事件 ; // 判斷當前是否正在使用 無障礙 相關功能產生事件 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.// 判斷是否是第一次按下 , 如果是第一次按下 , 則執(zhí)行下面的業(yè)務邏輯 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);// 如果是第一次按下 , 那么重置觸摸狀態(tài) 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 , 則執(zhí)行下一段代碼 ; // 說明下面的代碼塊中處理的是 ViewGroup 中子組件的事件分發(fā)功能 ; 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 軸的排列次序上 , // 頂層的組件優(yōu)先獲取到觸摸事件 final ArrayList<View> preorderedList = buildTouchDispatchChildList();final boolean customOrder = preorderedList == null&& isChildrenDrawingOrderEnabled();final View[] children = mChildren;... 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 軸方向上 , 對于事件傳遞 , 上面的組件優(yōu)先級高于被覆蓋的下面的組件優(yōu)先級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;} }源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java
總結
以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 二 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android 事件分发】事件分发源码
- 下一篇: android sina oauth2.