前言
我们都知道处理Android的手势问题时,都是通过Activity的 onTouchEvent 事件来处理的,那么今天我们来看看手势信息是从哪里传递来的呢
1. 入口
通过我们之前的Android View绘制流程中,我们知道在ViewRootImpl的setView方法中,我们生成了一个WindowInputEventReceiver对象,这个对象就是用来监听输入事件的。
ViewRootImpl类
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView,
int userId) {
synchronized (this) {
if (mView == null) {
......
if (inputChannel != null) {
......
//注册一个输入事件监听,当有输入事件时,会触发它的onInputEvent事件,见下方
mInputEventReceiver = new WindowInputEventReceiver(inputChannel,
Looper.myLooper());
......
}
}
}
}
WindowInputEventReceiver 类
@Override
public void onInputEvent(InputEvent event) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "processInputEventForCompatibility");
List<InputEvent> processedEvents;
try {
processedEvents =
mInputCompatProcessor.processInputEventForCompatibility(event);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
if (processedEvents != null) {
if (processedEvents.isEmpty()) {
// InputEvent consumed by mInputCompatProcessor
finishInputEvent(event, true);
} else {
for (int i = 0; i < processedEvents.size(); i++) {
//将输入事件插入队列
enqueueInputEvent(
processedEvents.get(i), this,
QueuedInput