天天记录 - Android Touch Event接收并传递给Activity
来源:互联网 发布:网络电视看cctv5的apk 编辑:程序博客网 时间:2024/06/15 07:10
代码执行堆栈
ViewRootImpl.dispatchMotion
ViewRootImpl.dispatchPointer (sendMessage)
ViewRootImpl.handleMessage@Override public void handleMessage(Message msg) { ...... case DISPATCH_POINTER: deliverPointerEvent((MotionEvent) msg.obj, msg.arg1 != 0); ...... }
ViewRootImpl.deliverPointerEvent
private void deliverPointerEvent(MotionEvent event, boolean sendDone) { if (ViewDebug.DEBUG_LATENCY) { mInputEventDeliverTimeNanos = System.nanoTime(); } final boolean isTouchEvent = event.isTouchEvent(); if (mInputEventConsistencyVerifier != null) { if (isTouchEvent) { mInputEventConsistencyVerifier.onTouchEvent(event, 0); } else { mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0); } } // If there is no view, then the event will not be handled. if (mView == null || !mAdded) { finishMotionEvent(event, sendDone, false); return; } // Translate the pointer event for compatibility, if needed. if (mTranslator != null) { mTranslator.translateEventInScreenToAppWindow(event); } // Enter touch mode on down or scroll. final int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_SCROLL) { ensureTouchMode(true); } // Offset the scroll position. if (mCurScrollY != 0) { event.offsetLocation(0, mCurScrollY); } if (MEASURE_LATENCY) { lt.sample("A Dispatching PointerEvents", System.nanoTime() - event.getEventTimeNano()); } // Remember the touch position for possible drag-initiation. if (isTouchEvent) { mLastTouchPoint.x = event.getRawX(); mLastTouchPoint.y = event.getRawY(); } // Dispatch touch to view hierarchy. // 派发Touch Event boolean handled = mView.dispatchPointerEvent(event); if (MEASURE_LATENCY) { lt.sample("B Dispatched PointerEvents ", System.nanoTime() - event.getEventTimeNano()); } if (handled) { finishMotionEvent(event, sendDone, true); return; } // Pointer event was unhandled. finishMotionEvent(event, sendDone, false); }
mView由PhoneWindow.DecorView实现,即执行PhoneWindow.DecorView.dispatchPointerEvent
@Override public boolean dispatchTouchEvent(MotionEvent ev) { final Callback cb = getCallback(); // cb != null cb就是拥有这个PhoneWindow的Activity // return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev) : super.dispatchTouchEvent(ev); }
Callback就是Activity,通过分析获知以上判断执行cb.dispatchTouchEvent即Activity.dispatchTouchEvent
/** * 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); }
接着执行 PhoneWindow.superDispatchTouchEvent
@Override public boolean superDispatchTouchEvent(MotionEvent event) { return mDecor.superDispatchTouchEvent(event); }
执行PhoneWindow.DecorView.superDispatchTouchEvent
public boolean superDispatchTouchEvent(MotionEvent event) { return super.dispatchTouchEvent(event); }
ViewGroup.dispatchTouchEvent 通过UI树,向下传递Touch Event
Activity.onTouchEvent
/** * Called when a touch screen event was not handled by any of the views * under it. This is most useful to process touch events that happen * outside of your window bounds, where there is no view to receive it. * * @param event The touch screen event being processed. * * @return Return true if you have consumed the event, false if you haven't. * The default implementation always returns false. */ public boolean onTouchEvent(MotionEvent event) { if (mWindow.shouldCloseOnTouch(this, event)) { finish(); return true; } return false; }
- 天天记录 - Android Touch Event接收并传递给Activity
- 天天记录 - Android Touch事件学习 - 修改MotionEvent的值
- 捕获Activity中的Touch Event
- 天天记录 - Android Activity 启动流程简单整理
- android touch event summary
- 天天记录 - Ubuntu 12.04编译并配置Android Windows SDK
- 【Android开发】启动Activity并传递信息
- Android:Activity之间传递参数并跳转
- UIView传递Touch Event 等事件
- Android Touch Event 超出范围
- android touch事件传递
- Android Touch事件传递
- Android touch事件传递
- Android touch 事件传递
- Android Touch事件传递
- Android Touch事件传递
- 天天记录 - Android AsyncTask 缺陷
- 天天记录 - Android PopupWindow例子
- Java 桥梁模式
- 学习最怕认真二字。牢记活用,系统整体牢固活用,忌孤立片面松散僵硬
- C# 正则表达式
- Scrum之Story Points
- POJ 1222 熄灯问题
- 天天记录 - Android Touch Event接收并传递给Activity
- MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
- Solr(1)环境搭建
- MS Sql索引建立---(状态位或标示位)
- 如何进行shell脚本正确性测试
- dex2jar apktool 工具的使用
- Flash元标签
- 润乾报表根据浏览器窗体自动缩放
- Solr(2)的实例