Android 如何在“DP”中判断屏幕分辨率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3530320/
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
How to tell screen resolution in "DP"?
提问by RobGThai
I am working on an application based on Galaxy S at the moment. I know that Galaxy S is 480 px wide and 800 px tall, but how much is that in DP?
我目前正在开发基于 Galaxy S 的应用程序。我知道 Galaxy S 宽 480 像素,高 800 像素,但在 DP 中是多少?
Lets say if I want to have two Layout side by side, I'll have them setting to 240 px. But how do I know what value I should use in DP unit?
假设我想并排放置两个布局,我会将它们设置为 240 像素。但是我怎么知道我应该在 DP 单元中使用什么值呢?
采纳答案by WarrenFaith
The conversion of dip units to screen pixels is simple: pixels = dips * (density / 160). For example, on 240 dpi screen, 1 dip would equal 1.5 physical pixels. Using dip units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
倾角单位到屏幕像素的转换很简单:像素 = 倾角 *(密度 / 160)。例如,在 240 dpi 的屏幕上,1 下降将等于 1.5 个物理像素。强烈建议使用浸入单位来定义应用程序的 UI,以确保在不同屏幕上正确显示 UI。
Found: http://developer.android.com/guide/practices/screens_support.html
发现:http: //developer.android.com/guide/practices/screens_support.html
[edit]
I just had to used this. Using the DisplayMetrics.density
returns only 0.75, 1 and 1.5. Use DisplayMetrics.densityDpi
instead or change the math to pixels = dips * DisplayMetrics.density
[编辑] 我只是不得不使用这个。使用DisplayMetrics.density
回报只有 0.75、1 和 1.5。使用DisplayMetrics.densityDpi
替代或更改算算pixels = dips * DisplayMetrics.density
回答by Alexander Suraphel
What I found out is that DP
is a not about resolution at all, it's about screen size because it's based on 160dpi baseline. An easy way to calculate will be: ScreenSizeInInches
* 160.
我发现这DP
根本不是关于分辨率,而是关于屏幕尺寸,因为它基于 160dpi 基线。一种简单的计算方法是:ScreenSizeInInches
* 160。
In your case:
在你的情况下:
Width in Inches = 480px/(240px/inch)
= 2 inches
Height in Inches = 800px/(240px/inch)
= 10/3 inches
以英寸为单位的宽度 = 480px/(240px/inch)
=2 inches
以英寸为单位的高度800px/(240px/inch)
==10/3 inches
Check: sqrt(sqr(2) + sqr(10/3))
~= 4 inches
which is the size of a Galaxy S.
检查:sqrt(sqr(2) + sqr(10/3))
~=4 inches
这是 Galaxy S 的大小。
So the size in dp is (2 * 160) x (10/3 * 160)
= 320dp x 533.3dp
所以dp中的大小是(2 * 160) x (10/3 * 160)
=320dp x 533.3dp