一. DecorView的创建
1. 创建Window抽象类的子类PhoneWindow类的实例对象
2.为PhoneWindow类对象设置WindowManager对象
3.为PhoneWindow类对象创建DecorView类对象(根据所选主题样式)
4. 为DecorView类对象中的content增加Activity中设置的布局文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 
 | 
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 
 
 
 
 public void setContentView(int layoutResID) {
 
 
 getWindow().setContentView(layoutResID);
 initWindowDecorActionBar();
 }
 
 
 
 
 
 
 mWindow = new PhoneWindow(this, window);
 
 
 mWindow.setWindowControllerCallback(this);
 mWindow.setCallback(this);
 
 
 mWindow.setWindowManager(
 (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
 mToken, mComponent.flattenToString(),
 (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
 }
 
 
 
 
 public void setContentView(int layoutResID) {
 
 
 if (mContentParent == null) {
 installDecor();
 } else {
 
 mContentParent.removeAllViews();
 }
 
 
 
 mLayoutInflater.inflate(layoutResID, mContentParent);
 
 final Callback cb = getCallback();
 if (cb != null && !isDestroyed()) {
 cb.onContentChanged();
 }
 }
 
 
 
 
 
 private void installDecor() {
 if (mDecor == null) {
 
 mDecor = generateDecor();
 mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
 mDecor.setIsRootNamespace(true);
 if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
 mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
 }
 }
 
 
 if (mContentParent == null) {
 mContentParent = generateLayout(mDecor);
 ...
 }
 }
 
 
 
 
 
 protected DecorView generateDecor() {
 return new DecorView(getContext(), -1);
 }
 
 
 
 
 
 
 protected ViewGroup generateLayout(DecorView decor) {
 
 TypedArray a = getWindowStyle();
 
 
 int layoutResource;
 int features = getLocalFeatures();
 
 
 View in = mLayoutInflater.inflate(layoutResource, null);
 
 
 
 decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
 mContentRoot = (ViewGroup) in;
 
 
 ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
 
 return contentParent;
 }
 
 | 
此时,顶层View(DecorView)已创建 并且 添加Activity中设置的布局文件。但是,此时,顶层View(DecorView)仍未显示出来,即不可见。
二. DecorView的显示
在主线程创建时,会调用 handleResumeActivity(),这里有DecorView的显示的流程:
1.将DecorView对象添加到WindowManager中。
2.创建ViewRootImpl对象。
3.WindowManager将DecorView对象交给ViewRootImpl对象。
4.ViewRootImpl对象通过Handler向主线程发送一条触发遍历操作的消息:performTraversals(),该方法用于执行View的绘制流程(measure,layout,draw)。
ViewRootImpl对象中接受的各种变化,如来自WindowManagerService的窗口属性变化,来自控件树的尺寸变化或者重绘请求等都会引发performTraversals()的调用并且在其中完成处理。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 
 | 
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 
 
 
 
 final void handleResumeActivity(IBinder token,
 boolean clearHide, boolean isForward, boolean reallyResume) {
 ActivityClientRecord r = performResumeActivity(token, clearHide);
 
 if (r != null) {
 final Activity a = r.activity;
 if (r.window == null && !a.mFinished && willBeVisible) {
 
 r.window = r.activity.getWindow();
 View decor = r.window.getDecorView();
 
 
 decor.setVisibility(View.INVISIBLE);
 ViewManager wm = a.getWindowManager();
 WindowManager.LayoutParams l = r.window.getAttributes();
 a.mDecor = decor;
 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
 
 
 
 if (a.mVisibleFromClient) {
 a.mWindowAdded = true;
 wm.addView(decor, l);
 }
 
 
 if (!r.activity.mFinished && willBeVisible
 && r.activity.mDecor != null && !r.hideForNow) {
 if (r.activity.mVisibleFromClient) {
 r.activity.makeVisible();
 
 }
 }
 }
 
 
 
 
 void makeVisible() {
 if (!mWindowAdded) {
 ViewManager wm = getWindowManager();
 
 wm.addView(mDecor, getWindow().getAttributes());
 mWindowAdded = true;
 }
 
 
 mDecor.setVisibility(View.VISIBLE);
 }
 
 
 
 
 
 public final class WindowManagerImpl implements WindowManager {
 private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
 ...
 @Override
 public void addView(View view, ViewGroup.LayoutParams params) {
 mGlobal.addView(view, params, mDisplay, mParentWindow); ->>分析3
 }
 }
 
 
 
 
 public void addView(View view, ViewGroup.LayoutParams params,Display display, Window parentWindow) {
 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
 ...
 synchronized (mLock) {
 
 ViewRootImpl root;
 root = new ViewRootImpl(view.getContext(), display);
 view.setLayoutParams(wparams);
 
 mViews.add(view);
 mRoots.add(root);
 mParams.add(wparams);
 }
 
 
 try {
 root.setView(view, wparams, panelParentView);
 
 
 }catch (RuntimeException e) {
 ...
 }
 
 }
 
 
 
 
 
 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
 requestLayout();
 }
 
 
 
 
 @Override
 public void requestLayout() {
 if (!mHandlingLayoutInLayoutRequest) {
 
 checkThread();
 mLayoutRequested = true;
 
 scheduleTraversals();
 }
 }
 
 
 
 
 void scheduleTraversals() {
 if (!mTraversalScheduled) {
 mTraversalScheduled = true;
 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
 
 
 
 
 mChoreographer.postCallback(Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
 ``````
 }
 }
 
 
 
 
 
 final class TraversalRunnable implements Runnable {
 @Override
 public void run() {
 doTraversal();
 }
 }
 final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
 
 
 
 
 void doTraversal() {
 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
 
 performTraversals();
 
 }
 
 
 
 
 |