Android:显示错误的屏幕分辨率
我试图使用此代码获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最后,经过两天的搜索和测试,我终于找到了真正的问题所在。
所有上述编码都会给出正确的分辨率。但最重要的是,我们需要在清单文件中提及目标SDK。否则,如果密度大于1,则会给出错误的结果。
需要将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.
need to set the targetSDK version from anything between 4-9..
Hope this will help some one in future facing the same issue.
尝试使用这个:
看起来你在除以 Galaxy S 的密度( 1.5 )后得到的像素。
编辑:
Try to use this:
Seems like you're getting the pixel after division of the density ( 1.5 ) of Galaxy S.
Edit:
通过将此行添加到清单文件来禁用预缩放:
Position:
之后您的显示将不再缩放,并且您还可以获得正确的显示分辨率。
Disable pre-scaling by adding this line to your manifest file:
Position:
After that your display will not be scaled anymore, and you also get the correct display resolution.
试试这个..
try this..
这似乎有效..虽然我不知道它是否是正确的方法..
This seems to be working..though i dont know if its a right method..
以下是我的做法:
不在 Activity 中时:
在引入 getSize 之前(在 API 级别 13 中),您可以使用现已弃用的 getWidth 和 getHeight 方法:
Here is how i did it:
When not in an Activity:
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated: