Android Insets相關(guān)知識(shí)總結(jié)
最近工作中總會(huì)涉及到Insets相關(guān)的一些內(nèi)容,網(wǎng)上對(duì)于Insets的分析以及介紹還是較少的,這里對(duì)Insets涉及到一些概念和方法做一個(gè)總結(jié)。
什么是Insets?
WindowInsets 源碼解釋為 window content的一系列插值集合,(個(gè)人理解為 一個(gè)Activity相對(duì)于手機(jī)屏幕需要空出的地方以騰納給statusbar、Ime、Navigationbar等系統(tǒng)窗口,具體表現(xiàn)為該區(qū)域需要的上下左右的寬高,比如輸入法窗口的區(qū)域就是一個(gè)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變量維護(hù)所有產(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ā)生改變后,會(huì)通過MSG_INSETS_CHANGED消息發(fā)送到InsetsController,進(jìn)行修改并保存到變量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è)置計(jì)算新的windowInsets void processSource(InsetsSource source,...): 根據(jù)計(jì)算值更新source值
InsetsStateController
管理所有窗口的Insets的state
private final InsetsState mLastState = new InsetsState(); //舊的InsetsState private final InsetsState mState = new InsetsState(); //新的InsetsState
幾個(gè)重要的方法:
private boolean isAboveIme(WindowContainer target)// 判斷當(dāng)前窗口是否處在輸入法窗口層級(jí)上 void onImeControlTargetChanged(@Nullable InsetsControlTarget imeTarget) //當(dāng)輸入法target 窗口發(fā)生變化觸發(fā) InsetsState getInsetsForDispatch(@NonNull WindowState target) //分發(fā)Insets 對(duì)Insets進(jìn)一步更新(更新frame 或者visible)
InsetsSource
是Insets產(chǎn)生者的描述,記錄每一個(gè)產(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可見性
/*幾個(gè)重要的方法/
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 計(jì)算Insets
InsetsSourceConsumer(ImeInsetsSourceConsumer)
對(duì)單一InsetsSource的消費(fèi)者,其內(nèi)部持有InsetsSourceControl,可以控制其leash的可見性和動(dòng)畫,輸入法有專門的ImeInsetsSourceConsumer來消費(fèi)輸入法的Insets
protected boolean mRequestedVisible; //單一Insets的可見性 private @Nullable InsetsSourceControl mSourceControl; // 持有InsetsSourceControl變量可以實(shí)現(xiàn)對(duì)單一InsetsSource的控制 protected final InsetsController mController; //所屬的InsetController protected final InsetsState mState; //本地state
/幾個(gè)重要的方法/
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是否會(huì)在獲得control時(shí)更新可見性,即判斷是否存在pending show(如果是bars 該方法等同于isRequestedVisible)
ImeInsetsSourceConsumer
private boolean mIsRequestedVisibleAwaitingControl; //判斷是否存在一個(gè)請(qǐng)求要讓輸入法顯示出來(但是由于當(dāng)前尚未獲得control因此暫時(shí)無法實(shí)現(xiàn)這個(gè)操作) void notifyHidden() //控制IMM隱藏輸入法 public @ShowResult int requestShow(boolean fromIme) //控制IMM顯示輸入法 public void removeSurface() //移除輸入法的surface - InsetsSourceControl 對(duì)InsetsSource的控制者,用來控制Insets的產(chǎn)生者,內(nèi)部持有控制輸入法動(dòng)畫的Leash private final @InternalInsetsType int mType; //InsetsSource類型 private final @Nullable SurfaceControl mLeash; //播放動(dòng)畫需要的Leash ,app可以控制對(duì)其設(shè)置position實(shí)現(xiàn)位移動(dòng)畫 private final Point mSurfacePosition; //當(dāng)前l(fā)eash(Surface)在屏幕中的position - InsetsSourceProvider 他是特定InsetsSource在server端的控制者,他被稱作provider是因?yàn)樗峁㊣nsetsSource給客戶端(客戶端通過InsetsSourceConsumer使用InsetsSource)
這里重點(diǎn)關(guān)注ImeInsetsSourceProvider
private InsetsControlTarget mImeTargetFromIme; //輸入法Insets的control(Insets需要有一個(gè)control,否則他就會(huì)失控 不可控制) private Runnable mShowImeRunner; //顯示輸入法線程 private boolean mIsImeLayoutDrawn; //輸入法是否已經(jīng)繪制完成
InsetsController
它是WindowInsets在client端的實(shí)現(xiàn) 用來控制insets ,InsetsController只在ViewRootImpl里面創(chuàng)建的,每個(gè)Window會(huì)對(duì)應(yīng)一個(gè)ViewRootImpl,同樣每個(gè)ViewRootImpl會(huì)對(duì)應(yīng)每個(gè)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) //動(dòng)畫結(jié)束時(shí)回調(diào)方法 public void onControlsChanged(InsetsSourceControl[] activeControls) //當(dāng)系統(tǒng)端分發(fā)新的Insets Controls時(shí)被調(diào)用 public boolean onStateChanged(InsetsState state) //Insets或者InsetsControl發(fā)生改變會(huì)調(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ā)生改變?cè)僦匦掳l(fā)送到server端 public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) //更新Insets動(dòng)畫
InsetsChanged、InsetsControlChanged方法
Insets的變化一般是通過消息機(jī)制來進(jìn)行更改的,主要是兩方面的更改包括InsetsChanged和InsetsControlChanged,他們是由System_server經(jīng)過WindowState調(diào)用到App進(jìn)程的。
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; //首先都會(huì)調(diào)用InsetsController的onStateChanged方法 }
onStateChanged
public boolean onStateChanged(InsetsState state) { boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) //判斷client端state和傳來的state是否一致 || !captionInsetsUnchanged(); //同時(shí)判斷上次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ǎn)或者失去焦點(diǎn)的時(shí)候也會(huì)調(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會(huì)根據(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不存在會(huì)創(chuàng)建 consumer.setControl(control, showTypes, hideTypes); //可以看到如果存在対贏得consumer 會(huì)調(diào)用setControl方法兩次 ... } mTmpControlArray.clear(); //showTypes、hideTypes值會(huì)在setControl方法內(nèi)進(jìn)行修改 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é)
- 每個(gè)ViewRootImpl對(duì)應(yīng)一個(gè)InsetsController實(shí)例,他是一個(gè)App進(jìn)程中控制Insets的核心類,用于保存?zhèn)鬟f系統(tǒng)中產(chǎn)生Insets的window的狀態(tài)和動(dòng)畫需要的leash以及控制播放動(dòng)畫
- InsetsSource是對(duì)產(chǎn)生Insets的窗口的狀態(tài)描述,包括可見性以及Insets的大小
- 每個(gè)InsetsController會(huì)持有一個(gè)成員變量mState(InsetsState),它保存了系統(tǒng)中所有產(chǎn)生Insets的Window(InsetsSource)的狀態(tài)列表,狀態(tài)主要是指可見性以及產(chǎn)生Insets的window的區(qū)域大小
- InsetsSourceConsumer 是用來消費(fèi)特定InsetsSource,消費(fèi)主要是指對(duì)產(chǎn)生Insets 的window即InsetsSource進(jìn)行可見性控制以及播放動(dòng)畫,通過持有的window的Leash來實(shí)現(xiàn),也就是mSourceControl(InsetsSourceControl)
- 每個(gè)InsetsController會(huì)持有多個(gè)InsetsSourceConsumer,他持有一個(gè)InsetsSourceConsumers列表,SparseArray mSourceConsumers
到這里Insets已經(jīng)總結(jié)完畢,后續(xù)將進(jìn)一步通過源碼分析Insets的原理以及和App之間的關(guān)系,由于水平有限,難免有錯(cuò)誤,若在閱讀時(shí)發(fā)現(xiàn)不妥或者錯(cuò)誤的地方留言指正,共同進(jìn)步,謝謝!
Have a nice day!
以上就是Android Insets相關(guān)知識(shí)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Android Insets的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 詳解Android Activity的啟動(dòng)流程
- Android利用startActivityForResult返回?cái)?shù)據(jù)到前一個(gè)Activity
- Android非異常情況下的Activity生命周期分析
- Android實(shí)現(xiàn)左滑退出Activity的完美封裝
- Android Activity的4種啟動(dòng)模式圖文介紹
- Android IPC機(jī)制ACtivity綁定Service通信代碼實(shí)例
- 通過實(shí)例解析android Activity啟動(dòng)過程
- Android用tabhost實(shí)現(xiàn) 界面切換,每個(gè)界面為一個(gè)獨(dú)立的activity操作
- Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能示例
- AndroidX下使用Activity和Fragment的變化詳解
- 關(guān)于android連續(xù)點(diǎn)擊出現(xiàn)多個(gè)Activity界面的解決方法
相關(guān)文章
Android中的導(dǎo)航navigation的使用詳細(xì)步驟
在Android中,導(dǎo)航主要通過使用Navigation SDK來實(shí)現(xiàn),該SDK提供了一組工具和組件,可以幫助開發(fā)人員構(gòu)建具有一致性和可訪問性的用戶界面,這篇文章主要介紹了Android中的導(dǎo)航navigation的使用詳細(xì)步驟,需要的朋友可以參考下2024-04-04移動(dòng)端html5圖片上傳方法【更好的兼容安卓IOS和微信】
這篇文章主要為大家詳細(xì)介紹了移動(dòng)端html5圖片上傳方法,更好的兼容安卓IOS和微信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android 設(shè)置應(yīng)用全屏的兩種解決方法
本篇文章小編為大家介紹,Android 設(shè)置應(yīng)用全屏的兩種解決方法。需要的朋友參考下2013-04-04Android中判斷網(wǎng)絡(luò)是否可用的代碼分享
這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)是否可用的代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03Android Studio設(shè)置顏色拾色器工具Color Picker教程
這篇文章主要介紹了Android Studio設(shè)置顏色拾色器工具Color Picker教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android自定義View實(shí)現(xiàn)左右滑動(dòng)選擇出生年份
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)左右滑動(dòng)選擇出生年份,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android Studio 下自動(dòng)注釋(自定義作者,類作用等)圖文詳解
android studio 下自動(dòng)注釋功能居然被隱藏了,很多功能都不見了,下面小編通過本文給大家分享Android Studio 下自動(dòng)注釋(自定義作者,類作用等)圖文詳解,需要的朋友參考下吧2017-11-11MVVMLight項(xiàng)目Model?View結(jié)構(gòu)及全局視圖模型注入器
這篇文章主要為大家介紹了MVVMLight項(xiàng)目中Model及View的結(jié)構(gòu)及全局視圖模型注入器的使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01