1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Called to process touch screen events. You can override this to
* intercept all touch screen events before they are dispatched to the
* window. Be sure to call this implementation for touch screen events
* that should be handled normally.
*
* @param ev The touch screen event.
*
* @return boolean Return true if this event was consumed.
*/
public
boolean
dispatchTouchEvent(MotionEvent ev) {
if
(ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if
(getWindow().superDispatchTouchEvent(ev)) {
return
true
;
}
return
onTouchEvent(ev);
}
来分发事件, 这里的逻辑是:
先让用户界面窗口处理:getWindow().superDispatchTouchEvent(ev)
如果窗口没有处理这个事件.
那就交给 Activity 自己处理.return onTouchEvent(ev)
这个
Window 跟 View 层级是怎么交互的呢?
我们找到了 Window 的实现类:PhoneWindow(com.android.internal.policy.impl.PhoneWindow)
?
1
2
3
4
@Override
public
boolean
superDispatchTouchEvent(MotionEvent event) {
return
mDecor.superDispatchTouchEvent(event);
}
这个
mDecor 就是用户界面的根 View 了.
private final class DecorView extends FrameLayout
(com.android.internal.policy.impl.PhoneWindow.DecorView)
原来窗口将事件交给根
View 来进行事件派发的.
mDecor 调用自己的 superDispatchTouchEvent(event)