摘要:網(wǎng)上看到過很多人寫的事件分發(fā)機(jī)制解析感覺表述都不是很清楚也可能沒有看到寫得好的文章所以自己重新看了一遍源碼來徹底搞清楚事件分發(fā)機(jī)制觸摸事件有哪些以及怎么從傳遞到大家可以上網(wǎng)查下,幾個(gè)重要方法的基本調(diào)用順序這些很容易搜到我們重點(diǎn)關(guān)注事件從到的
網(wǎng)上看到過很多人寫的事件分發(fā)機(jī)制解析,感覺表述都不是很清楚,也可能沒有看到寫得好的文章,所以自己重新看了一遍源碼,來徹底搞清楚Android事件分發(fā)機(jī)制.
觸摸事件有哪些以及怎么從Activity傳遞到DecorView大家可以上網(wǎng)查下,幾個(gè)重要方法的基本調(diào)用順序,這些很容易搜到,我們重點(diǎn)關(guān)注事件從ViewGroup到View的事件具體的執(zhí)行過程.
1 Android事件分發(fā)涉及到的方法主要有dispatchTouchEvent()事件分發(fā)
onInterceptTouchEvent()攔截事件
onTouchEvent()處理事件
requestDisallowInterceptTouchEvent()請求不要攔截事件
BigGroup類型ViewGroup
SmallGroup類型ViewGroup
TestView類型View*(clickable默認(rèn)false)
log如下所示:
BigGroup: dispatchTouchEvent() called with: ev = [ACTION_DOWN]
BigGroup: onInterceptTouchEvent() called with: ev = [ACTION_DOWN]
SmallGroup: dispatchTouchEvent() called with: ev = [ACTION_DOWN]
SmallGroup: onInterceptTouchEvent() called with: ev = [ACTION_DOWN]
TestView: dispatchTouchEvent() called with: ev = [ACTION_DOWN]
TestView: onTouchEvent() called with: event = [ACTION_DOWN]
SmallGroup: onTouchEvent() called with: event = [ACTION_DOWN]
BigGroup: onTouchEvent() called with: event = [ACTION_DOWN]
我們看到不管怎么點(diǎn)擊滑動(dòng),都只會(huì)觸發(fā)DOWN事件的流程,我們來看下這是為什么,首先我們來看下ViewGroup的dispatchTouchEvent()
///////////ViewGroup @Override public boolean dispatchTouchEvent(MotionEvent ev) { ...省略部分代碼 boolean handled = false; if (onFilterTouchEventForSecurity(ev)) { final int action = ev.getAction(); final int actionMasked = action & MotionEvent.ACTION_MASK; //如果是DOWN事件,清除標(biāo)記,恢復(fù)一些標(biāo)記狀態(tài) if (actionMasked == MotionEvent.ACTION_DOWN) { cancelAndClearTouchTargets(ev); resetTouchState(); } // Check for interception. //判斷是否攔截 final boolean intercepted; //觸發(fā)DOWN事件或者mFirstTouchTarget不是Null,才會(huì)走if里面的邏輯 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. //不是DOWN事件 mFirstTouchTarget為空 攔截后續(xù)事件 intercepted = true; } ...省略部分代碼 // Check for cancelation. 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; TouchTarget newTouchTarget = null; boolean alreadyDispatchedToNewTouchTarget = false; //事件沒有被取消或者沒有被攔截執(zhí)行if中邏輯 if (!canceled && !intercepted) { 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 << 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); final int childrenCount = mChildrenCount; if (newTouchTarget == null && childrenCount != 0) { 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. final ArrayListpreorderedList = buildTouchDispatchChildList(); final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled(); final View[] children = mChildren; for (int i = childrenCount - 1; i >= 0; i--) { final int childIndex = getAndVerifyPreorderedIndex( childrenCount, i, customOrder); final View child = getAndVerifyPreorderedView( preorderedList, children, childIndex); if (childWithAccessibilityFocus != null) { if (childWithAccessibilityFocus != child) { continue; } childWithAccessibilityFocus = null; i = childrenCount - 1; } //判斷child是否可以響應(yīng)事件 是否點(diǎn)擊在了View的范圍之內(nèi) if (!canViewReceivePointerEvents(child) || !isTransformedTouchPointInView(x, y, child, null)) { ev.setTargetAccessibilityFocus(false); continue; } //獲取上次響應(yīng)事件的View,DOWN事件時(shí)候newTouchTarget為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); //很重要的一個(gè)方法,在下面我們會(huì)看這個(gè)方法做了什么 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 index for (int j = 0; j < childrenCount; j++) { if (children[childIndex] == mChildren[j]) { mLastTouchDownIndex = j; break; } } } else { mLastTouchDownIndex = childIndex; } mLastTouchDownX = ev.getX(); mLastTouchDownY = ev.getY(); //在這個(gè)方法中把child賦值給了mFirstTouchTarget newTouchTarget = addTouchTarget(child, idBitsToAssign); alreadyDispatchedToNewTouchTarget = true; break; } ev.setTargetAccessibilityFocus(false); } if (preorderedList != null) preorderedList.clear(); } ... } } // 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; } } ... } ... return handled; } //最重要的幾行代碼的邏輯 private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, View child, int desiredPointerIdBits) { final boolean handled; ...省略部分代碼 // Perform any necessary transformations and dispatch. //child為空調(diào)用父View的dispatchTouchEvent 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()); } //child不為空調(diào)用child的dispatchTouchEvent方法 handled = child.dispatchTouchEvent(transformedEvent); } // Done. transformedEvent.recycle(); return handled; } //默認(rèn)返回false 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; } //////////View public boolean dispatchTouchEvent(MotionEvent event) { ... boolean result = false; ... if (onFilterTouchEventForSecurity(event)) { if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) { result = true; } //noinspection SimplifiableIfStatement ListenerInfo li = mListenerInfo; //我們看到有mOnTouchListener,并且onTouch返回true,就沒有onTouchEvent啥事了 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { result = true; } if (!result && onTouchEvent(event)) { result = true; } } ... return result; } 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 (clickable || (viewFlags & TOOLTIP) == TOOLTIP) { switch (action) { case MotionEvent.ACTION_UP: ... break; case MotionEvent.ACTION_DOWN: ... break; case MotionEvent.ACTION_CANCEL: ... break; } return true; } return false; }
我們梳理下DOWN事件的執(zhí)行流程:
1 觸發(fā)BigGroup的dispatchTouchEvent()方法
2 給intercepted賦值,disallowIntercept默認(rèn)為false,這是就會(huì)觸發(fā)onInterceptTouchEvent()方法,onInterceptTouchEvent默認(rèn)返回false.intercepted賦值為false
3 接下來走到if (!canceled && !intercepted)的邏輯里面去了,注意注意intercepted為true的話這個(gè)方法直接就跳過去了.在這段邏輯里遍歷所有的子View,接下來進(jìn)入淘汰機(jī)制,child不能獲得焦點(diǎn),下一個(gè),然后看看View不可見,下一個(gè),沒有點(diǎn)到child范圍之內(nèi),下一個(gè).剩下來的娃會(huì)執(zhí)行dispatchTransformedTouchEvent方法
4 接下來dispatchTransformedTouchEvent中調(diào)用了child的dispatchTouchEvent方法
5 執(zhí)行dispatchTouchEvent的child就是我們的SmallGroup,執(zhí)行邏輯同2-4
6 這次執(zhí)行dispatchTouchEvent的child就是我們的TestView了
7 TestView dispatchTouchEvent中首先判斷了有沒有onTouchListener,判斷onTouch方法的返回值.我們肯定沒寫的了,接下來會(huì)執(zhí)行onTouchEvent
8 在TestView onTouchEvent中我們看到如果這個(gè)View可點(diǎn)擊就返回true,不可點(diǎn)擊就返回false.TestView不可點(diǎn)擊,所以返回了false
9 觸發(fā)了連鎖反應(yīng),TestView dispatchTouchEvent返回false
10SmallGroup dispatchTransformedTouchEvent返回了false, mFirstTouchTarget為null
11SmallGroup再次執(zhí)行dispatchTransformedTouchEvent child參數(shù)為null,執(zhí)行super.dispatchTouchEvent(),ViewGroup的super最后就是View,所以會(huì)執(zhí)行邏輯7-9,不同的事主角事SmallGroup
12BigGroup dispatchTransformedTouchEvent返回了false, mFirstTouchTarget為null,重復(fù)步驟11,主角BigGroup,DOWN事件結(jié)束
mFirstTouchTarget為null會(huì)有什么影響呢?后續(xù)事件中intercepted為true,執(zhí)行dispatchTransformedTouchEvent child參數(shù)為null,執(zhí)行super.dispatchTouchEvent(),事件直接傳遞不下來了,后續(xù)事件都接收不到了
4 onInterceptTouchEvent()直接返回trueLOG如下所示:(在SmallGroup onInterceptTouchEvent中return true)
BigGroup: dispatchTouchEvent() called with: ev = [ACTION_DOWN]
BigGroup: onInterceptTouchEvent() called with: ev = [ACTION_DOWN]
SmallGroup: dispatchTouchEvent() called with: ev = [ACTION_DOWN]
SmallGroup: onInterceptTouchEvent() called with: ev = [ACTION_DOWN]
SmallGroup: onTouchEvent() called with: event = [ACTION_DOWN]
BigGroup: onTouchEvent() called with: event = [ACTION_DOWN]
按照我們之前的步驟分析,SmallGroup的onInterceptTouchEvent中返回true,那么事件就不會(huì)向下執(zhí)行,由于mFirstTouchTarget為null,所以后續(xù)事件我們都接收不到了.
如果在SmallGroup的onTouchEvent中返回true,就可以收到后續(xù)事件.但是SmallGrou的onInterceptTouchEvent只會(huì)執(zhí)行一次.我們來思考下為什么?
我們在SmallGroup的onInterceptTouchEvent中返回true,那么SmallGroup的mFristTouchTarget就是null;因?yàn)槲覀冊赟mallGroup的onTouchEvent中返回true,所以SmallGroup的dispatchTouchEvent也返回true.那么GigGroup的mFristTouchTarget不是null,所以事件才能延續(xù),由于不滿足事件為DOWN,也不滿足mFristTouchTarget?。絥ull,所以不會(huì)走到攔截方法里面.
SamllGroup 的onInterceptTouchEvent()直接返回true,我們在TestView中調(diào)用getParent().requestDisallowInterceptTouchEvent(true),會(huì)有效果嗎?
不會(huì),為什么呢?onInterceptTouchEvent直接返回true攔截的話,getParent().requestDisallowInterceptTouchEvent(true)根本就執(zhí)行不到,所以一般情況下不要攔截DOWN事件.
getParent().requestDisallowInterceptTouchEvent(true)成功以后會(huì)有什么現(xiàn)象呢?
disallowIntercept變量為true,所有的onInterceptTouchEvent方法都不會(huì)在執(zhí)行.
dispatchTouchEvent()
onInterceptTouchEvent()
onTouchEvent()
requestDisallowInterceptTouchEvent()
搞明白這幾個(gè)事件的順序及調(diào)用時(shí)機(jī),以及調(diào)用有哪些限制.我們就明白了Android的事件分發(fā)機(jī)制.mFirstTouchTarget是一個(gè)很重要的變量,大多數(shù)情況下,它影響onInterceptTouchEvent的調(diào)用情況,進(jìn)而影響事件是否往下傳遞.onTouchEvent()的返回值很大程度上決定了dispatchTouchEvent方法的返回值.dispatchTouchEvent返回false,后面的事件就基本沒它啥事了.
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/76158.html
摘要:瀏覽器是多進(jìn)程的詳情看我上篇總結(jié)瀏覽器執(zhí)行機(jī)制的文章深入前端徹底搞懂瀏覽器運(yùn)行機(jī)制瀏覽器每打開一個(gè)標(biāo)簽頁,就相當(dāng)于創(chuàng)建了一個(gè)獨(dú)立的瀏覽器進(jìn)程。執(zhí)行異步操作事件完成,回調(diào)函數(shù)進(jìn)入。主線程從讀取回調(diào)函數(shù)并執(zhí)行。 最近看了很多關(guān)于JS運(yùn)行機(jī)制的文章,每篇都獲益匪淺,但各有不同,所以在這里對這幾篇文章里說的很精辟的地方做一個(gè)總結(jié),參考文章鏈接見最后。本文博客地址 了解進(jìn)程和線程 進(jìn)程是應(yīng)用...
摘要:瀏覽器是多進(jìn)程的詳情看我上篇總結(jié)瀏覽器執(zhí)行機(jī)制的文章深入前端徹底搞懂瀏覽器運(yùn)行機(jī)制瀏覽器每打開一個(gè)標(biāo)簽頁,就相當(dāng)于創(chuàng)建了一個(gè)獨(dú)立的瀏覽器進(jìn)程。執(zhí)行異步操作事件完成,回調(diào)函數(shù)進(jìn)入。主線程從讀取回調(diào)函數(shù)并執(zhí)行。 最近看了很多關(guān)于JS運(yùn)行機(jī)制的文章,每篇都獲益匪淺,但各有不同,所以在這里對這幾篇文章里說的很精辟的地方做一個(gè)總結(jié),參考文章鏈接見最后。本文博客地址 了解進(jìn)程和線程 進(jìn)程是應(yīng)用...
摘要:徹底搞懂執(zhí)行機(jī)制首先我們大家都了解的是,是一門單線程語言,所以我們就可以得出是按照語句順序執(zhí)行的首先看這個(gè)顯然大家都知道結(jié)果,依次輸出,然而換一種這個(gè)時(shí)候再看代碼的順序執(zhí)行,輸出,,,。不過即使主線程為空,也是達(dá)不到的,根據(jù)標(biāo)準(zhǔn),最低是。 徹底搞懂JavaScript執(zhí)行機(jī)制 首先我們大家都了解的是,JavaScript 是一門單線程語言,所以我們就可以得出: JavaScript 是...
閱讀 3138·2021-09-22 15:50
閱讀 3339·2021-09-10 10:51
閱讀 3153·2019-08-29 17:10
閱讀 2928·2019-08-26 12:14
閱讀 1845·2019-08-26 12:00
閱讀 961·2019-08-26 11:44
閱讀 659·2019-08-26 11:44
閱讀 2830·2019-08-26 11:41