国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android Insets相關知識總結

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

最近工作中總會涉及到Insets相關的一些內容,網上對于Insets的分析以及介紹還是較少的,這里對Insets涉及到一些概念和方法做一個總結。

什么是Insets?

WindowInsets 源碼解釋為 window content的一系列插值集合,(個人理解為 一個Activity相對于手機屏幕需要空出的地方以騰納給statusbar、Ime、Navigationbar等系統窗口,具體表現為該區域需要的上下左右的寬高,比如輸入法窗口的區域就是一個Inset)

Android Insets相關知識總結

WindowInsets包括三類:SystemWindowInsets、StableInsets、WIndowDecorInsets

SystemWindowInsets:全窗口下,被navigationbar、statusbar、ime或其他系統窗口覆蓋的區域 StableInsets:全窗口下,被系統UI覆蓋的區域 WIndowDecorInsets:系統預留屬性 Insets相關類InsetsState

保存系統中所有的Insets的狀態,他是狀態描述者,持有系統中可以產生Window Insets的window狀態 private InsetsSource[] mSources = new InsetsSource[SIZE]; // mSources變量維護所有產生Insets的window(也就是InsetsSource)的狀態

它主要持有以下幾種類型的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發生改變后,會通過MSG_INSETS_CHANGED消息發送到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的關鍵方法:

WindowInsets calculateInsets(...):基于當前source設置計算新的windowInsetsvoid processSource(InsetsSource source,...): 根據計算值更新source值InsetsStateController

管理所有窗口的Insets的state

private final InsetsState mLastState = new InsetsState(); //舊的InsetsStateprivate final InsetsState mState = new InsetsState(); //新的InsetsState

幾個重要的方法:

private boolean isAboveIme(WindowContainer target)// 判斷當前窗口是否處在輸入法窗口層級上void onImeControlTargetChanged(@Nullable InsetsControlTarget imeTarget) //當輸入法target 窗口發生變化觸發InsetsState getInsetsForDispatch(@NonNull WindowState target) //分發Insets 對Insets進一步更新(更新frame 或者visible)InsetsSource

是Insets產生者的描述,記錄每一個產生Insets的window的狀態,主要記錄產生的Insets區域

private final @InternalInsetsType int mType; //Insets類型 nav或者status或者...private final Rect mFrame; //代表Insets區域private boolean mVisible; //Insets可見性

/*幾個重要的方法/

public void setFrame(Rect frame) //設置Insets大小public void setVisible(boolean visible) //設置Insets可見性private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility) //根據frame以及ignoreVisibility 計算InsetsInsetsSourceConsumer(ImeInsetsSourceConsumer)

對單一InsetsSource的消費者,其內部持有InsetsSourceControl,可以控制其leash的可見性和動畫,輸入法有專門的ImeInsetsSourceConsumer來消費輸入法的Insets

protected boolean mRequestedVisible; //單一Insets的可見性private @Nullable InsetsSourceControl mSourceControl; // 持有InsetsSourceControl變量可以實現對單一InsetsSource的控制protected final InsetsController mController; //所屬的InsetControllerprotected final InsetsState mState; //本地state

/幾個重要的方法/

public void updateSource(InsetsSource newSource, @AnimationType int animationType) //更新mstate中的source 主要更新framepublic void show(boolean fromIme) //顯示Insetsprotected void setRequestedVisible(boolean requestedVisible) //設置Insets的可見性public void setControl(@Nullable InsetsSourceControl control, @InsetsType int[] showTypes, @InsetsType int[] hideTypes) //后面講public void hide() //隱藏Insetsboolean applyLocalVisibilityOverride() //主要更新state可見性protected boolean isRequestedVisibleAwaitingControl() //判斷當前Insets是否會在獲得control時更新可見性,即判斷是否存在pending show(如果是bars 該方法等同于isRequestedVisible)ImeInsetsSourceConsumer

private boolean mIsRequestedVisibleAwaitingControl; //判斷是否存在一個請求要讓輸入法顯示出來(但是由于當前尚未獲得control因此暫時無法實現這個操作)void notifyHidden() //控制IMM隱藏輸入法public @ShowResult int requestShow(boolean fromIme) //控制IMM顯示輸入法public void removeSurface() //移除輸入法的surface- InsetsSourceControl對InsetsSource的控制者,用來控制Insets的產生者,內部持有控制輸入法動畫的Leashprivate final @InternalInsetsType int mType; //InsetsSource類型private final @Nullable SurfaceControl mLeash; //播放動畫需要的Leash ,app可以控制對其設置position實現位移動畫private final Point mSurfacePosition; //當前leash(Surface)在屏幕中的position- InsetsSourceProvider他是特定InsetsSource在server端的控制者,他被稱作provider是因為他提供InsetsSource給客戶端(客戶端通過InsetsSourceConsumer使用InsetsSource)

這里重點關注ImeInsetsSourceProvider

private InsetsControlTarget mImeTargetFromIme; //輸入法Insets的control(Insets需要有一個control,否則他就會失控 不可控制)private Runnable mShowImeRunner; //顯示輸入法線程private boolean mIsImeLayoutDrawn; //輸入法是否已經繪制完成InsetsController

它是WindowInsets在client端的實現 用來控制insets ,InsetsController只在ViewRootImpl里面創建的,每個Window會對應一個ViewRootImpl,同樣每個ViewRootImpl會對應每個InsetsController

/*關鍵成員變量*/InsetsState mState = new InsetsState(); //記錄本地State (Client端的Insetsstate)InsetsState mLastDispatchedState = new InsetsState(); //從system端傳來的InsetsStateInsetsState mRequestedState = new InsetsState(); //發送給系統端的InsetsStateSparseArray<InsetsSourceConsumer> mSourceConsumers = new SparseArray<>(); //持有sourceConsumers/*關鍵方法*/public void applyImeVisibility(boolean setVisible) //更新輸入法可見性public void notifyFinished(InsetsAnimationControlRunner runner, boolean shown) //動畫結束時回調方法public void onControlsChanged(InsetsSourceControl[] activeControls) //當系統端分發新的Insets Controls時被調用public boolean onStateChanged(InsetsState state) //Insets或者InsetsControl發生改變會調用public void setSystemBarsBehavior(@Behavior int behavior)public void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask) //更改Systembar的表現行為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端發生改變再重新發送到server端public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) //更新Insets動畫InsetsChanged、InsetsControlChanged方法

