理性分析 Window、Activity、DecorView 以及 ViewRoot 之间关系

系统 Windows
ViewRoot并不属于View树的一部分。从源码实现上来看,它既非View的子类,也非View Group,但它实现了ViewParent接口,这让它可以作为View的名义上的父视图。

[[433927]]

文末本文转载自微信公众号「 Android开发编程」,作者 Android开发编程。转载本文请联系 Android开发编程公众号。

前言

Activity和window,DecorView ,viewRoot是什么关系

今天我们就来讲解下,这样你在面试时候,游刃有余;

一、基本概念介绍

1、Activity

  • Activity负责控制生命周期和处理事件;
  • 负责统筹视图的添加与显示,以及通过一些回调方法与Window和View进行交互;
  • 一个Activity包含一个Window,真正控制视图的是Window,Window才是真正代表一个窗口;
  • 统筹视图的添加与显示,通过回调与Window和View进行交互;

2、Window

  • Window是视图的承载者,是一个抽象类;
  • Activity中持有的实际上是Window的子类PhoneWindow;
  • Window通过WindowManager加载了一个DecorView到Window中,并将DecorView交给了ViewRoot;

3、DecorView

  • DecorView的父类是FrameLayout,是Android View树的根节;
  • 内部包含一个竖直方向的LinearLayout,它有上下三个部分,上面是个ViewStub,延迟加载的视图(ActionBar,根据Theme设置),中间的是标题栏(根据Theme设置,有的布局没有),下面的是内容栏。setContentView所设置的布局文件其实就是被加到内容栏之中的;
  1. ViewGroup content = (ViewGroup)findViewById(android.R.id.content); 
  2. ViewGroup rootView = (ViewGroup) content.getChildAt(0) 

 4、ViewRoot

  • 控制View的事件处理和逻辑处理;
  • ViewRoot子类是ViewRootImpl类,它是连接WindowManagerService和DecorView的纽带,View的三大流程(测量(measure),布局(layout),绘制(draw))均通过ViewRoot来完成;
  • ViewRoot并不属于View树的一部分。从源码实现上来看,它既非View的子类,也非View Group,但它实现了ViewParent接口,这让它可以作为View的名义上的父视图;
  • RootView继承了Handler类,可以接收事件并分发;
  • Android的所有触屏事件、按键事件、界面刷新等事件都是通过ViewRoot进行分发的;

二、DecorView的创建整个流程详解

1、attach

