[关闭]
@TryLoveCatch 2022-05-05T10:27:42.000000Z 字数 18127 阅读 552

Android知识体系之窗口机制

Android知识体系


窗口机制

Activity&Window&DecorView的关系

Activity&Window的关系

Activity启动流程 可以参考上面的,我们直接说ApplicationThread.scheduleLaunchActivity(),发送消息给H,然后H调用handleLaunchActivity

  1. private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
  2. ...
  3. // Initialize before creating the activity
  4. WindowManagerGlobal.initialize();
  5. Activity a = performLaunchActivity(r, customIntent);
  6. if (a != null) {
  7. r.createdConfig = new Configuration(mConfiguration);
  8. Bundle oldState = r.state;
  9. handleResumeActivity(r.token, false, r.isForward,
  10. ...
  11. }

再来看下performLaunchActivity

ActivityThread.performLaunchActivity

  1. ActivityThread.performLaunchActivity {
  2. //类似Application的创建过程,通过classLoader加载到activity.
  3. activity = mInstrumentation.newActivity(classLoader,
  4. component.getClassName(), r.intent);
  5. //因为Activity有界面,所以其Context是ContextThemeWrapper类型,但实现类仍是ContextImpl.
  6. Context appContext = createBaseContextForActivity(r, activity);
  7. activity.attach(context,mInstrumentation,application,...);
  8. //调用activity.oncreate
  9. mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
  10. ...
  11. //调用Activity的onstart方法
  12. activity.performStart();
  13. //调用activitu的OnRestoreInstanceState方法进行Window数据恢复
  14. mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,r.persistentState);
  15. }

由上面这些源码我们可以知道

所以,handleLaunchActivity里面会调用onCreate,onStart,onRestoreInstanceState,onResume;onCreate里面又回调用setContextView。还有就是,一个 Activity 界面的绘制,其实是在 onResume() 之后才开始的

在ActivityThread.performLaunchActivity中,创建Activity的实例,接着会调用Activity.attach()来初始化一些内容,而Window对象就是在attach里进行创建初始化赋值的。

Activity.attach

  1. final void attach(...) {
  2. ...
  3. mWindow = new PhoneWindow(this);
  4. mWindow.setWindowManager((WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, mComponent.flattenToString(),(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
  5. if (mParent != null) {
  6. mWindow.setContainer(mParent.getWindow());
  7. }
  8. mWindowManager = mWindow.getWindowManager();
  9. ...
  10. }

从上面两个代码可以看出来,Activity在onCreate之前,先创建了一个Window,即PhoneWindow,并设置了WindowManager,因此一个Activity对应着一个Window也就是应用窗口。
由上面的代码可知Window类的成员变量mWindowManager和Activity的成员变量mWindowManager都是指向同一个WindowManager对象,而WindowManager对象是调用如下代码获取:

  1. (WindowManager)context.getSystemService(Context.WINDOW_SERVICE)

而我们知道Activity是继承Context类的,ContextImpl类中实现了WindowManager服务的注册,代码如下:

  1. class ContextImpl extends Context {
  2. static {
  3. registerService(WINDOW_SERVICE, new ServiceFetcher() {
  4. Display mDefaultDisplay;
  5. public Object getService(ContextImpl ctx) {
  6. Display display = ctx.mDisplay;
  7. if (display == null) {
  8. if (mDefaultDisplay == null) {
  9. DisplayManager dm = (DisplayManager)ctx.getOuterContext().
  10. getSystemService(Context.DISPLAY_SERVICE);
  11. mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
  12. }
  13. display = mDefaultDisplay;
  14. }
  15. return new WindowManagerImpl(display);
  16. }});
  17. .......
  18. }
  19. }

有上面代码可知,在ContextImpl类中注册服务是一个静态代码块,也就是说值执行main方法之前就已经完成了WindowManager服务的注册。在整个应用第一次创建Context对象的时候就已经创建WindowManagerImpl对象了,所以,不管一个应用程序中有多少个Activity,但只有一个WindowManagerImpl对象。

Activity添加View

每个Activity都会在onCreate里面调用setContextView方法来加载布局视图,而这其实就是视图View添加到Activity窗口上的一个过程。加载布局视图代码如下Activity#setContentView:

  1. public void setContentView(int layoutResID) {
  2. getWindow().setContentView(layoutResID);
  3. initWindowDecorActionBar();
  4. }

PhoneWindow#setContentView

  1. public void setContentView(int layoutResID) {
  2. if (mContentParent == null) {
  3. //给窗口安装装饰视图DecorView
  4. installDecor();
  5. } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  6. mContentParent.removeAllViews();
  7. }
  8. if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
  9. final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
  10. getContext());
  11. transitionTo(newScene);
  12. } else {
  13. //加载xml布局视图内容到视图容器mContentParent中
  14. mLayoutInflater.inflate(layoutResID, mContentParent);
  15. }
  16. }