Insets的變化一般是通過消息機制來進行更改的,主要是兩方面的更改包括InsetsChanged和InsetsControlChanged,他們是由System_server經過WindowState調用到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@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); }}

異步發送消息: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; //首先都會調用InsetsController的onStateChanged方法}onStateChanged

public boolean onStateChanged(InsetsState state) { boolean stateChanged = !mState.equals(state, true /* excludingCaptionInsets */,false /* excludeInvisibleIme */) //判斷client端state和傳來的state是否一致 || !captionInsetsUnchanged(); //同時判斷上次server端傳來的state是否同當前傳傳來的state一致 if (!stateChanged && mLastDispatchedState.equals(state)) { return false; } if (DEBUG) Log.d(TAG, 'onStateChanged: ' + state); updateState(state); //判斷client端本地state是否已經發生改變 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發生改變了,通知server端Insets更改了 mHost.notifyInsetsChanged(); //更新傳遞給server端的InsetsState updateRequestedState(); } return true;}onControlsChanged

該方法在窗口獲取焦點或者失去焦點的時候也會調用到

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]; //系統Insets會根據showTypes數組內的值去更新可見性 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不存在會創建 consumer.setControl(control, showTypes, hideTypes); //可以看到如果存在??贏得consumer 會調用setControl方法兩次 ... } mTmpControlArray.clear(); //showTypes、hideTypes值會在setControl方法內進行修改 int animatingTypes = invokeControllableInsetsChangedListeners(); showTypes[0] &= ~animatingTypes; hideTypes[0] &= ~animatingTypes; //假設showTypes[0]=8 代表要顯示輸入法 if (showTypes[0] != 0) { applyAnimation(showTypes[0], true /* show */, false /* fromIme */); } //假設hideTypes[0]=8 代表要隱藏輸入法 if (hideTypes[0] != 0) { applyAnimation(hideTypes[0], false /* show */, false /* fromIme */); } if (requestedStateStale) { updateRequestedState(); }}總結

Android Insets相關知識總結

每個ViewRootImpl對應一個InsetsController實例,他是一個App進程中控制Insets的核心類,用于保存傳遞系統中產生Insets的window的狀態和動畫需要的leash以及控制播放動畫 InsetsSource是對產生Insets的窗口的狀態描述,包括可見性以及Insets的大小 每個InsetsController會持有一個成員變量mState(InsetsState),它保存了系統中所有產生Insets的Window(InsetsSource)的狀態列表,狀態主要是指可見性以及產生Insets的window的區域大小 InsetsSourceConsumer 是用來消費特定InsetsSource,消費主要是指對產生Insets 的window即InsetsSource進行可見性控制以及播放動畫,通過持有的window的Leash來實現,也就是mSourceControl(InsetsSourceControl) 每個InsetsController會持有多個InsetsSourceConsumer,他持有一個InsetsSourceConsumers列表,SparseArray mSourceConsumers

到這里Insets已經總結完畢,后續將進一步通過源碼分析Insets的原理以及和App之間的關系,由于水平有限,難免有錯誤,若在閱讀時發現不妥或者錯誤的地方留言指正,共同進步,謝謝!

Have a nice day!

以上就是Android Insets相關知識總結的詳細內容,更多關于Android Insets的資料請關注好吧啦網其它相關文章!

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲资源在线 | 美国特级毛片 | 97超级碰碰碰碰在线视频 | 欧美叫床戏做爰无遮挡 | 国产成人免费网站 | 美日韩一区二区三区 | 久久久久久久国产精品影院 | 国产精品久久久久免费视频 | 国产性色| 亚洲男人天堂手机版 | 久久99国产精一区二区三区 | 国产高清在线精品一区a | 99re久久资源最新地址 | 天空在线观看免费完整 | 日本乱人伦片中文三区 | 伊人婷婷色香五月综合缴激情 | 成年女人免费毛片视频永久 | 国产伦一区二区三区四区久久 | 视频一区视频二区在线观看 | 亚洲欧美日韩在线一区 | 日韩三级小视频 | 亚洲日本欧美综合在线一 | 99久久精品国产国产毛片 | 国产亚洲欧美在线播放网站 | 欧美美女一区二区三区 | 日韩视频久久 | 美女扒开腿被男人猛视频 | 欧美yyy | 我们2018在线完整免费观看 | 高清视频一区 | 亚洲欧美在线播放 | 亚洲精品一区二区三区 | 日韩精品一区二区三区在线观看l | 久久久9视频在线观看 | 亚洲欧美视频一区二区三区 | 日韩一级 | 国产成人aa在线视频 | 亚洲欧洲日产国码二区首页 | 97视频免费播放观看在线视频 | 精品一区二区三区在线视频观看 | 日韩欧美亚洲每的更新在线 |