日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )

發布時間:2025/6/17 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 事件分發 系列文章目錄


【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )



文章目錄

  • Android 事件分發 系列文章目錄
  • 一、Activity 的事件傳遞
  • 二、PhoneWindow 事件傳遞
  • 三、DecorView 事件傳遞
  • 四、ViewGroup 事件傳遞





一、Activity 的事件傳遞



手指觸摸到 Android 手機屏幕時 , 先由硬件驅動層產生事件 , 然后傳遞到 Framework 層 , 之后傳遞到 AMS , 最后到 Activity 界面中 ;

在 Activity 界面中 , 會第一時間調用 dispatchTouchEvent 方法 , 然后會按照下圖的層級 , 逐步向下分發觸摸事件 ;

Activity | dispatchTouchEvent 分析 :

先判定該觸摸事件是否是按下操作 , MotionEvent.ACTION_DOWN ;

觸摸事件有 444 種操作 :

  • 按下 : MotionEvent.ACTION_DOWN ;
  • 移動 : MotionEvent.ACTION_MOVE ;
  • 抬起 : MotionEvent.ACTION_UP ;
  • 取消 : MotionEvent.ACTION_CANCEL , 手指移出組件邊界 , 產生取消事件 ;

Activity | dispatchTouchEvent 源碼示例 :

public class Activity extends ContextThemeWrapperimplements LayoutInflater.Factory2,Window.Callback, KeyEvent.Callback,OnCreateContextMenuListener, ComponentCallbacks2,Window.OnWindowDismissedCallback, WindowControllerCallback,AutofillManager.AutofillClient {/*** 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();}// getWindow() 獲取的是 PhoneWindow 窗口 // 調用 PhoneWindow 的 superDispatchTouchEvent ; if (getWindow().superDispatchTouchEvent(ev)) {return true;}return onTouchEvent(ev);}public void onUserInteraction() {} }

源碼地址 : /frameworks/base/core/java/android/app/Activity.java





二、PhoneWindow 事件傳遞



在上述 Activity 的 dispatchTouchEvent 方法中 , 調用了 PhoneWindow 的 superDispatchTouchEvent 方法 ;
Activity 的下一層是 PhoneWindow ;

PhoneWindow | superDispatchTouchEvent 源碼 :

public class PhoneWindow extends Window implements MenuBuilder.Callback {// This is the top-level view of the window, containing the window decor.private DecorView mDecor;@Overridepublic boolean superDispatchTouchEvent(MotionEvent event) {return mDecor.superDispatchTouchEvent(event);} }

源碼路徑 : /frameworks/base/core/java/com/android/internal/policy/PhoneWindow.java





三、DecorView 事件傳遞



在 PhoneWindow 的 superDispatchTouchEvent 方法中 , 調用了 DecorView 的 superDispatchTouchEvent 方法 ;
PhoneWindow 的下一層是 DecorView ;

DecorView | superDispatchTouchEvent 源碼 :

public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks {public boolean superDispatchTouchEvent(MotionEvent event) {return super.dispatchTouchEvent(event);} }

源碼路徑 : /frameworks/base/core/java/com/android/internal/policy/DecorView.java





四、ViewGroup 事件傳遞



DecorView 中的 superDispatchTouchEvent 中 , 調用父類的 superDispatchTouchEvent 方法 , 這里涉及到事件分發 superDispatchTouchEvent 方法的 DecorView 的父類是 ViewGroup ;

DecorView 是 FrameLayout 的子類 , FrameLayout 是 ViewGroup 的子類 ;

@UiThread public abstract class ViewGroup extends View implements ViewParent, ViewManager {@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {} }

源碼路徑 : /frameworks/base/core/java/android/view/ViewGroup.java

總結

以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。