目录什么是Insets?Insets相关类InsetsStateInsetsStateControllerInsetsSourceInsetsSourceConsumer(ImeIn
最近工作中总会涉及到Insets相关的一些内容,网上对于Insets的分析以及介绍还是较少的,这里对Insets涉及到一些概念和方法做一个总结。
WindowInsets 源码解释为 window content的一系列插值集合,(个人理解为 一个Activity相对于手机屏幕需要空出的地方以腾纳给statusbar、Ime、Navigationbar等系统窗口,具体表现为该区域需要的上下左右的宽高,比如输入法窗口的区域就是一个Inset)
WindowInsets包括三类:SystemWindowInsets、StableInsets、WIndowDecorInsets
保存系统中所有的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值
管理所有窗口的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)
是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动画
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
@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);
}
}
异步发送消息: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方法
}
public boolean onStateChanged(InsetsState state) {
boolean stateChanged = !mState.equals(state, true ,false ) //判断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 , true );
//更新mLastDispatchedState 即更新server端传来的state
mLastDispatchedState.set(state, true );
//将更新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;
}
该方法在窗口获取焦点或者失去焦点的时候也会调用到
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 , false );
}
//假设hideTypes[0]=8 代表要隐藏输入法
if (hideTypes[0] != 0) {
applyAnimation(hideTypes[0], false , false );
}
if (requestedStateStale) {
updateRequestedState();
}
}
到这里Insets已经总结完毕,后续将进一步通过源码分析Insets的原理以及和App之间的关系,由于水平有限,难免有错误,若在阅读时发现不妥或者错误的地方留言指正,共同进步,谢谢!
Have a nice day!
以上就是Android Insets相关知识总结的详细内容,更多关于Android Insets的资料请关注编程网其它相关文章!
--结束END--
本文标题: Android Insets相关知识总结
本文链接: https://lsjlt.com/news/122137.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0