国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android Insets相關(guān)知識(shí)總結(jié)

瀏覽:87日期:2022-09-20 13:47:34

最近工作中總會(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)

Android Insets相關(guān)知識(shí)總結(jié)

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ì)算新的windowInsetsvoid processSource(InsetsSource source,...): 根據(jù)計(jì)算值更新source值InsetsStateController

管理所有窗口的Insets的state

private final InsetsState mLastState = new InsetsState(); //舊的InsetsStateprivate 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ì)算InsetsInsetsSourceConsumer(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; //所屬的InsetControllerprotected final InsetsState mState; //本地state

/幾個(gè)重要的方法/

public void updateSource(InsetsSource newSource, @AnimationType int animationType) //更新mstate中的source 主要更新framepublic void show(boolean fromIme) //顯示Insetsprotected void setRequestedVisible(boolean requestedVisible) //設(shè)置Insets的可見性public void setControl(@Nullable InsetsSourceControl control, @InsetsType int[] showTypes, @InsetsType int[] hideTypes) //后面講public void hide() //隱藏Insetsboolean 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í)無(wú)法實(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)畫的Leashprivate 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端傳來的InsetsStateInsetsState mRequestedState = new InsetsState(); //發(fā)送給系統(tǒng)端的InsetsStateSparseArray<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) //顯示Insetsvoid hide(@InsetsType int types, boolean fromIme) //隱藏Insetsprivate void updateState(InsetsState newState) //更新stateprivate 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@Overridepublic void insetsChanged(InsetsState insetsState) { final ViewRootImpl viewAncestor = mViewAncestor.get(); if (viewAncestor != null) { viewAncestor.dispatchInsetsChanged(insetsState); }}@Overridepublic 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é)

Android Insets相關(guān)知識(shí)總結(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)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 肃南| 行唐县| 潜江市| 民权县| 潮州市| 吴忠市| 增城市| 绥滨县| 确山县| 治县。| 沧州市| 正阳县| 新沂市| 宜川县| 荆州市| 侯马市| 申扎县| 资溪县| 永善县| 通化市| 蒙城县| 彭水| 德惠市| 溆浦县| 江西省| 贵溪市| 梁平县| 西昌市| 潢川县| 龙口市| 天气| 台中县| 冕宁县| 上虞市| 托克托县| 永和县| 孝感市| 边坝县| 建宁县| 蓝田县| 介休市|