调用installDecor方法给窗口安装视图装饰,所谓视图装饰指的就是界面上看到的标题栏,导航栏Actionbar,也就是窗口的标题栏。

installDecor

  1. private void installDecor() {
  2. if (mDecor == null) {
  3. //
  4. mDecor = generateDecor();
  5. mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
  6. mDecor.setIsRootNamespace(true);
  7. if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
  8. mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
  9. }
  10. }
  11. if (mContentParent == null) {
  12. mContentParent = generateLayout(mDecor);
  13. }
  14. ........
  15. }

该方法中主要做了两件事:

generateDecor()

  1. protected DecorView generateDecor() {
  2. return new DecorView(getContext(), -1);
  3. }

generateLayout()

  1. protected ViewGroup generateLayout(DecorView decor) {
  2. int layoutResource; //窗口修饰布局文件
  3. if() {
  4. layoutResource = com.android.internal.R.layout.dialog_title_icons;
  5. } else if() {
  6. layoutResource = com.android.internal.R.layout.screen_title_icons;
  7. } else if() {
  8. layoutResource = com.android.internal.R.layout.screen_progress;
  9. } else if() {
  10. }
  11. ....
  12. View in = mLayoutInflater.inflate(layoutResource, null);
  13. decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  14. ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
  15. return contentParent;
  16. }
  1. 根据窗口的风格修饰类型为该窗口选择不同的窗口布局文件(根视图),例如FullScreen(全屏)、NoTitleBar(不含标题栏)等
  2. android:id="@android:id/content",根据这个id得到mContentParent,这个就是咱们setContentView的根布局,一般为FrameLayout

窗口布局文件,R.layout.screen_title

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:fitsSystemWindows="true">
  4. <FrameLayout
  5. android:layout_width="match_parent"
  6. android:layout_height="?android:attr/windowTitleSize"
  7. style="?android:attr/windowTitleBackgroundStyle">
  8. <TextView android:id="@android:id/title"
  9. style="?android:attr/windowTitleStyle"
  10. android:background="@null"
  11. android:fadingEdge="horizontal"
  12. android:gravity="center_vertical"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent" />
  15. </FrameLayout>
  16. <FrameLayout android:id="@android:id/content"
  17. android:layout_width="match_parent"
  18. android:layout_height="0dip"
  19. android:layout_weight="1"
  20. android:foregroundGravity="fill_horizontal|top"
  21. android:foreground="?android:attr/windowContentOverlay" />
  22. </LinearLayout>

全屏的窗口布局文件 R.layout.screen_simple:

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@android:id/content"
  3. android:fitsSystemWindows="true"
  4. android:foregroundInsidePadding="false"
  5. android:foregroundGravity="fill_horizontal|top"
  6. android:foreground="?android:attr/windowContentOverlay" />

然后,回过头来,在installDecor里面调用inflate方法将布局视图内容加载到刚刚获得的内容容器mContentParent上。

