getWindowVisibleDisplayFrame()在Android 2.2,2.3中给出了不同的值(但不是2.3.3)

Thu*_*bit 5 android android-activity

我有一个Activity用途

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
Run Code Online (Sandbox Code Playgroud)

确定可用的屏幕空间并决定放置图像的位置.

单击硬件"返回"按钮离开后返回活动Activity,矩形值为

(0,0,800,480)
Run Code Online (Sandbox Code Playgroud)

但是,在我点击硬件"home"按钮离开后返回Activity Activity,矩形值为

(0,38,800,480)
Run Code Online (Sandbox Code Playgroud)

它会抛出显示和图像放置.

如何确保在呼叫时获得一致的值

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
Run Code Online (Sandbox Code Playgroud)

无论我怎么离开应用程序?

更新:感谢@Reno帮助测试; 它似乎依赖于Android版本而不是设备.

Ren*_*eno 4

好吧,如果您阅读源代码中的注释,它承认这种方法有点损坏

    public void getWindowVisibleDisplayFrame(Rect outRect) {
    if (mAttachInfo != null) {
        try {
            mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
        } catch (RemoteException e) {
            return;
        }
        // XXX This is really broken, and probably all needs to be done
        // in the window manager, and we need to know more about whether
        // we want the area behind or in front of the IME.
        final Rect insets = mAttachInfo.mVisibleInsets;
        outRect.left += insets.left;
        outRect.top += insets.top;
        outRect.right -= insets.right;
        outRect.bottom -= insets.bottom;
        return;
    }
Run Code Online (Sandbox Code Playgroud)

对于 < 2.3.3 版本,您必须忽略 outRect.top 值