WindowInsets 在View下的的分發(一)

緒論

Android 4.4后,可以通過將StatusBar和NavigationBar的背景設置為透明或者通過 getWindow().getDecorView().setSystemUiVisibility 的方式,使得 contentView 可以鋪滿整個DecorView。然而大多數情況下,我們并不希望有實質性的內容被StatusBar或者NavigationBar給覆蓋掉,那么Android是如何處理這些看似額外的空間的分發呢,這就涉及到了WindowInsets了。

WindowInsets 在View下的的分發(二)

什么是WindowInsets?

在Android源碼的注釋中解釋為 window content 的一系列插入集合,final 型,不可修改,但后期可能繼續擴展。其主要成員包括 mSystemWindowInsets, mWindowDecorInsets, mStableInsets。

  • mSystemWindowInsets

表示全窗口下,被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而言,則是另外一番情況了,具體將在下篇中說明。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容