//存储所有Window对应的View privatefinal ArrayList<View> mViews = new ArrayList<View>(); //所有Window对应的ViewRootImpl privatefinal ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>(); //所有Window对应的布局参数 LayoutParams privatefinal ArrayList<WindowManager.LayoutParams> mParams = new ArrayList<WindowManager.LayoutParams>(); //存储那些正在被删除的对象 privatefinal ArraySet<View> mDyingViews = new ArraySet<View>();
publicvoidaddView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow){ //检测参数是否合法 if (view == null) { thrownew IllegalArgumentException("view must not be null"); } if (display == null) { thrownew IllegalArgumentException("display must not be null"); } if (!(params instanceof WindowManager.LayoutParams)) { thrownew IllegalArgumentException("Params must be WindowManager.LayoutParams"); }
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; //如果是子Window还需要调整参数 if (parentWindow != null) { parentWindow.adjustLayoutParamsForSubWindow(wparams); } else {
// ../android/view/WindowManagerGlobal.java publicvoidupdateViewLayout(View view, ViewGroup.LayoutParams params){ if (view == null) { thrownew IllegalArgumentException("view must not be null"); } if (!(params instanceof WindowManager.LayoutParams)) { thrownew IllegalArgumentException("Params must be WindowManager.LayoutParams"); }
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params; //更新View的LayoutParams view.setLayoutParams(wparams);
synchronized (mLock) { int index = findViewLocked(view, true); ViewRootImpl root = mRoots.get(index); mParams.remove(index); mParams.add(index, wparams); root.setLayoutParams(wparams, false); } }
// ../core/java/com/android/server/wm/Session.java final WindowManagerService mService; publicintrelayout(IWindow window, int seq, WindowManager.LayoutParams attrs, int requestedWidth, int requestedHeight, int viewFlags, int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets, Rect outVisibleInsets, Rect outStableInsets, Rect outsets, Rect outBackdropFrame, MergedConfiguration mergedConfiguration, Surface outSurface){
int res = mService.relayoutWindow(this, window, seq, attrs, requestedWidth, requestedHeight, viewFlags, flags, outFrame, outOverscanInsets, outContentInsets, outVisibleInsets, outStableInsets, outsets, outBackdropFrame, mergedConfiguration, outSurface);
// ../android/view/WindowManagerGlobal.java publicvoidremoveView(View view, boolean immediate){ if (view == null) { thrownew IllegalArgumentException("view must not be null"); }
synchronized (mLock) { //找到需要删除的View索引 int index = findViewLocked(view, true); View curView = mRoots.get(index).getView(); removeViewLocked(index, immediate); if (curView == view) { return; }
thrownew IllegalStateException("Calling with view " + view + " but the ViewAncestor is attached to " + curView); } }
if (view != null) { InputMethodManager imm = InputMethodManager.getInstance(); if (imm != null) { imm.windowDismissed(mViews.get(index).getWindowToken()); } } //在die 中执行删除Window操作 boolean deferred = root.die(immediate); if (view != null) { view.assignParent(null); if (deferred) { //存储即将删除的View mDyingViews.add(view); } } }
../android/view/ViewRootImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
booleandie(boolean immediate/*是否同步执行删除*/){ // Make sure we do execute immediately if we are in the middle of a traversal or the damage // done by dispatchDetachedFromWindow will cause havoc on return. if (immediate && !mIsInTraversal) { //删除对应Window doDie(); returnfalse; }
if (!mIsDrawing) { destroyHardwareRenderer(); } else { Log.e(mTag, "Attempting to destroy the window while drawing!\n" + " window=" + this + ", title=" + mWindowAttributes.getTitle()); } mHandler.sendEmptyMessage(MSG_DIE); returntrue; }