Activity的setContentView()开始

  1. public void setContentView(@LayoutRes int layoutResID) { 
  2. getWindow().setContentView(layoutResID); 
  3. initWindowDecorActionBar(); 

 可以看到实际上是交给Window来装载视图的;

  1. final void attach(Context context, ActivityThread aThread, 
  2. Instrumentation instr, IBinder token, int ident, 
  3. Application application, Intent intent, ActivityInfo info, 
  4. CharSequence title, Activity parent, String id, 
  5. NonConfigurationInstances lastNonConfigurationInstances, 
  6. Configuration config, String referrer, IVoiceInteractor voiceInteractor, 
  7. Window window) { 
  8. .................................................................. 
  9.         mWindow = new PhoneWindow(this, window);//创建一个Window对象 
  10.         mWindow.setWindowControllerCallback(this); 
  11.         mWindow.setCallback(this);//设置回调,向Activity分发点击或状态改变等事件 
  12.         mWindow.setOnWindowDismissedCallback(this); 
  13.         ................................................................. 
  14.         mWindow.setWindowManager( 
  15.         (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), 
  16.         mToken, mComponent.flattenToString(), 
  17.                 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);//给Window设置WindowManager对象 
  18.         .................................................................... 

 在Activity的attach方法中生成了PhoneWindow的实例;

有了Window对象,接下来就将DecorView加载到Window中;

2、setContentView

  1. public void setContentView(int layoutResID) { 
  2.     if (mContentParent == null) {//mContentParent为空,创建一个DecroView 
  3.     installDecor(); 
  4.     } else { 
  5.         mContentParent.removeAllViews();//mContentParent不为空,删除其中的View 
  6.     } 
  7.     mLayoutInflater.inflate(layoutResID, mContentParent);//为mContentParent添加子View,即Activity中设置的布局文件 
  8.     final Callback cb = getCallback(); 
  9.     if (cb != null && !isDestroyed()) { 
  10.         cb.onContentChanged();//回调通知,内容改变 
  11.     } 

 mContentParent就是ContentView所对应的的FrameLayout;

Activity的setContentView的流程大致可以总结为:

Activity首先在Attach方法中生成了PhoneWindow的实例;

在setContentView中直接交给Window来装载视图,先在PhoneWindow中创建了一个DecroView;

其中创建的过程中可能根据Theme不同,加载不同的布局格式,即Activity中设置的布局;

3、installDecor

  1. private void installDecor() { 
  2.     if (mDecor == null) { 
  3.         mDecor = generateDecor(); //生成DecorView 
  4.         mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
  5.         mDecor.setIsRootNamespace(true); 
  6.         if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) { 
  7.             mDecor.postOnAnimation(mInvalidatePanelMenuRunnable); 
  8.         } 
  9.     } 
  10.     if (mContentParent == null) { 
  11.         mContentParent = generateLayout(mDecor); // 为DecorView设置布局格式,并返回mContentParent 
  12.         ... 
  13.         }  
  14.     } 
  15. protected DecorView generateDecor() { 
  16.     return new DecorView(getContext(), -1); 

 很简单,创建了一个DecorView;

再看generateLayout;

4、generateLayout

  1. protected ViewGroup generateLayout(DecorView decor) { 
  2.     // 从主题文件中获取样式信息 
  3.     TypedArray a = getWindowStyle(); 
  4.     ................... 
  5.     if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) { 
  6.         requestFeature(FEATURE_NO_TITLE); 
  7.     } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) { 
  8.         // Don't allow an action bar if there is no title. 
  9.         requestFeature(FEATURE_ACTION_BAR); 
  10.     } 
  11.     ................ 
  12.     // 根据主题样式,加载窗口布局 
  13.     int layoutResource; 
  14.     int features = getLocalFeatures(); 
  15.     // System.out.println("Features: 0x" + Integer.toHexString(features)); 
  16.     if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) { 
  17.         layoutResource = R.layout.screen_swipe_dismiss; 
  18.     } else if(...){ 
  19.         ... 
  20.     } 
  21.     View in = mLayoutInflater.inflate(layoutResource, null);//加载layoutResource 
  22.     //往DecorView中添加子View,即文章开头介绍DecorView时提到的布局格式,那只是一个例子,根据主题样式不同,加载不同的布局。 
  23.     decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));  
  24.     mContentRoot = (ViewGroup) in
  25.     ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);// 这里获取的就是mContentParent 
  26.     if (contentParent == null) { 
  27.         throw new RuntimeException("Window couldn't find content container view"); 
  28.     } 
  29.     if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { 
  30.         ProgressBar progress = getCircularProgressBar(false); 
  31.         if (progress != null) { 
  32.             progress.setIndeterminate(true); 
  33.         } 
  34.     } 
  35.     if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) { 
  36.         registerSwipeCallbacks(); 
  37.     } 
  38.     // Remaining setup -- of background and title -- that only applies 
  39.     // to top-level windows. 
  40.     ... 
  41.     return contentParent; 
  •  先从主题中获取样式,然后根据样式;
  • 加载对应的布局到DecorView中,然后从中获取mContentParent;
  • 获得到之后,可以回到上面的代码,为mContentParent添加View,即Activity中的布局;

5、DecorView的显示

