开发者

How to tell screen resolution in "DP"?

I am working on an application based on Galaxy S at the moment. I know that Galaxy S is 48开发者_运维问答0 px wide and 800 px tall, but how much is that in 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?


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.

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


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.

In your case:

Width in Inches = 480px/(240px/inch) = 2 inches Height in Inches = 800px/(240px/inch) = 10/3 inches

Check: sqrt(sqr(2) + sqr(10/3)) ~= 4 inches which is the size of a Galaxy S.

So the size in dp is (2 * 160) x (10/3 * 160) = 320dp x 533.3dp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