自己对Android时间分发机制的理解。布局文件中从外到内为FramLayout——LinearLayout——Button。
当点击Button的时候,MotionEvent是这样传递的。
window.dispatchTouchEvent ——phoneWindow.dispatchTouchEvent——DecordView.dispatchTouchEvent——ViewGroup.dispatchTouchEvent——view.dispatchtouchEvent——view.onTouchEvent返回true,代表button消费。
ViewGroup.dispatchTouchEvent有两种情况,一种向下分发,但时间没有被处理,返回了false,则调用自身的onTouchEvent处理,一种是被拦截,不向下分发直接自己处理。
首先看一下事件被拦截情况,子view调用
getParent().requestDisallowInterceptTouchEvent(true);
去设置,即disallowIntercept为true,看一下dispatchTouchEvent对此的处理代码。
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) {
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;
}
resetTouchState代码如下
private void resetTouchState() {
clearTouchTargets();
resetCancelNextUpFlag(this);
mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT;
mNes
上面代码可以看出,如果是DOWN事件,会重置mGroupFlags状态,可以理解成把disallowIntercept重置为false,所以执行
getParent().requestDisallowInterceptTouchEvent(true);要在DOWN事件之后才有效,会直接向下传递事件。如果getParent().requestDisallowInterceptTouchEvent(false);执行onInterceptTouchEvent(ev)。
现提示,mFirstTouchTarget当Down事件被Button消费时,mFirstTouchTarget就被赋值为button。
dispatchTouchEvent方法无论拦截还是不拦截,最后都会执行下面代码
if (mFirstTouchTarget == null) {
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
}
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) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}}
当disallowIntercept为true,说明拦截,没有向下传递,所以事件没有被子view消费,则mFirstTouchTarget==null成立,调用super.dispatchTouchEvent,也就是调用super.dispatchTouchEvent,也就是在view.dispatchTouchEvent。 提前说明提示:在后面介绍另外一种情况,事件向下传递Down事件之后mFirstTouchTarget会被赋值,执行child.dispatchTouchEvent(event)。继续看view.dispatchTouchEvent中执行如下代码
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
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;
}
}
判断view是否可点击,注册mOnTouchListener事件以及ontouch返回true,则result=true,直接被ontouch消费,如果result为false,并且onTouchEvent为true,则result=true,事件被onTouchEvent消费,返回true,通知父view,时间被我消费。如果既没有注册mOnTouchListener或者是mOnTouchListener.ontouch返回false,或者onTouchEvent返回false,则view.dispathchTouchEvent返回false,事件向上传递。
另外一种情况是:disallowIntercept为false,执行onInterceptTouchEvent,默认返回false,如果返回true,则和以上逻辑一样。
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
返回false情况下,for循环遍历子view是在DOWN事件中执行的,判断当前点击的位置在哪个view/view group上,如果没有找到,既child==null,执行view的dispatchEvent方法。
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) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}
如果找到了,就调用那个相对应的的dispatchTouchEvent方法,并且事件被消费了,上面提到的mFirstTouchTarget就会被赋值。如果此刻的view属于viewgroup,执行逻辑和以上一样,都是viewgroup的执行,继续向下分发。如果是view,就执行view.dispatchTouchEvent方法。view.dispatchTouchEvent上面分析过了。
在onTouch中会调用performClick的方法
private final class PerformClick implements Runnable {
@Override
public void run() {
performClickInternal();
}
}
是一个子线程处理连续快速点击的情况,每次点击view的状态都会改变。
!post(mPerformClick)
public boolean post(Runnable action) {
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
return attachInfo.mHandler.post(action);
}
// Postpone the runnable until we know on which thread it needs to run.
// Assume that the runnable will be successfully placed after attach.
getRunQueue().post(action);
return true;
}
点击时间过多,把action添加到队列中,执行是用handler发送到主线程执行的。
另外注意,DOWN事件返回true,MOVE事件才会执行,如果中间一个Action返回了false,事件讲不在分发给此view。