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

2023-09-12 22:19:48 作者:听歌哭泣@

我有一个活动,它使用

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

确定可用的屏幕空间,并决定在何处放置图像。

to determine the useable screen space and decide where to place images.

回归活动后,我点击硬件后退按钮离开活动,矩形值

Returning to the Activity after I click the hardware "back" button to leave the Activity, the rectangle values are

(0,0,800,480)

然而,久违的活动后,我点击了硬件的家按钮离开活动,矩形值

(0,38,800,480)

这引发关闭显示器和图片的位置。

which throws off the display and the image placement.

我怎样才能确保我得到一个一致的值调用时

How can I ensure I get a consistent values when calling

getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);

无论怎样,我离开了应用程序?

no matter how I left the app?

更新:感谢@Reno帮助测试;这似乎是依赖于Android版本比设备。

UPDATE: Thanks to @Reno for helping test; it seems to be dependent on Android version than the device.

推荐答案

韦尔普,如果你阅读源中的注释,它承认一种,这种方法被打破

Welp, if you read the comments in the source, it admits that this method is kind of broken

    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;
    }

您必须忽略版本和LT的outRect.top值; 2.3.3

You will have to ignore the outRect.top value for versions < 2.3.3