现在总结一下他们之间的关系:一个Activity中有一个Window对象用于描述当前应用的窗口,而Window是抽象类,实际上指向PhoneWindow类。PhoneWindow类中有一个内部类DecorView用于描述窗口的顶层视图,该类实际上是ViewGroup类型。它里面有一个@android:id/content指向的mContentParent,也是一个ViewGroup,我们setContextView里面设置进去的xml,会成为它的子 view。

DecorView添加到Window上

我们知道Activity中的PhoneWindow对象帮我们创建了一个PhoneWindow内部类DecorView(父类为FrameLayout)窗口顶层视图,然后通过LayoutInflater将xml内容布局解析成View树形结构添加到DecorView顶层视图中id为content的FrameLayout父容器上面。到此,我们已经知道Activity的content内容布局最终会添加到DecorView窗口顶层视图上面

那么,窗口顶层视图DecorView是怎么绘制到我们的手机屏幕上的呢?
上面我们说到

handleLaunchActivity里面会调用onCreate,onStart,onRestoreInstanceState,onResume;onCreate里面又回调用setContextView

所以接下来,我们看下onResume的调用。
我们上面说过,在handleLaunchActivity里面,performLaunchActivity之后,会调用handleResumeActivity:

  1. final void handleResumeActivity(IBinder token,
  2. boolean clearHide, boolean isForward, boolean reallyResume){
  3. //调用activity.onResume,把activity数据记录更新到ActivityClientRecord
  4. ActivityClientRecord r = performResumeActivity(token, clearHide);
  5. if (r != null) {
  6. final Activity a = r.activity;
  7. //activity.mStartedActivity是用来标记启动Activity,有没有带返回值,一般我们startActivity(intent)是否默认是startActivityForResult(intent,-1),默认值是-1,所以这里mStartedActivity = false
  8. boolean willBeVisible = !a.mStartedActivity;
  9. ...
  10. //mFinished标记Activity有没有结束,而r.window一开始activity并未赋值给ActivityClientRecord,所以这里为null
  11. if (r.window == null && !a.mFinished && willBeVisible) {
  12. r.window = r.activity.getWindow(); //赋值
  13. View decor = r.window.getDecorView();
  14. decor.setVisibility(View.INVISIBLE);
  15. ViewManager wm = a.getWindowManager();
  16. WindowManager.LayoutParams l = r.window.getAttributes();
  17. a.mDecor = decor;
  18. l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
  19. l.softInputMode |= forwardBit;
  20. if (a.mVisibleFromClient) {
  21. a.mWindowAdded = true;
  22. //把当前的DecorView与WindowManager绑定一起
  23. wm.addView(decor, l);
  24. }
  25. ...

performResumeActivity注释已经很清楚了,里面会回调onResume方法,这里就不详细说明了。一个 Activity 界面的绘制,其实是在 onResume() 之后才开始的。
我们主要看这句:

  1. wm.addView(decor, l);

很明显,这句就是把Activity的顶层视图DecorView添加到窗口视图上,我们来看addView方法
下面是WindowManagerImpl的源码

  1. public final class WindowManagerImpl implements WindowManager {
  2. private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
  3. private final Display mDisplay;
  4. private IBinder mDefaultToken;
  5. public WindowManagerImpl(Display display) {
  6. this(display, null);
  7. }
  8. private WindowManagerImpl(Display display, Window parentWindow) {
  9. mDisplay = display;
  10. mParentWindow = parentWindow;
  11. }
  12. public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
  13. return new WindowManagerImpl(mDisplay, parentWindow);
  14. }
  15. @Override
  16. public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
  17. applyDefaultToken(params);
  18. mGlobal.addView(view, params, mDisplay, mParentWindow);
  19. }
  20. @Override
  21. public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
  22. applyDefaultToken(params);
  23. mGlobal.updateViewLayout(view, params);
  24. }
  25. @Override
  26. public void removeView(View view) {
  27. mGlobal.removeView(view, false);
  28. }
  29. @Override
  30. public void removeViewImmediate(View view) {
  31. mGlobal.removeView(view, true);
  32. }
  33. @Override
  34. public Display getDefaultDisplay() {
  35. return mDisplay;
  36. }
  37. }

