Android DisplayMetrics 在 ICS 上返回错误的屏幕尺寸(以像素为单位)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10991194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:39:57  来源:igfitidea点击:

Android DisplayMetrics returns incorrect screen size in pixels on ICS

androidandroid-layoutandroid-screenandroid-resolution

提问by dell116

I've tried this....

我试过这个....

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int fullscreenheight = metrics.heightPixels;
int fullscreenwidth = metrics.widthPixels;

and....

和....

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

The Gnex has a display of 720×1280. The returned result for width/height (depending on orientation of course) is never 1280. I thought this might have something to do with the on screen navigation bar in Ice Cream Sandwich, so I hide that with :

Gnex 的显示屏为 720×1280。宽度/高度的返回结果(当然取决于方向)永远不会是 1280。我认为这可能与 Ice Cream Sandwich 中的屏幕导航栏有关,因此我将其隐藏:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

...and then started a thread which continuously logs the screen size height/width. Even after the ICS navigation bar was completely gone....the screen size would never report the 1280 value. I would get numbers like this:

...然后启动一个线程,连续记​​录屏幕大小的高度/宽度。即使在 ICS 导航栏完全消失之后……屏幕尺寸也永远不会报告 1280 值。我会得到这样的数字:

width  = 720
height = 1184

How do you get the true device screen resolution in ICS?

如何在 ICS 中获得真实的设备屏幕分辨率?

The reason why I need this value is because my app plays video and I would like for the video to take up the entire screen when the device is in landscape orientation. Now I know what you're thinking, why not just give the videoview a value of "match_parent" for the height/width? The reason is because my videos have multiple aspect ratios and I'd like to be able to calculate the correct video size depending on the entire width of the screen.

我需要这个值的原因是我的应用程序播放视频,我希望视频在设备处于横向时占据整个屏幕。现在我知道你在想什么,为什么不给视频视图一个高度/宽度的“match_parent”值?原因是因为我的视频有多个纵横比,我希望能够根据屏幕的整个宽度计算正确的视频大小。

采纳答案by dell116

I found this hidden treasure for ICS ONLY......if you're targeting API's higher than ICS see the comment by Chris Schmich below.

我只为 ICS 找到了这个隐藏的宝藏......如果你的目标 API 高于 ICS,请参阅下面 Chris Schmich 的评论。

How to hide and display the navigation bar on Android ICS build

如何在 Android ICS 构建中隐藏和显示导航栏

In case the link dies....here's how to get the actual device screen size.....

如果链接失效......这里是如何获得实际设备屏幕尺寸......

Display display = getWindowManager().getDefaultDisplay();     
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawWidth = (Integer) mGetRawW.invoke(display);
int rawHeight = (Integer) mGetRawH.invoke(display);

EDIT(11/19/12)

编辑(11/19/12)

The above code WILL NOT WORK on Android 4.2 and an exception will be thrown....I have yet to find a way to get the full screen size of a device in Android 4.2. When I do, I will edit this answer, but for now, you should code with contingency for android 4.2!!

上面的代码在 Android 4.2 上不起作用,并且会抛出异常......我还没有找到一种方法来获得 Android 4.2 中设备的全屏尺寸。当我这样做时,我会编辑这个答案,但现在,你应该为 android 4.2 编码!

回答by Frank Nguyen

From the answer of Ahmed, this is full code without error:

从艾哈迈德的回答来看,这是没有错误的完整代码:

    int width = 0, height = 0;
    final DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    try {
        // For JellyBean 4.2 (API 17) and onward
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealMetrics(metrics);

            width = metrics.widthPixels;
            height = metrics.heightPixels;
        } else {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");

            try {
                width = (Integer) mGetRawW.invoke(display);
                height = (Integer) mGetRawH.invoke(display);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (NoSuchMethodException e3) {  
        e3.printStackTrace();
    }

回答by Sam

You can use getRealSizeto get the screen resolution. It's officially supported in API 17 and higher, but it actually existed in the SDK since API 14. If you use the SDK platform for API 17 or higher, you can access the method normally and just bypass the warning about it only being available in API 17.

您可以使用getRealSize来获取屏幕分辨率。它在 API 17 及更高版本中得到官方支持,但它实际上从 API 14 开始就存在于 SDK 中。 如果您使用 API 17 或更高版本的 SDK 平台,您可以正常访问该方法,只需绕过有关它仅在 API 中可用的警告17.

/**
 * Gets the device's native screen resolution rotated
 * based on the device's current screen orientation.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) //To remove warning about using
                                               //getRealSize prior to API 17
private Point screenResolution() {
    WindowManager windowManager =
        (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point screenResolution = new Point();

    if (Build.VERSION.SDK_INT < 14)
        throw new RuntimeException("Unsupported Android version.");
    display.getRealSize(screenResolution);

    return screenResolution;    
}

I've tested this in a real Android 5.0.2 device and an emulator running 4.1.1 (and possibly others).

我已经在真正的 Android 5.0.2 设备和运行 4.1.1(可能还有其他)的模拟器中测试了这个。

Thanks to bencfor pointing this out in the comments.

由于BENC指出这一点的意见

回答by Ahmed

Try this

尝试这个

    final DisplayMetrics metrics = new DisplayMetrics(); 
    Display display = getWindowManager().getDefaultDisplay();     
    Method mGetRawH = null,mGetRawW = null;

    try {
                    // For JellyBeans and onward
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
            display.getRealMetrics(metrics);

            realWidth = metrics.widthPixels;
            realHeight = metrics.heightPixels;
        }else{
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");

            try {
                realWidth = (Integer) mGetRawW.invoke(display);
                realHeight = (Integer) mGetRawH.invoke(display);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

回答by slartus

public static Point getRealSize(Display display) {
        Point outPoint = new Point();
        Method mGetRawH;
        try {
            mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            outPoint.x = (Integer) mGetRawW.invoke(display);
            outPoint.y = (Integer) mGetRawH.invoke(display);
            return outPoint;
        } catch (Throwable e) {
            return null;
        }
    }

    public static Point getSize(Display display) {
        if (Build.VERSION.SDK_INT >= 17) {
            Point outPoint = new Point();
            DisplayMetrics metrics = new DisplayMetrics();
            display.getRealMetrics(metrics);
            outPoint.x = metrics.widthPixels;
            outPoint.y = metrics.heightPixels;
            return outPoint;
        }
        if (Build.VERSION.SDK_INT >= 14) {
            Point outPoint = getRealSize(display);
            if (outPoint != null)
                return outPoint;
        }
        Point outPoint = new Point();
        if (Build.VERSION.SDK_INT >= 13) {
            display.getSize(outPoint);
        } else {
            outPoint.x = display.getWidth();
            outPoint.y = display.getHeight();
        }
        return outPoint;
    }

回答by slartus

On some devices, e.g. tablets, the navigation bar can't be hidden at all (source, see section 'Controls for system UI visibility') because there's no hardware buttons as backup. If on your device you've managed to hide the bar though, you could make a full screen View and request its size with getWidth() and getHeight().

在某些设备上,例如平板电脑,导航栏根本无法隐藏(来源,请参阅“系统 UI 可见性控件”部分),因为没有硬件按钮作为备份。如果在您的设备上您设法隐藏了栏,您可以制作全屏视图并使用 getWidth() 和 getHeight() 请求其大小。

As far as I know, there isn't a reliable way to get the screen size on all ICS devices.

据我所知,没有一种可靠的方法可以在所有 ICS 设备上获取屏幕尺寸。