AndroidInsets相关知识总结
⽬录
什么是Insets?
Insets相关类
InsetsState
InsetsStateController
InsetsSource
InsetsSourceConsumer(ImeInsetsSourceConsumer)
ImeInsetsSourceConsumer
InsetsController
InsetsChanged、InsetsControlChanged⽅法
onStateChanged
onControlsChanged
总结
最近⼯作中总会涉及到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 /* 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);
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可见性
/*⼏个重要的⽅法/
public void setFrame(Rect frame) //设置Insets⼤⼩
public void setVisible(boolean visible) //设置Insets可见性
private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility) //根据frame以及ignoreVisibility 计算Insets
InsetsSourceConsumer(ImeInsetsSourceConsumer)
对单⼀InsetsSource的消费者,其内部持有InsetsSourceControl,可以控制其leash的可见性和动画,输⼊法有专门的ImeInsetsSourceConsumer来消费输⼊法的Insets
protected boolean mRequestedVisible; //单⼀Insets的可见性
private @Nullable InsetsSourceControl mSourceControl; // 持有InsetsSourceControl变量可以实现对单⼀InsetsSource的控制
protected final InsetsController mController; //所属的InsetController
protected final InsetsState mState; //本地state
/⼏个重要的⽅法/
public void updateSource(InsetsSource newSource, @AnimationType int animationType) //更新mstate中的source 主要更新frame
public void show(boolean fromIme) //显⽰Insets
protected void setRequestedVisible(boolean requestedVisible) //设置Insets的可见性
public void setControl(@Nullable InsetsSourceControl control,
@InsetsType int[] showTypes, @InsetsType int[] hideTypes) //后⾯讲
public void hide() //隐藏Insets
boolean 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的产⽣者,内部持有控制输⼊法动画的Leash
private 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端传来的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
动画
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
@Override
public void insetsChanged(InsetsState insetsState) {
final ViewRootImpl viewAncestor = ();
if (viewAncestor != null) {
viewAncestor.dispatchInsetsChanged(insetsState);
}
}
@Override
public void insetsControlChanged(InsetsState insetsState,
InsetsSourceControl[] activeControls) {
final ViewRootImpl viewAncestor = ();
if (viewAncestor != null) {
viewAncestor.dispatchInsetsControlChanged(insetsState, activeControls);
}
}
异步发送消息:MSG_INSETS_CHANGED、MSG_INSETS_CONTROL_CHANGED
case MSG_INSETS_CHANGED:
break;
case MSG_INSETS_CONTROL_CHANGED: {
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更改了
//更新传递给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.Type(), 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 = (Type());
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 = Type();
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();
}
}
总结
1. 每个ViewRootImpl对应⼀个InsetsController实例,他是⼀个App进程中控制Insets的核⼼类,⽤于保存传递系统中产⽣Insets的window的状态和动
画需要的leash以及控制播放动画
2. InsetsSource是对产⽣Insets的窗⼝的状态描述,包括可见性以及Insets的⼤⼩
3. 每个InsetsController会持有⼀个成员变量mState(InsetsState),它保存了系统中所有产⽣Insets的Window(InsetsSource)的状态列表,状态主
要是指可见性以及产⽣Insets的window的区域⼤⼩
4. InsetsSourceConsumer 是⽤来消费特定InsetsSource,消费主要是指对产⽣Insets 的window即InsetsSource进⾏可见性控制以及播放动画,通过
statusbar是什么意思持有的window的Leash来实现,也就是mSourceControl(InsetsSourceControl)
5. 每个InsetsController会持有多个InsetsSourceConsumer,他持有⼀个InsetsSourceConsumers列表,SparseArray mSourceConsumers
到这⾥Insets已经总结完毕,后续将进⼀步通过源码分析Insets的原理以及和App之间的关系,由于⽔平有限,难免有错误,若在阅读时发现不妥或者错误的地⽅留⾔指正,共同进步,谢谢!
Have a nice day!
以上就是Android Insets相关知识总结的详细内容,更多关于Android Insets的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论