我们发现,其实都是调用了WindowManagerGlobal类相对应的方法:

  1. public final class WindowManagerGlobal {
  2. private final ArrayList<View> mViews = new ArrayList<View>();
  3. private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
  4. private final ArrayList<WindowManager.LayoutParams> mParams =
  5. new ArrayList<WindowManager.LayoutParams>();
  6. ........
  7. //单例模式构造方法
  8. public static WindowManagerGlobal getInstance() {
  9. synchronized (WindowManagerGlobal.class) {
  10. if (sDefaultWindowManager == null) {
  11. sDefaultWindowManager = new WindowManagerGlobal();
  12. }
  13. return sDefaultWindowManager;
  14. }
  15. }
  16. ........
  17. //窗口的添加过程
  18. public void addView(View view, ViewGroup.LayoutParams params,
  19. Display display, Window parentWindow) {
  20. final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
  21. if (parentWindow != null) {
  22. parentWindow.adjustLayoutParamsForSubWindow(wparams);
  23. } else {
  24. final Context context = view.getContext();
  25. if (context != null
  26. && context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
  27. wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
  28. }
  29. }
  30. ViewRootImpl root;
  31. View panelParentView = null;
  32. synchronized (mLock) {
  33. if (mSystemPropertyUpdater == null) {
  34. mSystemPropertyUpdater = new Runnable() {
  35. @Override public void run() {
  36. synchronized (mLock) {
  37. for (int i = mRoots.size() - 1; i >= 0; --i) {
  38. mRoots.get(i).loadSystemProperties();
  39. }
  40. }
  41. }
  42. };
  43. }
  44. //不能重复添加窗口
  45. int index = findViewLocked(view, false);
  46. if (index >= 0) {
  47. if (mDyingViews.contains(view)) {
  48. mRoots.get(index).doDie();
  49. } else {
  50. throw new IllegalStateException("View " + view
  51. + " has already been added to the window manager.");
  52. }
  53. }
  54. //判断当前窗口是否为子窗口,如果是则获得其父窗口并保存在panelParentView变量中
  55. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
  56. wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
  57. final int count = mViews.size();
  58. for (int i = 0; i < count; i++) {
  59. if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
  60. panelParentView = mViews.get(i);
  61. }
  62. }
  63. }
  64. //每一个窗口对应着一个ViewRootImpl对象
  65. root = new ViewRootImpl(view.getContext(), display);
  66. //给当前窗口视图设置参数
  67. view.setLayoutParams(wparams);
  68. //保存三个数组
  69. mViews.add(view);
  70. mRoots.add(root);
  71. mParams.add(wparams);
  72. }
  73. try {
  74. //真正执行窗口的视图View绘制工作的方法
  75. root.setView(view, wparams, panelParentView);
  76. }
  77. }
  78. //主要更新当前窗口的参数LayoutParams
  79. public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
  80. final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
  81. view.setLayoutParams(wparams);
  82. synchronized (mLock) {
  83. int index = findViewLocked(view, true);
  84. ViewRootImpl root = mRoots.get(index);
  85. mParams.remove(index);
  86. mParams.add(index, wparams);
  87. root.setLayoutParams(wparams, false);
  88. }
  89. }
  90. //从三个数组里面分别移除DecorView对象,ViewRootIpl对象,WindowManager.LayoutParams对象
  91. public void removeView(View view, boolean immediate) {
  92. synchronized (mLock) {
  93. int index = findViewLocked(view, true);
  94. View curView = mRoots.get(index).getView();
  95. removeViewLocked(index, immediate);
  96. if (curView == view) {
  97. return;
  98. }
  99. }
  100. }

在WindowManagerGlobal类中维系着三个数组分别是:

  • mViews:保存着当前应用所有窗口的顶层视图DecorView对象
  • mRoots:保存着当前应用所有窗口的视图绘制类ViewRootImpl
  • mParams:保存着当前应用所有窗口的参数 WindowManager.LayoutParams

另外,上面我们说过:

一个应用中不管有多少个Activity,都共用一个WindowManagerImpl对象,

