Android的:如何获得设备的实际屏幕尺寸?如何获得、实际、设备、屏幕尺寸

2023-09-05 10:54:28 作者:叼着棒棒糖闯天下

我曾尝试不同的方式来获得设备的屏幕尺寸,但它总是返回我错了尺寸(791x480,而不是854×480),这可能是由于导航栏。我的设备正在运行软糖4.1.1。

I have tried different ways to get the screen size of the device, but it always returned me the wrong size (791x480 instead of 854x480), it might be due to the navigation bar. My device is currently running JellyBean 4.1.1.

我想:

Display display = getWindowManager().getDefaultDisplay();  
Point size = new Point();
display.getSize(size);
OpenGLRenderer.widthScreen = size.x;
OpenGLRenderer.heightScreen = size.y;

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String resolution = dm.widthPixels + "x" + dm.heightPixels;

我用下面的code,使应用程序全屏:

I'm using the following code to make the app full screen:

setContentView(mGLSurfaceView);
mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

我注意到,在logcat的大小合适,则返回:

I noticed that in the logcat the right size is returned :

01-01 03:05:06.967: I/InputReader(1961): 
    Device reconfigured: id=8, name='cyttsp-spi', 
    surface size is now 480x854, mode is 1

在我AndroidManifest:

In my AndroidManifest :

uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"

我用:

Get在冰淇淋三明治的实际屏幕分辨率 HTC感觉实际分辨率 Android的:如何获得屏幕尺寸 Get the real screen resolution on Ice Cream Sandwich HTC Sensation Real Resolution Android: How to get screen dimensions

为什么我不能够得到真正的大小(的480x854 )?或者,我怎么可以使用由重新配置设备返回地面?

Why am I not able to get the real size (480x854) ? Or how can I use the surface returned by the "Device reconfigured" ?

推荐答案

尝试调用

    display.getRealSize();

免责声明: 我没有意识到这一点,但是这将仅适用于API 17(安卓4.2 /软糖)及以上。

DISCLAIMER: I did not realize this, but this will only work for API 17 (Android 4.2/JellyBean) and up.

 
精彩推荐