Android分發(fā)事件

ViewRoot里面有一個(gè)InputHandler,它開啟一個(gè)線程來(lái)接受native曾發(fā)送過來(lái)的消息,然后調(diào)用handlePoniter類似名字的方法,這個(gè)方法也會(huì)發(fā)一個(gè)異步消息DISPACTH_POINT,然后調(diào)用viewroot的deliverPointerEvent(),然后調(diào)用DecorView對(duì)象mView的dispacthTouchEvent(),不同的是,它沒有在這之前攔截消息給輸入法窗口。DecorView的dispacthTouchEvent如下:

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

final Callback cb = getCallback();

return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)

: super.dispatchTouchEvent(ev);

}

這里看看有沒有實(shí)現(xiàn)Window.CallBack接口,如果沒有則調(diào)用public boolean superDispatchTouchEvent(MotionEvent event) {

return super.dispatchTouchEvent(event);

}

也就是去ViewGroup中去遍歷了

2.又是Window.callBack對(duì)象的dispacthTouchEvent(),是不是很眼熟,這個(gè)跟keyEvent里面的一樣,這個(gè)CallBack接口,Activity實(shí)現(xiàn)了這個(gè)接口,程序流走到Actiivyt里面的dispactchTochEvent中:

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

onUserInteraction();

}

if (getWindow().superDispatchTouchEvent(ev)) {

return true;

}

return onTouchEvent(ev);

}

這里還是調(diào)用PhoneWindow對(duì)象也就是Actiivty的Window對(duì)象的superDispatchTouchEvent:

@Override

public boolean superDispatchTouchEvent(MotionEvent event) {

return mDecor.superDispatchTouchEvent(event);

}

其實(shí)還是DecorView的mDecor.superDispatchTouchEvent(event);,這個(gè)方法又回到了第一步走最后那段源碼中,也就是遍歷ViewTree去尋找消費(fèi)這個(gè)touEvent的targetView

當(dāng)然,如果沒有找到,那么調(diào)用Activity里面的onTouchEvent(ev);

public boolean onTouchEvent(MotionEvent event) {

if (mWindow.shouldCloseOnTouch(this, event)) {

finish();

return true;

}

return false;

}

3.以上是總的流程,至于ViewGroup里面的dispacthTouchEvent(),源碼如下,請(qǐng)看里面的注釋:

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) {

final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;

if (!disallowIntercept) {

//如果你的ViewGroup中有設(shè)置onInterceptTouchEvent這個(gè)方法的ActionDown時(shí)間并返回true,則此處不再下發(fā),而是ViewGroup自身消費(fèi)掉

1. ? ? 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.

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;

if (!canceled && !intercepted) {

// If the event is targeting accessiiblity 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 << 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 ArrayList preorderedList = buildOrderedChildList();

final boolean customOrder = preorderedList == null

&& isChildrenDrawingOrderEnabled();

final View[] children = mChildren;

2.如果沒有攔截,則走遍歷遞歸子View

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

final int childIndex = customOrder

? getChildDrawingOrder(childrenCount, i) : i;

final View child = (preorderedList == null)

? children[childIndex] : preorderedList.get(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;

}

if (!canViewReceivePointerEvents(child)

|| !isTransformedTouchPointInView(x, y, child, null)) {

ev.setTargetAccessibilityFocus(false);

continue;

}

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);

//for循環(huán)里面對(duì)childview檢查是否消費(fèi)觸摸事件,如果為true則break跳出循環(huán),這之前要把找到的touch保存到一個(gè)touchTarget對(duì)象中

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();

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;

}

}

}

3.上面遍歷找到的targetview會(huì)存放到newTouchTarget中,如果沒找到則會(huì)把firstTouchTarget給它,firstTouchTarget是least recently added target,如果mFirstTouchTarget也為空,那么就把viewgroup自己當(dāng)成一個(gè)普通的view來(lái)調(diào)用,也就是調(diào)用View.dispatchTouchEvent方法,如果不然,則對(duì)touchTarget的鏈表中的next就行循環(huán)找到targetView,并且調(diào)用這個(gè)dispatchTransformedTouchEvent來(lái)遍歷遞歸是否消費(fèi)本次事件,如果消費(fèi)則handle=ture.如果最終沒消費(fèi),則調(diào)用onUnhandedEvent方法,而firstTouchTarget是在for循環(huán)中給newTochTarget賦值的時(shí)候通過private TouchTarget addTouchTarget(View child, int pointerIdBits) {

TouchTarget target = TouchTarget.obtain(child, pointerIdBits);

target.next = mFirstTouchTarget;

mFirstTouchTarget = target;

return target;

}

所以正常情況下firstTouchTarget不會(huì)為空,如果為空說(shuō)明下面沒有找到,那么只能自己消費(fèi)掉

//-------------------------------------------------------------------------------------------------------------

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

}

綜上所述,dispatchKeyEvent與dispatchTouchEvent在ViewTree中找targetView是不同的,dispacthKey在從上往下,上層的viewgroup有focus則自己消費(fèi)掉,而dispatchTouchEvent則是先在child里面找,如果child沒有消費(fèi),這個(gè)時(shí)候它才會(huì)給自己。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容