因此:

而在WindowManagerImpl类中是单例模式获得WindowManagerGlobal对象,因此:一个应用中也就只有一个WindowManagerGlobal对象了。而在该对象中通过维护以上三个数组来维护一个应用中所有窗口的管理。

我们来看下addView方法:

  1. public void addView(View view, ViewGroup.LayoutParams params,Display display, Window parentWindow) {
  2. ...
  3. ViewRootImpl root = new ViewRootImpl(view.getContext(), display);
  4. view.setLayoutParams(wparams);
  5. mViews.add(view);
  6. mRoots.add(root);
  7. mParams.add(wparams);
  8. root.setView(view, wparams, panelParentView);
  9. ...
  10. }

这个过程创建一个ViewRootImpl,并将之前创建的DecoView作为参数传入,以后DecoView的事件都由ViewRootImpl来管理了,比如DecoView上添加View,删除View。
ViewRootImpl实现了ViewParent这个接口,这个接口最常见的一个方法是requestLayout()。
ViewRootImpl是个ViewParent,在DecoView添加的View时,就会将View中的ViewParent设为DecoView所在的ViewRootImpl,View的ViewParent相同时,理解为这些View在一个View链上。
所以每当调用View的requestLayout()时,其实是调用到ViewRootImpl,ViewRootImpl会控制整个事件的流程。可以看出一个ViewRootImpl对添加到DecoView的所有View进行事件管理。

我们接着看ViewRootImpl的setView方法:

  1. public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
  2. ...
  3. // Schedule the first layout -before- adding to the window
  4. // manager, to make sure we do the relayout before receiving
  5. // any other events from the system.
  6. requestLayout();
  7. ...
  8. try {
  9. ...
  10. res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,getHostVisibility(), mDisplay.getDisplayId(),mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,mAttachInfo.mOutsets, mInputChannel);
  11. }
  12. }。

这时候,我们在看下这种图,就会很清晰了

我们在看下requestLayout()这个方法

  1. @Override
  2. public void requestLayout() {
  3. if (!mHandlingLayoutInLayoutRequest) {
  4. checkThread();
  5. mLayoutRequested = true;
  6. scheduleTraversals();
  7. }
  8. }

checkThread判断线程一致行的,注意其实并不是判断是否时UI线程

  1. void checkThread() {
  2. if (mThread != Thread.currentThread()) {
  3. throw new CalledFromWrongThreadException(
  4. "Only the original thread that created a view hierarchy can touch its views.");
  5. }
  6. }

这里就涉及到一个问题,子线程是否可以更新UI,具体情况看这里 子线程是否可以更新UI

