Android Insets相關(guān)知識總結(jié)
最近工作中總會涉及到Insets相關(guān)的一些內(nèi)容,網(wǎng)上對于Insets的分析以及介紹還是較少的,這里對Insets涉及到一些概念和方法做一個總結(jié)。
什么是Insets?
WindowInsets 源碼解釋為 window content的一系列插值集合,(個人理解為 一個Activity相對于手機屏幕需要空出的地方以騰納給statusbar、Ime、Navigationbar等系統(tǒng)窗口,具體表現(xiàn)為該區(qū)域需要的上下左右的寬高,比如輸入法窗口的區(qū)域就是一個Inset)
WindowInsets包括三類:SystemWindowInsets、StableInsets、WIndowDecorInsets
- SystemWindowInsets:全窗口下,被navigationbar、statusbar、ime或其他系統(tǒng)窗口覆蓋的區(qū)域
- StableInsets:全窗口下,被系統(tǒng)UI覆蓋的區(qū)域
- WIndowDecorInsets:系統(tǒng)預(yù)留屬性
Insets相關(guān)類
InsetsState
保存系統(tǒng)中所有的Insets的狀態(tài),他是狀態(tài)描述者,持有系統(tǒng)中可以產(chǎn)生Window Insets的window狀態(tài) private InsetsSource[] mSources = new InsetsSource[SIZE]; // mSources變量維護所有產(chǎn)生Insets的window(也就是InsetsSource)
的狀態(tài)
它主要持有以下幾種類型的Insets
ITYPE_STATUS_BAR, ITYPE_NAVIGATION_BAR, ITYPE_CAPTION_BAR, ITYPE_TOP_GESTURES, ITYPE_BOTTOM_GESTURES, ITYPE_LEFT_GESTURES, ITYPE_RIGHT_GESTURES, ITYPE_TOP_TAPPABLE_ELEMENT, ITYPE_BOTTOM_TAPPABLE_ELEMENT, ITYPE_LEFT_DISPLAY_CUTOUT, ITYPE_TOP_DISPLAY_CUTOUT, ITYPE_RIGHT_DISPLAY_CUTOUT, ITYPE_BOTTOM_DISPLAY_CUTOUT, ITYPE_IME, ITYPE_CLIMATE_BAR, ITYPE_EXTRA_NAVIGATION_BAR
如果InsetsState發(fā)生改變后,會通過MSG_INSETS_CHANGED消息發(fā)送到InsetsController,進行修改并保存到變量mState中
public boolean onStateChanged(InsetsState state) { boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) || !captionInsetsUnchanged(); if (!stateChanged && mLastDispatchedState.equals(state)) { return false; } updateState(state); boolean localStateChanged = !mState.equals(mLastDispatchedState, true /* excludingCaptionInsets */, true /* excludeInvisibleIme */); mLastDispatchedState.set(state, true /* copySources */); applyLocalVisibilityOverride(); if (localStateChanged) { if (DEBUG) Log.d(TAG, "onStateChanged, notifyInsetsChanged, send state to WM: " + mState); mHost.notifyInsetsChanged(); updateRequestedState(); } return true; }
InsetsState的關(guān)鍵方法:
WindowInsets calculateInsets(...):基于當(dāng)前source設(shè)置計算新的windowInsets void processSource(InsetsSource source,...): 根據(jù)計算值更新source值
InsetsStateController
管理所有窗口的Insets的state
private final InsetsState mLastState = new InsetsState(); //舊的InsetsState private final InsetsState mState = new InsetsState(); //新的InsetsState
幾個重要的方法:
private boolean isAboveIme(WindowContainer target)// 判斷當(dāng)前窗口是否處在輸入法窗口層級上 void onImeControlTargetChanged(@Nullable InsetsControlTarget imeTarget) //當(dāng)輸入法target 窗口發(fā)生變化觸發(fā) InsetsState getInsetsForDispatch(@NonNull WindowState target) //分發(fā)Insets 對Insets進一步更新(更新frame 或者visible)
InsetsSource
是Insets產(chǎn)生者的描述,記錄每一個產(chǎn)生Insets的window的狀態(tài),主要記錄產(chǎn)生的Insets區(qū)域
private final @InternalInsetsType int mType; //Insets類型 nav或者status或者... private final Rect mFrame; //代表Insets區(qū)域 private boolean mVisible; //Insets可見性
/*幾個重要的方法/
public void setFrame(Rect frame) //設(shè)置Insets大小 public void setVisible(boolean visible) //設(shè)置Insets可見性 private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility) //根據(jù)frame以及ignoreVisibility 計算Insets
InsetsSourceConsumer(ImeInsetsSourceConsumer)
對單一InsetsSource的消費者,其內(nèi)部持有InsetsSourceControl,可以控制其leash的可見性和動畫,輸入法有專門的ImeInsetsSourceConsumer來消費輸入法的Insets
protected boolean mRequestedVisible; //單一Insets的可見性 private @Nullable InsetsSourceControl mSourceControl; // 持有InsetsSourceControl變量可以實現(xiàn)對單一InsetsSource的控制 protected final InsetsController mController; //所屬的InsetController protected final InsetsState mState; //本地state
/幾個重要的方法/
public void updateSource(InsetsSource newSource, @AnimationType int animationType) //更新mstate中的source 主要更新frame public void show(boolean fromIme) //顯示Insets protected void setRequestedVisible(boolean requestedVisible) //設(shè)置Insets的可見性 public void setControl(@Nullable InsetsSourceControl control, @InsetsType int[] showTypes, @InsetsType int[] hideTypes) //后面講 public void hide() //隱藏Insets boolean applyLocalVisibilityOverride() //主要更新state可見性 protected boolean isRequestedVisibleAwaitingControl() //判斷當(dāng)前Insets是否會在獲得control時更新可見性,即判斷是否存在pending show(如果是bars 該方法等同于isRequestedVisible)
ImeInsetsSourceConsumer
private boolean mIsRequestedVisibleAwaitingControl; //判斷是否存在一個請求要讓輸入法顯示出來(但是由于當(dāng)前尚未獲得control因此暫時無法實現(xiàn)這個操作) void notifyHidden() //控制IMM隱藏輸入法 public @ShowResult int requestShow(boolean fromIme) //控制IMM顯示輸入法 public void removeSurface() //移除輸入法的surface - InsetsSourceControl 對InsetsSource的控制者,用來控制Insets的產(chǎn)生者,內(nèi)部持有控制輸入法動畫的Leash private final @InternalInsetsType int mType; //InsetsSource類型 private final @Nullable SurfaceControl mLeash; //播放動畫需要的Leash ,app可以控制對其設(shè)置position實現(xiàn)位移動畫 private final Point mSurfacePosition; //當(dāng)前l(fā)eash(Surface)在屏幕中的position - InsetsSourceProvider 他是特定InsetsSource在server端的控制者,他被稱作provider是因為他提供InsetsSource給客戶端(客戶端通過InsetsSourceConsumer使用InsetsSource)
這里重點關(guān)注ImeInsetsSourceProvider
private InsetsControlTarget mImeTargetFromIme; //輸入法Insets的control(Insets需要有一個control,否則他就會失控 不可控制) private Runnable mShowImeRunner; //顯示輸入法線程 private boolean mIsImeLayoutDrawn; //輸入法是否已經(jīng)繪制完成
InsetsController
它是WindowInsets在client端的實現(xiàn) 用來控制insets ,InsetsController只在ViewRootImpl里面創(chuàng)建的,每個Window會對應(yīng)一個ViewRootImpl,同樣每個ViewRootImpl會對應(yīng)每個InsetsController
/*關(guān)鍵成員變量*/ InsetsState mState = new InsetsState(); //記錄本地State (Client端的Insetsstate) InsetsState mLastDispatchedState = new InsetsState(); //從system端傳來的InsetsState InsetsState mRequestedState = new InsetsState(); //發(fā)送給系統(tǒng)端的InsetsState SparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>(); //持有sourceConsumers /*關(guān)鍵方法*/ public void applyImeVisibility(boolean setVisible) //更新輸入法可見性 public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) //動畫結(jié)束時回調(diào)方法 public void onControlsChanged(InsetsSourceControl[] activeControls) //當(dāng)系統(tǒng)端分發(fā)新的Insets Controls時被調(diào)用 public boolean onStateChanged(InsetsState state) //Insets或者InsetsControl發(fā)生改變會調(diào)用 public void setSystemBarsBehavior(@Behavior int behavior) public void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask) //更改Systembar的表現(xiàn)行為 public void show(@InsetsType int types, boolean fromIme) //顯示Insets void hide(@InsetsType int types, boolean fromIme) //隱藏Insets private void updateState(InsetsState newState) //更新state private void updateRequestedState() //如果Insets在client端發(fā)生改變再重新發(fā)送到server端 public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) //更新Insets動畫
InsetsChanged、InsetsControlChanged方法
Insets的變化一般是通過消息機制來進行更改的,主要是兩方面的更改包括InsetsChanged和InsetsControlChanged,他們是由System_server經(jīng)過WindowState調(diào)用到App進程的。
WindowState.java //屬于Server端 void notifyInsetsChanged() { ProtoLog.d(WM_DEBUG_IME, "notifyInsetsChanged for %s ", this); try { mClient.insetsChanged(getInsetsState()); } catch (RemoteException e) { Slog.w(TAG, "Failed to deliver inset state change w=" + this, e); } } ViewRootImpl#W @Override public void insetsChanged(InsetsState insetsState) { final ViewRootImpl viewAncestor = mViewAncestor.get(); if (viewAncestor != null) { viewAncestor.dispatchInsetsChanged(insetsState); } } @Override public void insetsControlChanged(InsetsState insetsState, InsetsSourceControl[] activeControls) { final ViewRootImpl viewAncestor = mViewAncestor.get(); if (viewAncestor != null) { viewAncestor.dispatchInsetsControlChanged(insetsState, activeControls); } }
異步發(fā)送消息:MSG_INSETS_CHANGED、MSG_INSETS_CONTROL_CHANGED
case MSG_INSETS_CHANGED: mInsetsController.onStateChanged((InsetsState) msg.obj); break; case MSG_INSETS_CONTROL_CHANGED: { mInsetsController.onStateChanged((InsetsState) args.arg1); mInsetsController.onControlsChanged((InsetsSourceControl[]) args.arg2); break; //首先都會調(diào)用InsetsController的onStateChanged方法 }
onStateChanged
public boolean onStateChanged(InsetsState state) { boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) //判斷client端state和傳來的state是否一致 || !captionInsetsUnchanged(); //同時判斷上次server端傳來的state是否同當(dāng)前傳傳來的state一致 if (!stateChanged && mLastDispatchedState.equals(state)) { return false; } if (DEBUG) Log.d(TAG, "onStateChanged: " + state); updateState(state); //判斷client端本地state是否已經(jīng)發(fā)生改變 boolean localStateChanged = !mState.equals(mLastDispatchedState, true /* excludingCaptionInsets */, true /* excludeInvisibleIme */); //更新mLastDispatchedState 即更新server端傳來的state mLastDispatchedState.set(state, true /* copySources */); //將更新apply到本地 applyLocalVisibilityOverride(); if (localStateChanged) { if (DEBUG) Log.d(TAG, "onStateChanged, notifyInsetsChanged, send state to WM: " + mState); //如果本地Insets發(fā)生改變了,通知server端Insets更改了 mHost.notifyInsetsChanged(); //更新傳遞給server端的InsetsState updateRequestedState(); } return true; }
onControlsChanged
該方法在窗口獲取焦點或者失去焦點的時候也會調(diào)用到
public void onControlsChanged(InsetsSourceControl[] activeControls) { if (activeControls != null) { for (InsetsSourceControl activeControl : activeControls) { if (activeControl != null) { // TODO(b/122982984): Figure out why it can be null. mTmpControlArray.put(activeControl.getType(), activeControl); } } } boolean requestedStateStale = false; final int[] showTypes = new int[1]; //系統(tǒng)Insets會根據(jù)showTypes數(shù)組內(nèi)的值去更新可見性 final int[] hideTypes = new int[1]; //遍歷所有的SourceConsumer 更新system_server傳來的InsetsSourceControl for (int i = mSourceConsumers.size() - 1; i >= 0; i--) { final InsetsSourceConsumer consumer = mSourceConsumers.valueAt(i); final InsetsSourceControl control = mTmpControlArray.get(consumer.getType()); consumer.setControl(control, showTypes, hideTypes); } // Ensure to create source consumers if not available yet. //便利system_server傳遞來的InsetsSourceControl for (int i = mTmpControlArray.size() - 1; i >= 0; i--) { final InsetsSourceControl control = mTmpControlArray.valueAt(i); final @InternalInsetsType int type = control.getType(); final InsetsSourceConsumer consumer = getSourceConsumer(type); //如果consumer不存在會創(chuàng)建 consumer.setControl(control, showTypes, hideTypes); //可以看到如果存在対贏得consumer 會調(diào)用setControl方法兩次 ... } mTmpControlArray.clear(); //showTypes、hideTypes值會在setControl方法內(nèi)進行修改 int animatingTypes = invokeControllableInsetsChangedListeners(); showTypes[0] &= ~animatingTypes; hideTypes[0] &= ~animatingTypes; //假設(shè)showTypes[0]=8 代表要顯示輸入法 if (showTypes[0] != 0) { applyAnimation(showTypes[0], true /* show */, false /* fromIme */); } //假設(shè)hideTypes[0]=8 代表要隱藏輸入法 if (hideTypes[0] != 0) { applyAnimation(hideTypes[0], false /* show */, false /* fromIme */); } if (requestedStateStale) { updateRequestedState(); } }
總結(jié)
- 每個ViewRootImpl對應(yīng)一個InsetsController實例,他是一個App進程中控制Insets的核心類,用于保存?zhèn)鬟f系統(tǒng)中產(chǎn)生Insets的window的狀態(tài)和動畫需要的leash以及控制播放動畫
- InsetsSource是對產(chǎn)生Insets的窗口的狀態(tài)描述,包括可見性以及Insets的大小
- 每個InsetsController會持有一個成員變量mState(InsetsState),它保存了系統(tǒng)中所有產(chǎn)生Insets的Window(InsetsSource)的狀態(tài)列表,狀態(tài)主要是指可見性以及產(chǎn)生Insets的window的區(qū)域大小
- InsetsSourceConsumer 是用來消費特定InsetsSource,消費主要是指對產(chǎn)生Insets 的window即InsetsSource進行可見性控制以及播放動畫,通過持有的window的Leash來實現(xiàn),也就是mSourceControl(InsetsSourceControl)
- 每個InsetsController會持有多個InsetsSourceConsumer,他持有一個InsetsSourceConsumers列表,SparseArray mSourceConsumers
到這里Insets已經(jīng)總結(jié)完畢,后續(xù)將進一步通過源碼分析Insets的原理以及和App之間的關(guān)系,由于水平有限,難免有錯誤,若在閱讀時發(fā)現(xiàn)不妥或者錯誤的地方留言指正,共同進步,謝謝!
Have a nice day!
以上就是Android Insets相關(guān)知識總結(jié)的詳細內(nèi)容,更多關(guān)于Android Insets的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解Android Activity的啟動流程
- Android利用startActivityForResult返回數(shù)據(jù)到前一個Activity
- Android非異常情況下的Activity生命周期分析
- Android實現(xiàn)左滑退出Activity的完美封裝
- Android Activity的4種啟動模式圖文介紹
- Android IPC機制ACtivity綁定Service通信代碼實例
- 通過實例解析android Activity啟動過程
- Android用tabhost實現(xiàn) 界面切換,每個界面為一個獨立的activity操作
- Android 開發(fā)使用Activity實現(xiàn)加載等待界面功能示例
- AndroidX下使用Activity和Fragment的變化詳解
- 關(guān)于android連續(xù)點擊出現(xiàn)多個Activity界面的解決方法
相關(guān)文章
Android中的導(dǎo)航navigation的使用詳細步驟
在Android中,導(dǎo)航主要通過使用Navigation SDK來實現(xiàn),該SDK提供了一組工具和組件,可以幫助開發(fā)人員構(gòu)建具有一致性和可訪問性的用戶界面,這篇文章主要介紹了Android中的導(dǎo)航navigation的使用詳細步驟,需要的朋友可以參考下2024-04-04Android 設(shè)置應(yīng)用全屏的兩種解決方法
本篇文章小編為大家介紹,Android 設(shè)置應(yīng)用全屏的兩種解決方法。需要的朋友參考下2013-04-04Android中判斷網(wǎng)絡(luò)是否可用的代碼分享
這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)是否可用的代碼分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03Android Studio設(shè)置顏色拾色器工具Color Picker教程
這篇文章主要介紹了Android Studio設(shè)置顏色拾色器工具Color Picker教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android自定義View實現(xiàn)左右滑動選擇出生年份
這篇文章主要介紹了Android自定義View實現(xiàn)左右滑動選擇出生年份,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06Android Studio 下自動注釋(自定義作者,類作用等)圖文詳解
android studio 下自動注釋功能居然被隱藏了,很多功能都不見了,下面小編通過本文給大家分享Android Studio 下自動注釋(自定義作者,類作用等)圖文詳解,需要的朋友參考下吧2017-11-11MVVMLight項目Model?View結(jié)構(gòu)及全局視圖模型注入器
這篇文章主要為大家介紹了MVVMLight項目中Model及View的結(jié)構(gòu)及全局視圖模型注入器的使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01