Android 4.4后,可以通过将StatusBar和NavigationBar的背景设置为透明或者通过 getWindow().getDecorView().setSystemUiVisibility 的方式,使得 contentView 可以铺满整个DecorView。然而大多数情况下,我们并不希望有实质性的内容被StatusBar或者NavigationBar给覆盖掉,那么Android是如何处理这些看似额外的空间的分发呢,这就涉及到了WindowInsets了。
什么是WindowInsets?
在Android源码的注释中解释为 window content 的一系列插入集合,final 型,不可修改,但后期可能继续扩展。其主要成员包括 mSystemWindowInsets, mWindowDecorInsets, mStableInsets。
表示全窗口下,被StatusBar, NavigationBar, IME 或者其它系统窗口部分或者全部覆盖的区域。
- mWindowDecorInsets
表示内容窗口下,被Android FrameWork提供的窗体,诸如ActionBar, TitleBar, ToolBar,部分或全部覆盖区域。
- mStableInsets
表示全窗口下,被系统UI部分或者全部覆盖的区域。
如何理解WindowInsets
以 mSystemWindowInsets 为例:
private Rect mSystemWindowInsets;
public int getSystemWindowInsetLeft() {
return mSystemWindowInsets.left;
}
public int getSystemWindowInsetTop() {
return mSystemWindowInsets.top;
}
public int getSystemWindowInsetRight() {
return mSystemWindowInsets.right;
}
public int getSystemWindowInsetBottom() {
return mSystemWindowInsets.bottom;
}
这里的Rect的概念已经区别于View的Rect了,它的四个点已经不再表示围成矩形的坐标,而表示的是insets需要的左右的宽度,顶部和底部需要的高度。
消费Windowinsets
以 mSystemWindowInsets 为例:
private boolean mSystemWindowInsetsConsumed = false;
public WindowInsets consumeSystemWindowInsets() {
final WindowInsets result = new WindowInsets(this);
result.mSystemWindowInsets = EMPTY_RECT;
result.mSystemWindowInsetsConsumed = true;
return result;
}
public WindowInsets consumeSystemWindowInsets(boolean left, boolean top,
boolean right, boolean bottom) {
if (left || top || right || bottom) {
final WindowInsets result = new WindowInsets(this);
result.mSystemWindowInsets = new Rect(
left ? 0 : mSystemWindowInsets.left,
top ? 0 : mSystemWindowInsets.top,
right ? 0 : mSystemWindowInsets.right,
bottom ? 0 : mSystemWindowInsets.bottom);
return result;
}
return this;
}
从上可以看出,mSystemWindowInsets的消费分为全部消费和部分消费,如果不存在消费,则返回对象本身,如果消费了,则返回将消费部分置为0的对象copy。
//判断WindowInsets是否被消费掉
public boolean isConsumed() {
return mSystemWindowInsetsConsumed && mWindowDecorInsetsConsumed && mStableInsetsConsumed;
}
可见要消费掉WindowInsets,需要同时消耗掉 mSystemWindowInsets, mWindowDecorInsets, mStableInsets。
谁在消费WindowInsets
从WindowInsets的注释中,可以发现两个函数
/**
* @see View.OnApplyWindowInsetsListener
* @see View#onApplyWindowInsets(WindowInsets)
*/
深入View的源码可以发现,如果View设定了OnApplyWindowInsetsListener后,会采用OnApplyWindowInsetsListener的实现来处理WindowInsets,否则才会使用onApplyWindowInsets(WindowInsets)方法来处理WindowInsets,在dispatchApplyWindowInsets(WindowInsets)中进行分发处理,代码如下:
public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
try {
mPrivateFlags3 |= PFLAG3_APPLYING_INSETS;
if (mListenerInfo != null && mListenerInfo.mOnApplyWindowInsetsListener != null) {
return mListenerInfo.mOnApplyWindowInsetsListener.onApplyWindowInsets(this, insets);
} else {
return onApplyWindowInsets(insets);
}
} finally {
mPrivateFlags3 &= ~PFLAG3_APPLYING_INSETS;
}
}
可以发现,对于View而言,会在dispatchApplyWindowInset的过程中Apply WindowInsets。对应的ViewGroup代码如下:
@Override
public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
insets = super.dispatchApplyWindowInsets(insets);
if (!insets.isConsumed()) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
insets = getChildAt(i).dispatchApplyWindowInsets(insets);
if (insets.isConsumed()) {
break;
}
}
}
return insets;
}
ViewGroup自身也会Apply WindowInsets,如果该过程中没有消耗掉WindowInsets,则会继续传递给 child 处理WindwInsets,如果child中消耗了WindowInsets, 则会退出分发循环。
再看一下,View自身是如何处理WindowInsets的
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if ((mPrivateFlags3 & PFLAG3_FITTING_SYSTEM_WINDOWS) == 0) {
// We weren't called from within a direct call to fitSystemWindows,
// call into it as a fallback in case we're in a class that overrides it
// and has logic to perform.
if (fitSystemWindows(insets.getSystemWindowInsets())) {
return insets.consumeSystemWindowInsets();
}
} else {
// We were called from within a direct call to fitSystemWindows.
if (fitSystemWindowsInt(insets.getSystemWindowInsets())) {
return insets.consumeSystemWindowInsets();
}
}
return insets;
}
查看fitSystemWindows(Rect insets)方法
//当if条件成立时,会进入 dispatchApply逻辑,不成立则进入实际的处理逻辑fitSystemWindowsInt(insets)
//如此设置的原因在于,不直接调用fitSystemWindowsInt(insets)方法,而是要经过dispatchApply后再调用
protected boolean fitSystemWindows(Rect insets) {
if ((mPrivateFlags3 & PFLAG3_APPLYING_INSETS) == 0) {
if (insets == null) {
// Null insets by definition have already been consumed.
// This call cannot apply insets since there are none to apply,
// so return false.
return false;
}
// If we're not in the process of dispatching the newer apply insets call,
// that means we're not in the compatibility path. Dispatch into the newer
// apply insets path and take things from there.
try {
mPrivateFlags3 |= PFLAG3_FITTING_SYSTEM_WINDOWS;
return dispatchApplyWindowInsets(new WindowInsets(insets)).isConsumed();
} finally {
mPrivateFlags3 &= ~PFLAG3_FITTING_SYSTEM_WINDOWS;
}
} else {
// We're being called from the newer apply insets path.
// Perform the standard fallback behavior.
return fitSystemWindowsInt(insets);
}
}
再看fitSystemWindowsInt(Rect insets)方法
//当View设置了fitSystemWindow = true 后, 才会处理 WindowInsets,否则,直接返回false。
//
private boolean fitSystemWindowsInt(Rect insets) {
if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
mUserPaddingStart = UNDEFINED_PADDING;
mUserPaddingEnd = UNDEFINED_PADDING;
Rect localInsets = sThreadLocal.get();
if (localInsets == null) {
localInsets = new Rect();
sThreadLocal.set(localInsets);
}
boolean res = computeFitSystemWindows(insets, localInsets);
mUserPaddingLeftInitial = localInsets.left;
mUserPaddingRightInitial = localInsets.right;
internalSetPadding(localInsets.left, localInsets.top,
localInsets.right, localInsets.bottom);
return res;
}
return false;
}
//计算是否消费WindowInsets
protected boolean computeFitSystemWindows(Rect inoutInsets, Rect outLocalInsets) {
if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
|| mAttachInfo == null
|| ((mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0
&& !mAttachInfo.mOverscanRequested)) {
outLocalInsets.set(inoutInsets);
inoutInsets.set(0, 0, 0, 0);
return true;
} else {
// The application wants to take care of fitting system window for
// the content... however we still need to take care of any overscan here.
final Rect overscan = mAttachInfo.mOverscanInsets;
outLocalInsets.set(overscan);
inoutInsets.left -= overscan.left;
inoutInsets.top -= overscan.top;
inoutInsets.right -= overscan.right;
inoutInsets.bottom -= overscan.bottom;
return false;
}
}
//重新调整View的padding值
protected void internalSetPadding(int left, int top, int right, int bottom) {
//省略部分非关键代码
...
if (mPaddingLeft != left) {
changed = true;
mPaddingLeft = left;
}
if (mPaddingTop != top) {
changed = true;
mPaddingTop = top;
}
if (mPaddingRight != right) {
changed = true;
mPaddingRight = right;
}
if (mPaddingBottom != bottom) {
changed = true;
mPaddingBottom = bottom;
}
if (changed) {
requestLayout();
invalidateOutline();
}
}
以上便是WindowInsets在View和ViewGroup中处理WindowInsets的过程,ViewGroup与View之间处理WindowInsets的区别在于dispatchApplyWindowInsets(…)函数,网上大数文章将其当成一个分发逻辑看待,其实更准确的说法应该是一个消费或者分发逻辑。
总结:
WindowInsets是一个描述了屏幕上的各个插入空间的一个类,其在后期中可以扩展,WindowInsets在消耗后将不再继续传递。对于普通的View而言,要消耗WindowInsets必须先设置View的 fitsSystemWindows 的属性为true。这也是为什么对普通View层级设置fitsSystemWindows属性为true却只有一个顶层的生效而已。单对于一些特殊的View而言,则是另外一番情况了,具体将在下篇中说明。