我们主要来看scheduleTraversals()

  1. void scheduleTraversals() {
  2. if (!mTraversalScheduled) {
  3. mTraversalScheduled = true;
  4. mTraversalBarrier = mHandler.getLooper().postSyncBarrier();
  5. mChoreographer.postCallback(
  6. Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
  7. if (!mUnbufferedInputDispatch) {
  8. scheduleConsumeBatchedInput();
  9. }
  10. notifyRendererOfFramePending();
  11. }
  12. }
  13. ..............
  14. final class TraversalRunnable implements Runnable {
  15. @Override
  16. public void run() {
  17. doTraversal();
  18. }
  19. }
  20. final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
  21. ...............
  22. void doTraversal() {
  23. if (mTraversalScheduled) {
  24. mTraversalScheduled = false;
  25. mHandler.getLooper().removeSyncBarrier(mTraversalBarrier);
  26. try {
  27. performTraversals();
  28. } finally {
  29. Trace.traceEnd(Trace.TRACE_TAG_VIEW);
  30. }
  31. }
  32. }
  33. ............

跟踪代码,最后DecorView的绘制会进入到ViewRootImpl类中的performTraversals()成员方法:

  1. private void performTraversals() {
  2. // cache mView since it is used so much below...
  3. //我们在Step3知道,mView就是DecorView根布局
  4. final View host = mView;
  5. //在Step3 成员变量mAdded赋值为true,因此条件不成立
  6. if (host == null || !mAdded)
  7. return;
  8. //是否正在遍历
  9. mIsInTraversal = true;
  10. //是否马上绘制View
  11. mWillDrawSoon = true;
  12. .............
  13. //顶层视图DecorView所需要窗口的宽度和高度
  14. int desiredWindowWidth;
  15. int desiredWindowHeight;
  16. .....................
  17. //在构造方法中mFirst已经设置为true,表示是否是第一次绘制DecorView
  18. if (mFirst) {
  19. mFullRedrawNeeded = true;
  20. mLayoutRequested = true;
  21. //如果窗口的类型是有状态栏的,那么顶层视图DecorView所需要窗口的宽度和高度就是除了状态栏
  22. if (lp.type == WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL
  23. || lp.type == WindowManager.LayoutParams.TYPE_INPUT_METHOD) {
  24. // NOTE -- system code, won't try to do compat mode.
  25. Point size = new Point();
  26. mDisplay.getRealSize(size);
  27. desiredWindowWidth = size.x;
  28. desiredWindowHeight = size.y;
  29. } else {//否则顶层视图DecorView所需要窗口的宽度和高度就是整个屏幕的宽高
  30. DisplayMetrics packageMetrics =
  31. mView.getContext().getResources().getDisplayMetrics();
  32. desiredWindowWidth = packageMetrics.widthPixels;
  33. desiredWindowHeight = packageMetrics.heightPixels;
  34. }
  35. }
  36. ............
  37. //获得view宽高的测量规格,mWidth和mHeight表示窗口的宽高,lp.widthhe和lp.height表示DecorView根布局宽和高
  38. int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
  39. int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
  40. // Ask host how big it wants to be
  41. //执行测量操作
  42. performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
  43. ........................
  44. //执行布局操作
  45. performLayout(lp, desiredWindowWidth, desiredWindowHeight);
  46. .......................
  47. //回调OnGlobalLayoutListener,在meausre、layout之后,draw之前
  48. if (triggerGlobalLayoutListener) {
  49. mAttachInfo.mRecomputeGlobalAttributes = false;
  50. mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
  51. }
  52. .......................
  53. //执行绘制操作
  54. performDraw();
  55. }

performTraversals方法会经过measure、layout和draw三个过程才能将一个View绘制出来,所以View的绘制是ViewRootImpl完成的,另外当手动调用invalidate,postInvalidate,requestInvalidate也会最终调用performTraversals,来重新绘制View。

这里面还有一个问题,我们通过上面的分析可以知道,在onCreate和onResume里面调用getMeasureHeight() = 0是没有问题,平时我们获取的时候,可以使用监听:

  1. view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  2. @Override
  3. public void onGlobalLayout() {
  4. // TODO Auto-generated method stub
  5. }
  6. });

上面的代码已经很清楚了:

  1. //执行测量操作
  2. performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
  3. ........................
  4. //执行布局操作
  5. performLayout(lp, desiredWindowWidth, desiredWindowHeight);
  6. .......................
  7. //回调OnGlobalLayoutListener,在meausre、layout之后,draw之前
  8. if (triggerGlobalLayoutListener) {
  9. mAttachInfo.mRecomputeGlobalAttributes = false;
  10. mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
  11. }
  12. .......................
  13. //执行绘制操作
  14. performDraw();

OnGlobalLayoutListener,是在meausre、layout之后,draw之前回调的。

  • ActivityThread.performLaunchActivity()
  • Activity.attach()
  • Activity.onCreate()
  • Activity.setContentView()
  • PhoneWindow.setContentView()
  • PhoneWindow.installDecor()
    • PhoneWindow.generateDecor():创建DecorView对象,继承FrameLayout
    • PhoneWindow.generateLayout():根据不通的窗口风格,选择不通的layout文件,并添加到DecorView,返回id为content的FrameLayout,作为
  • ActivityThread.performResumeActivity()

小结

参考

https://blog.csdn.net/freekiteyu/article/details/79408969

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注