緒論
Android 4.4后,可以通過將StatusBar和NavigationBar的背景設(shè)置為透明或者通過 getWindow().getDecorView().setSystemUiVisibility 的方式,使得 contentView 可以鋪滿整個(gè)DecorView。然而大多數(shù)情況下,我們并不希望有實(shí)質(zhì)性的內(nèi)容被StatusBar或者NavigationBar給覆蓋掉,那么Android是如何處理這些看似額外的空間的分發(fā)呢,這就涉及到了WindowInsets了。
WindowInsets 在View下的的分發(fā)(二)
什么是WindowInsets?
在Android源碼的注釋中解釋為 window content 的一系列插入集合,final 型,不可修改,但后期可能繼續(xù)擴(kuò)展。其主要成員包括 mSystemWindowInsets, mWindowDecorInsets, mStableInsets。
- mSystemWindowInsets
表示全窗口下,被StatusBar, NavigationBar, IME 或者其它系統(tǒng)窗口部分或者全部覆蓋的區(qū)域。
- mWindowDecorInsets
表示內(nèi)容窗口下,被Android FrameWork提供的窗體,諸如ActionBar, TitleBar, ToolBar,部分或全部覆蓋區(qū)域。
- mStableInsets
表示全窗口下,被系統(tǒng)UI部分或者全部覆蓋的區(qū)域。
如何理解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的概念已經(jīng)區(qū)別于View的Rect了,它的四個(gè)點(diǎn)已經(jīng)不再表示圍成矩形的坐標(biāo),而表示的是insets需要的左右的寬度,頂部和底部需要的高度。
消費(fèi)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的消費(fèi)分為全部消費(fèi)和部分消費(fèi),如果不存在消費(fèi),則返回對象本身,如果消費(fèi)了,則返回將消費(fèi)部分置為0的對象copy。
//判斷WindowInsets是否被消費(fèi)掉
public boolean isConsumed() {
return mSystemWindowInsetsConsumed && mWindowDecorInsetsConsumed && mStableInsetsConsumed;
}
可見要消費(fèi)掉WindowInsets,需要同時(shí)消耗掉 mSystemWindowInsets, mWindowDecorInsets, mStableInsets。
誰在消費(fèi)WindowInsets
從WindowInsets的注釋中,可以發(fā)現(xiàn)兩個(gè)函數(shù)
/**
* @see View.OnApplyWindowInsetsListener
* @see View#onApplyWindowInsets(WindowInsets)
*/
深入View的源碼可以發(fā)現(xiàn),如果View設(shè)定了OnApplyWindowInsetsListener后,會(huì)采用OnApplyWindowInsetsListener的實(shí)現(xiàn)來處理WindowInsets,否則才會(huì)使用onApplyWindowInsets(WindowInsets)方法來處理WindowInsets,在dispatchApplyWindowInsets(WindowInsets)中進(jìn)行分發(fā)處理,代碼如下:
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;
}
}
可以發(fā)現(xiàn),對于View而言,會(huì)在dispatchApplyWindowInset的過程中Apply WindowInsets。對應(yīng)的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自身也會(huì)Apply WindowInsets,如果該過程中沒有消耗掉WindowInsets,則會(huì)繼續(xù)傳遞給 child 處理WindwInsets,如果child中消耗了WindowInsets, 則會(huì)退出分發(fā)循環(huán)。
再看一下,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)方法
//當(dāng)if條件成立時(shí),會(huì)進(jìn)入 dispatchApply邏輯,不成立則進(jìn)入實(shí)際的處理邏輯fitSystemWindowsInt(insets)
//如此設(shè)置的原因在于,不直接調(diào)用fitSystemWindowsInt(insets)方法,而是要經(jīng)過dispatchApply后再調(diào)用
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)方法
//當(dāng)View設(shè)置了fitSystemWindow = true 后, 才會(huì)處理 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;
}
//計(jì)算是否消費(fèi)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;
}
}
//重新調(diào)整View的padding值
protected void internalSetPadding(int left, int top, int right, int bottom) {
//省略部分非關(guān)鍵代碼
...
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的區(qū)別在于dispatchApplyWindowInsets(...)函數(shù),網(wǎng)上大數(shù)文章將其當(dāng)成一個(gè)分發(fā)邏輯看待,其實(shí)更準(zhǔn)確的說法應(yīng)該是一個(gè)消費(fèi)或者分發(fā)邏輯。
總結(jié):
WindowInsets是一個(gè)描述了屏幕上的各個(gè)插入空間的一個(gè)類,其在后期中可以擴(kuò)展,WindowInsets在消耗后將不再繼續(xù)傳遞。對于普通的View而言,要消耗WindowInsets必須先設(shè)置View的 fitsSystemWindows 的屬性為true。這也是為什么對普通View層級設(shè)置fitsSystemWindows屬性為true卻只有一個(gè)頂層的生效而已。單對于一些特殊的View而言,則是另外一番情況了,具體將在下篇中說明。