参考资料:
Android事件传递机制
Android事件分发机制(郭霖)
Android触摸屏事件派发机制详解与源码分析一(View篇)–工匠若水
Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)–工匠若水
Android事件分发机制详解:史上最全面、最易懂
极客学院
Android开发艺术探索
了解Android事件分发/传递机制是学习自定义控件的基础,所以绝对有必要通过阅读源码来深入理解事件分发的机制和流程,在此综合各个渠道的资料,对事件分发做了一个总结归纳,仅作为个人学习用途。感谢互联网的开放,感谢所有前辈。
方法介绍
// View:
// 进行事件分发
public boolean dispatchTouchEvent(MotionEvent event)
// 处理点击事件
public boolean onTouchEvent(MotionEvent event)
// ViewGroup:
public boolean dispatchTouchEvent(MotionEvent event)
public boolean onTouchEvent(MotionEvent event)
// 拦截事件
public boolean onInterceptTouchEvent(MotionEvent ev)
事件分发的流程可以用如下伪代码来描述:
public boolean dispatchTouchEvent (MotionEvent ev) {
boolean consume = false;
if (onInterceptTouchEvent(ev)) {
consume = onTouchEvent(ev);
} else {
consume = child.dispatchTouchEvent(ev);
}
return consuem;
}
事件的分发顺序:Activity->Window->ViewGroup->View,所以我们先来分析Activity的事件分发过程。
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
PhoneWindow.java
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
boolean handled = mDecor.superDispatchTouchEvent(event);
return handled;
}
DecorView.java
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
ViewGroup.java
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
}
首先从简单一点的View的事件分发机制入手,ViewGroup多了一个onInterceptTouchEvent()方法。
Button和ImageView关于事件传递的区别:
1. Button默认是clickable,ImageView/TextView默认不是clickable
2. ImageView设置click监听之后,会变成clickable
3. 由于ImageView是不可点击的,所以在onTouchEvent中默认不会消耗事件(return false),从而dispatchTouchEvent不会派发下一次事件
4. 在ImageView的onTouch方法中返回true,则会跳过onTouchEvent,dispatchTouchEvent收到了true,进而派发下一次事件。
View—->dispatchTouchEvent:
// 关键代码
public boolean dispatchTouchEvent(MotionEvent event) {
...
if (onFilterTouchEventForSecurity(event)) {
ListenerInfo li = mListenerInfo;
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;
}
View—->onTouchEvent:
// 关键代码:
public boolean onTouchEvent(MotionEvent event) {
...
if ((viewFlags & ENABLED_MASK) == DISABLED) {
...
return (((viewFlags & CLICKABLE) == CLICKABLE
|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE);
}
...
if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) ||
(viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE) {
switch (action) {
case MotionEvent.ACTION_UP:
...
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
...
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
...
if (!focusTaken) {
...
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
...
}
}
...
break;
case MotionEvent.ACTION_DOWN:
...
break;
case MotionEvent.ACTION_CANCEL:
...
break;
case MotionEvent.ACTION_MOVE:
...
break;
}
return true;
}
return false;
}
ViewGroup—->dispatchTouchEvent:
if (actionMasked == MotionEvent.ACTION_DOWN || mFirstTouchTarget != null)
这一条判断语句说明当事件为ACTION_DOWN或者mFirstTouchTarget不为null(即已经找到能够接收touch事件的目标组件)时if成立。否则if不成立,将intercepted设置为true,也即拦截事件。当事件为ACTION_DOWN或者mFirstTouchTarget不为null时判断disallowIntercept(禁止拦截)标志位,而这个标记在ViewGroup中提供了public的设置方法,即requestDisallowInterceptTouchEvent(boolean disallowIntercept)
到此ViewGroup的dispatchTouchEvent方法分析完毕。
public boolean dispatchTouchEvent(MotionEvent ev) {
...
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
// Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
...
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) {
intercepted = onInterceptTouchEvent(ev);
/// M : add log to help debugging
...
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
...
intercepted = true;
}
...
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) {
...
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
...
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
...
}
}
...
}
}
// 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;
}
...
}
predecessor = target;
target = next;
}
}
...
return handled;
}