将DecorView建立起来,通过setContentView设置的界面,如何在onResume后对用户可见,需要从ActivityThread说起;

  1. private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) { 
  2.     //就是在这里调用了Activity.attach(),接着调用了Activity.onCreate()和Activity.onStart()生命周期, 
  3.     //但是由于只是初始化了mDecor,添加了布局文件,还没有把 
  4.     //mDecor添加到负责UI显示的PhoneWindow中,所以这时候对用户来说,是不可见的 
  5.     Activity a = performLaunchActivity(r, customIntent); 
  6.     ...... 
  7.     if (a != null) { 
  8.     //这里面执行了Activity.onResume() 
  9.     handleResumeActivity(r.token, false, r.isForward, 
  10.                         !r.activity.mFinished && !r.startsNotResumed); 
  11.     if (!r.activity.mFinished && r.startsNotResumed) { 
  12.         try { 
  13.                 r.activity.mCalled = false
  14.                 //执行Activity.onPause() 
  15.                 mInstrumentation.callActivityOnPause(r.activity); 
  16.                 } 
  17.         } 
  18.     } 

 重点看下handleResumeActivity(),在这其中,DecorView将会显示出来,同时重要的一个角色;ViewRoot也将登场;

6、handleResumeActivity

  1. final void handleResumeActivity(IBinder token, boolean clearHide,  
  2.                                 boolean isForward, boolean reallyResume) { 
  3.     //这个时候,Activity.onResume()已经调用了,但是现在界面还是不可见的 
  4.     ActivityClientRecord r = performResumeActivity(token, clearHide); 
  5.     if (r != null) { 
  6.         final Activity a = r.activity; 
  7.         if (r.window == null && !a.mFinished && willBeVisible) { 
  8.             r.window = r.activity.getWindow(); 
  9.             View decor = r.window.getDecorView(); 
  10.             //decor对用户不可见 
  11.             decor.setVisibility(View.INVISIBLE); 
  12.             ViewManager wm = a.getWindowManager(); 
  13.             WindowManager.LayoutParams l = r.window.getAttributes(); 
  14.             a.mDecor = decor; 
  15.             l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; 
  16.             if (a.mVisibleFromClient) { 
  17.                 a.mWindowAdded = true
  18.                 //被添加进WindowManager了,但是这个时候,还是不可见的 
  19.                 wm.addView(decor, l); 
  20.             } 
  21.             if (!r.activity.mFinished && willBeVisible 
  22.                     && r.activity.mDecor != null && !r.hideForNow) { 
  23.                 //在这里,执行了重要的操作,使得DecorView可见 
  24.                 if (r.activity.mVisibleFromClient) { 
  25.                     r.activity.makeVisible(); 
  26.                 } 
  27.             } 
  28.         } 
  29.     } 

 当我们执行了Activity.makeVisible()方法之后,界面才对我们是可见的;

  1. void makeVisible() { 
  2.    if (!mWindowAdded) { 
  3.         ViewManager wm = getWindowManager(); 
  4.         wm.addView(mDecor, getWindow().getAttributes());//将DecorView添加到WindowManager 
  5.         mWindowAdded = true
  6.     } 
  7.     mDecor.setVisibility(View.VISIBLE);//DecorView可见 
  •  到此DecorView便可见,显示在屏幕中;
  • 但是在这其中,wm.addView(mDecor, getWindow().getAttributes());
  • 起到了重要的作用,因为其内部创建了一个ViewRootImpl对象,负责绘制显示各个子View;
  • 具体来看addView()方法,因为WindowManager是个接口,具体是交给WindowManagerImpl来实现的;

7、addView

  1. public final class WindowManagerImpl implements WindowManager {     
  2.     private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); 
  3.     ... 
  4.     @Override 
  5.     public void addView(View view, ViewGroup.LayoutParams params) { 
  6.         mGlobal.addView(view, params, mDisplay, mParentWindow); 
  7.     } 
  8. 交给WindowManagerGlobal 的addView()方法去实现; 
  9. public void addView(View view, ViewGroup.LayoutParams params, 
  10.                     Display display, Window parentWindow) { 
  11.     final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; 
  12.     ...... 
  13.     synchronized (mLock) { 
  14.         ViewRootImpl root; 
  15.         //实例化一个ViewRootImpl对象 
  16.         root = new ViewRootImpl(view.getContext(), display); 
  17.         view.setLayoutParams(wparams); 
  18.         mViews.add(view); 
  19.         mRoots.add(root); 
  20.         mParams.add(wparams); 
  21.     } 
  22.     ...... 
  23.     try { 
  24.         //将DecorView交给ViewRootImpl 
  25.         root.setView(view, wparams, panelParentView); 
  26.     } catch (RuntimeException e) { 
  27.     } 
  •  看到其中实例化了ViewRootImpl对象,然后调用其setView()方法;
  • 其中setView()方法经过一些列折腾,最终调用了performTraversals()方法,完成绘制,最终界面才显示出来;

总结

  • Activity就像个控制器,不负责视图部分。Window像个承载器,装着内部视图;
  • DecorView就是个顶层视图,是所有View的最外层布局;
  • ViewRoot像个连接器,负责沟通,通过硬件的感知来通知视图,进行用户之间的交互;

 

责任编辑:武晓燕 来源: Android开发编程
相关推荐

2009-04-17 22:25:16

多核四核CPU

2017-02-21 12:20:20

Android事件分发机制实例解析

2012-10-31 09:20:45

数据中心制冷PUE

2016-12-12 09:58:47

AndroidAndroid Vie

2010-03-22 09:43:00

无线交换机

2021-11-04 09:37:31

Android截屏实现方式监听截屏

2013-11-14 17:02:41

Android多窗口

2010-01-11 11:09:10

C++语法

2011-05-19 17:49:08

ActivityAndroid开发

2024-03-04 11:12:20

大数据物联网

2021-01-14 12:17:52

大数据数据分析技术

2009-06-25 14:46:50

JDKJREJVM

2023-03-07 13:28:17

2015-12-09 09:47:50

2012-05-31 14:54:59

Hadoop大数据

2011-08-08 09:51:52

Cocoa 框架

2010-08-03 16:21:54

FlexFlash

2015-03-09 11:01:43

2020-05-12 16:58:05

LinuxUnix技术

2012-12-27 15:29:33

Android开发Activity
点赞
收藏

51CTO技术栈公众号