Touch事件如何传递到Activity
基础知识
1、Activity、PhoneWindow、DecorView、ViewRootImpl
Activity {
mWindow = new PhoneWindow(this);
mWindow.setCallback(this);
}
PhoneWindow {
private DecorView mDecor;
private final class DecorView extends FrameLayout {
}
}
ViewRootImpl {
View mView; //是DecorView
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
...
mView = view;
...
view.assignParent(this);
...
}
}
简单来说,就是Activity里面有个PhoneWindow,PhoneWindow里面有个DecorView,而DecorView的parent是ViewRootImpl,所以Activity的很多方法调用都是从ViewRootImpl开始的
(可以参考View绘制过程(一)从Activity开始)
2、第一次启动App的时候,系统会为App新建一个进程,我们称为App进程,然后便执行
ActivityThread的main()方法,这就是主线程
public static void main(String[] args) {
...
Looper.prepareMainLooper();
...
ActivityThread thread = new ActivityThread();
thread.attach(false);
...
Looper.loop();
}
Looper.loop()是个死循环不断取消息,当有新消息时进入Handler处理
(可以参考Handler和Looper解析)
执行完Looper.loop(),此后所有通知App的事件都是通过给Looper的MessageQueue添加Message
开始
写个简单的Demo,在MainActivity里
findViewById(R.id.tv_title).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Thread.dumpStack();
return true;
}
});
看下Touch的调用栈,我们来一一分析标红色的方法
1、当我们触摸控件的时候,nativePollOnce()就会收到消息
2、nativePollOnce()后紧接着就是InputEventReceiver.dispatchInputEvent()
,从方法的注释就可以看出来是从native调用的,也就是nativePollOnce()内部调用了这个方法
// Called from native code.
@SuppressWarnings("unused")
private void dispatchInputEvent(int seq, InputEvent event) {
mSeqMap.put(event.getSequenceNumber(), seq);
onInputEvent(event);
}
3、在2里面我们并没有看到ViewRootImpl$WindowInputEventReceiver,那怎么进入ViewRootImpl$WindowInputEventReceiver.onInputEvent()
的呢?其实2里面的InputEventReceiver是WindowInputEventReceiver
ViewRootImpl {
void enqueueInputEvent(...) {
...
}
final class WindowInputEventReceiver extends InputEventReceiver {
public WindowInputEventReceiver(InputChannel inputChannel, Looper looper) {
super(inputChannel, looper);
}
@Override
public void onInputEvent(InputEvent event) {
enqueueInputEvent(event, this, 0, true);
}
...
}
}
所以Touch事件在应用层的源头是nativePollOnce()内部调用了
ViewRootImpl$WindowInputEventReceiver.dispatchInputEvent()
(引出一个疑问:系统与App通信经常都是通过Binder,但这里并不是,而是直接调用了ViewRootImpl$WindowInputEventReceiver,我们知道跨进程是没办法直接操作对方的对象,那又是如何通知到当前Activity的ViewRootImpl?
这个比较复杂,涉及较多native,个人推荐阅读http://www.cnblogs.com/lcw/p/3373214.html 难度深度适中,而老罗的http://blog.csdn.net/luoshengyang/article/details/6882903 实在是太细节太复杂,而且是较早的系统版本)
接下来我们跟踪Event如何传递到Activity
4、ViewRootImpl$ViewPostImeInputStage.processPointerEvent
final class ViewPostImeInputStage extends InputStage {
private int processPointerEvent(QueuedInputEvent q) {
final MotionEvent event = (MotionEvent)q.mEvent;
mAttachInfo.mUnbufferedDispatchRequested = false;
boolean handled = mView.dispatchPointerEvent(event);
if (mAttachInfo.mUnbufferedDispatchRequested && !mUnbufferedInputDispatch) {
mUnbufferedInputDispatch = true;
if (mConsumeBatchedInputScheduled) {
scheduleConsumeBatchedInputImmediately();
}
}
return handled ? FINISH_HANDLED : FORWARD;
}
...
}
关注mView.dispatchPointerEvent(event),上面说了mView就是DecorView
5、PhoneWindow$DecorView.dispatchTouchEvent()
public class PhoneWindow extends Window implements MenuBuilder.Callback {
...
private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final Callback cb = getCallback();
return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
: super.dispatchTouchEvent(ev);
}
...
}
}
Activity里mWindow.setCallback(this),cb就是Activity,这样就进入
Activity.dispatchTouchEvent()
接下来我们跟踪Event如何传递到View
6、Activity
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
7、PhoneWindow
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
8、DecorView
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
super.dispatchTouchEvent(event)就是调用ViewGroup的方法了,然后就是递归调用child.dispatchTouchEvent()(可以参考Touch事件分发逻辑)