最近工作中总会涉及到Insets相关的一些内容,网上对于Insets的分析以及介绍还是较少的,这里对Insets涉及到一些概念和方法做一个总结。
什么是Insets?
WindowInsets 源码解释为 window content的一系列插值集合,(个人理解为 一个Activity相对于手机屏幕需要空出的地方以腾纳给statusbar、Ime、Navigationbar等系统窗口,具体表现为该区域需要的上下左右的宽高,比如输入法窗口的区域就是一个Inset)
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 ,false ) || !captionInsetsUnchanged();
if (!stateChanged && mLastDispatchedState.equals(state)) {
return false;
}
updateState(state);
boolean localStateChanged = !mState.equals(mLastDispatchedState,
true , true );
mLastDispatchedState.set(state, true );
applyLocalVisibilityOverride();
if (localStateChanged) {
if (DEBUG) Log.d(TAG, "onStateChanged, notifyInsetsChanged, send state to WM: " + mState);
mHost.notifyInsetsChanged();
updateRequestedState();
}
return true;
}
InsetsState的关键方法:
WindowInsets calculateInsets(...):基于当前source设置计算新的windowInsets
void processSource(InsetsSource source,...): 根据计算值更新source值
InsetsStateController
管理所有窗口的Insets的state
private final InsetsState mLastState = new InsetsState(); //旧的InsetsState
private 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可见性
InsetsState mState = new InsetsState(); //记录本地State (Client端的Insetsstate) InsetsState mLastDispatchedState = new InsetsState(); //从system端传来的InsetsState InsetsState mRequestedState = new InsetsState(); //发送给系统端的InsetsState SparseArray<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) //显示Insets void hide(@InsetsType int types, boolean fromIme) //隐藏Insets private void updateState(InsetsState newState) //更新state private void updateRequestedState() //如果Insets在client端发生改变再重新发送到server端 public void applyAnimation(@InsetsType final int types, boolean show, boolean fromIme) //更新Insets动画