Android:显示错误的屏幕分辨率

发布于 2024-10-18 11:36:55 字数 405 浏览 7 评论 0原文

我试图使用此代码获取 Android 手机的屏幕分辨率

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

当我在 Galaxy S 手机中尝试此代码时,我得到的屏幕分辨率为 320x533 像素,但实际上 Galaxy S 的屏幕分辨率为 480x800 像素。那么代码有什么问题吗?

如何获取特定设备的实际屏幕分辨率?

I was trying to get the screen resolution of android phones,using this code

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

When i tried this code in my Galaxy S phone i got the screen resolution as 320x533 px, but in reality the Galaxy S got a screen resolution of 480x800 px. So what's wrong with the code??

How can i get the actual screen resolution of a particular device??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

哆啦不做梦 2024-10-25 11:36:55

最后,经过两天的搜索和测试,我终于找到了真正的问题所在。
所有上述编码都会给出正确的分辨率。但最重要的是,我们需要在清单文件中提及目标SDK。否则,如果密度大于1,则会给出错误的结果。

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

需要将targetSDK版本设置为4-9之间的任何值。
希望这对将来遇到同样问题的人有所帮助。

Finally after 2 days of searching and testing, i finally managed to find out what's the real issue here..
All those above coding will give correct resolution. But the most important this is that we need to mention the target SDK in the manifest file.. Otherwise it will give wrong result if the density is more than 1.

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

need to set the targetSDK version from anything between 4-9..
Hope this will help some one in future facing the same issue.

往昔成烟 2024-10-25 11:36:55

尝试使用这个:

width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;

看起来你在除以 Galaxy S 的密度( 1.5 )后得到的像素。

480 / 1.5 = 320
799.5 / 1.5 = 533

编辑:

density = getResources().getDisplayMetrics().density;

Try to use this:

width = getResources().getDisplayMetrics().widthPixels;
height = getResources().getDisplayMetrics().heightPixels;

Seems like you're getting the pixel after division of the density ( 1.5 ) of Galaxy S.

480 / 1.5 = 320
799.5 / 1.5 = 533

Edit:

density = getResources().getDisplayMetrics().density;
北笙凉宸 2024-10-25 11:36:55

通过将此行添加到清单文件来禁用预缩放:

<supports-screens android:anyDensity="true" />

Position:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="...."
  android:versionCode="1"
  android:versionName="1.0">

<supports-screens android:anyDensity="true" />

<application android:label="@string/app_name" >
...

之后您的显示将不再缩放,并且您还可以获得正确的显示分辨率。

Disable pre-scaling by adding this line to your manifest file:

<supports-screens android:anyDensity="true" />

Position:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="...."
  android:versionCode="1"
  android:versionName="1.0">

<supports-screens android:anyDensity="true" />

<application android:label="@string/app_name" >
...

After that your display will not be scaled anymore, and you also get the correct display resolution.

感情废物 2024-10-25 11:36:55

试试这个..

 Display display = getWindowManager().getDefaultDisplay();
int ScreenHeight = display.getHeight();
int ScreenWidth = display.getWidth();

try this..

 Display display = getWindowManager().getDefaultDisplay();
int ScreenHeight = display.getHeight();
int ScreenWidth = display.getWidth();
孤城病女 2024-10-25 11:36:55

这似乎有效..虽然我不知道它是否是正确的方法..

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm);

density=dm.densityDpi; 
width=(density/160)*dm.widthPixels; 
height=(density/160)*dm.heightPixels;

This seems to be working..though i dont know if its a right method..

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm);

density=dm.densityDpi; 
width=(density/160)*dm.widthPixels; 
height=(density/160)*dm.heightPixels;
笑饮青盏花 2024-10-25 11:36:55

以下是我的做法:

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

不在 Activity 中时:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

在引入 getSize 之前(在 API 级别 13 中),您可以使用现已弃用的 getWidth 和 getHeight 方法:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Here is how i did it:

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

When not in an Activity:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文