Android DisplayMetrics 在 ICS 上返回错误的屏幕尺寸(以像素为单位) [英] Android DisplayMetrics returns incorrect screen size in pixels on ICS

查看:42
本文介绍了Android DisplayMetrics 在 ICS 上返回错误的屏幕尺寸(以像素为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过了....

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

和....

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

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

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 :

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

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

...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:

width  = 720
height = 1184

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

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

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

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.

推荐答案

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

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

如何隐藏和在 Android ICS build 上显示导航栏

如果链接失效....这是获取实际设备屏幕尺寸的方法.....

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

编辑 (11/19/12)

EDIT (11/19/12)

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

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 DisplayMetrics 在 ICS 上返回错误的屏幕尺寸(以像素为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