網(wǎng)站首頁 編程語言 正文
Android布局控件View?ViewRootImpl?WindowManagerService關(guān)系_Android
作者:One_Month ? 更新時間: 2023-04-24 編程語言1. View,ViewRoot和WindowManager簡單介紹
1.1 View和ViewGroup
Android的基本布局控件,結(jié)構(gòu)是樹裝,ViewGroup實現(xiàn)了ViewParent接口,每個View內(nèi)部保留一個ViewParent變量,代表他的父節(jié)點
1.2 ViewRootImpl
ViewRoot概念的具體實現(xiàn)類,也實現(xiàn)了ViewParent接口,每個View樹頂級ViewParent就是這個類,主要管理
- View樹的measure,layout,draw
- 向WindowManagerService注冊Window
- 接收WindowManagerService的事件回調(diào)
1.3 WindowManager
具體實現(xiàn)類WindowManagerImpl,不過最終任務(wù)委托給了WindowManagerGlobal對象,負(fù)責(zé)建立WindowManagerService連接和通信
ViewRootImpl的功能相當(dāng)于中介,左手掌握的頂級View,右手掌握WindowManger通信
2. ViewRootImpl的起源
2.1 ViewRootImpl創(chuàng)建時機
這個和WindowManager是有點關(guān)聯(lián)的,起點一般是Activity調(diào)用resume的時機,這個具體是在ActivityThread類中的
public final class ActivityThread {
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(4);
wm = a.getWindowManager();
LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = 1;
l.softInputMode |= forwardBit;
if (r.mPreserveWindow) {
a.mWindowAdded = true;
r.mPreserveWindow = false;
ViewRootImpl impl = decor.getViewRootImpl();
if (impl != null) {
impl.notifyChildRebuilt();
}
}
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
// 核心
wm.addView(decor, l);
} else {
a.onWindowAttributesChanged(l);
}
}
} else if (!willBeVisible) {
r.hideForNow = true;
}
}
}
wm.addView()函數(shù)是入口,wm實際是WindowManager對象,WindowManager真正做工作的類是WindowManagerGlobal
addView()實際上是添加的DecorView ,從r.window獲取(Activity的話window實現(xiàn)類是PhoneWindow)
public final class WindowManagerGlobal {
public void addView(View view, android.view.ViewGroup.LayoutParams params, Display display, Window parentWindow) {
// 省略非必要代碼
ViewRootImpl root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
this.mViews.add(view);
this.mRoots.add(root);
this.mParams.add(wparams);
try {
root.setView(view, wparams, panelParentView);
} catch (RuntimeException var13) {
if (index >= 0) {
this.removeViewLocked(index, true);
}
throw var13;
}
}
}
}
}
這時候創(chuàng)建了ViewRootImpl對象,并把View,ViewRootImpl和windowManagerParam保存到WindowManagerGlobal的集合中,root.setView() 負(fù)責(zé)接力下一棒
2.2 ViewRootImpl通知注冊Window
public final class ViewRootImpl implements ViewParent, Callbacks, DrawCallbacks {
public void setView(View view, LayoutParams attrs, View panelParentView) {
synchronized(this) {
if (this.mView == null) {
this.mView = view;
// 省略代碼
try {
this.mOrigWindowType = this.mWindowAttributes.type;
this.mAttachInfo.mRecomputeGlobalAttributes = true;
this.collectViewAttributes();
/**
* 核心
*/
res = this.mWindowSession.addToDisplay(this.mWindow, this.mSeq, this.mWindowAttributes, this.getHostVisibility(), this.mDisplay.getDisplayId(), this.mAttachInfo.mContentInsets, this.mAttachInfo.mStableInsets, this.mAttachInfo.mOutsets, this.mInputChannel);
} catch (RemoteException var19) {
this.mAdded = false;
this.mView = null;
this.mAttachInfo.mRootView = null;
this.mInputChannel = null;
this.mFallbackEventHandler.setView((View)null);
this.unscheduleTraversals();
this.setAccessibilityFocus((View)null, (AccessibilityNodeInfo)null);
throw new RuntimeException("Adding window failed", var19);
} finally {
if (restore) {
attrs.restore();
}
}
}
}
}
}
ViewRootImpl保存和View對象,這里我們也可以發(fā)現(xiàn) 每一個頂級View都會對應(yīng)一個ViewRootImpl對象,this.mWindowSession.addToDisplay() 通過WindowSession通知WindowManagerService注冊Window
3.ViewRootImpl與WindowManagerService的通信
3.1 WindowSession
ViewRootImpl向WindowManagerService發(fā)送信息的類,實際上是一個AIDL接口
public interface IWindowSession extends IInterface {}
ViewRootImpl通過WindowManagerGlobal.getWindowSession() 獲取WindowSession對象
public static IWindowSession getWindowSession() {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
// Emulate the legacy behavior. The global instance of InputMethodManager
// was instantiated here.
// TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
IWindowManager windowManager = getWindowManagerService();
sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
});
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
return sWindowSession;
}
}
前面注冊Window的時候就用到的的就是WindowSession對象的addToDisplay
3.2 IWindow
這個也是一個AIDL接口文件,主要是用于WindowManagerService向ViewRootImpl發(fā)送消息,ViewRootImpl調(diào)用mWindowSession.addToDisplay注冊窗口,會把IWindow這個信使帶給WindowManagerService
public final class ViewRootImpl implements ViewParent, Callbacks, DrawCallbacks {
static class W extends android.view.IWindow.Stub {
private final WeakReference<ViewRootImpl> mViewAncestor;
private final IWindowSession mWindowSession;
W(ViewRootImpl viewAncestor) {
this.mViewAncestor = new WeakReference(viewAncestor);
this.mWindowSession = viewAncestor.mWindowSession;
}
public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets, Rect stableInsets, Rect outsets, boolean reportDraw, MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout, boolean alwaysConsumeNavBar, int displayId) {
// 省略實現(xiàn)
}
public void moved(int newX, int newY) {
// 省略實現(xiàn)
}
public void dispatchAppVisibility(boolean visible) {
// 省略實現(xiàn)
}
public void dispatchGetNewSurface() {
// 省略實現(xiàn)
}
public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
// 省略實現(xiàn)
}
private static int checkCallingPermission(String permission) {
// 省略實現(xiàn)
}
public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
// 省略實現(xiàn)
}
public void closeSystemDialogs(String reason) {
// 省略實現(xiàn)
}
public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep, boolean sync) {
// 省略實現(xiàn)
}
public void dispatchWallpaperCommand(String action, int x, int y, int z, Bundle extras, boolean sync) {
// 省略實現(xiàn)
}
public void dispatchDragEvent(DragEvent event) {
// 省略實現(xiàn)
}
public void updatePointerIcon(float x, float y) {
// 省略實現(xiàn)
}
public void dispatchSystemUiVisibilityChanged(int seq, int globalVisibility, int localValue, int localChanges) {
// 省略實現(xiàn)
}
public void dispatchWindowShown() {
// 省略實現(xiàn)
}
public void requestAppKeyboardShortcuts(IResultReceiver receiver, int deviceId) {
// 省略實現(xiàn)
}
public void dispatchPointerCaptureChanged(boolean hasCapture) {
// 省略實現(xiàn)
}
}
}
我們比較熟悉的可能就是WindowFocusChanged這個函數(shù)
4. ViewRootImpl與View
這兩個關(guān)系緊密,我們最容易接觸到的是ViewRootImpl控制者View的刷新
void scheduleTraversals() {
if (!this.mTraversalScheduled) {
this.mTraversalScheduled = true;
this.mTraversalBarrier = this.mHandler.getLooper().getQueue().postSyncBarrier();
this.mChoreographer.postCallback(2, this.mTraversalRunnable, (Object)null);
if (!this.mUnbufferedInputDispatch) {
this.scheduleConsumeBatchedInput();
}
this.notifyRendererOfFramePending();
this.pokeDrawLockIfNeeded();
}
}
scheduleTraversals是刷新方法的起點:
主線程Handler設(shè)置同步屏障(postSyncBarrier),讓刷新任務(wù)先執(zhí)行
注冊mChoreographer.postCallback()接收界面更新的同步消息
接收到刷新的同步消息后,執(zhí)行mTraversalRunnable的run方法,調(diào)用ViewRootImpl.doTraversal()
void doTraversal() {
if (this.mTraversalScheduled) {
this.mTraversalScheduled = false;
this.mHandler.getLooper().getQueue().removeSyncBarrier(this.mTraversalBarrier);
if (this.mProfile) {
Debug.startMethodTracing("ViewAncestor");
}
this.performTraversals();
if (this.mProfile) {
Debug.stopMethodTracing();
this.mProfile = false;
}
}
}
this.performTraversals()內(nèi)部調(diào)用performMeasure, performLayout, performDraw View樹的measure,layout和draw實現(xiàn)刷新
原文鏈接:https://juejin.cn/post/7198067983775334456
相關(guān)推薦
- 2022-07-12 純css控制文字顯示隱藏
- 2022-05-12 Python中賦值運算符的含義與使用方法_python
- 2022-11-07 go?分布式鎖簡單實現(xiàn)實例詳解_Golang
- 2022-08-26 Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟_python
- 2023-03-03 C語言malloc與calloc區(qū)別詳解_C 語言
- 2022-06-28 C#開發(fā)Winform實現(xiàn)學(xué)生管理系統(tǒng)_C#教程
- 2022-12-12 C語言實現(xiàn)打印星號圖案_C 語言
- 2023-05-06 React組件渲染后對DOM的操作方式_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支