Density in AVD manager

hackhack

Active Member
Licensed User
Longtime User
When you make a new virtual device you have to enter a density - how important is that? And how do you figure out what it is?
 

jschuchert

Active Member
Licensed User
Longtime User
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

Aren't dp and px reversed in the formula?
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
I understand what the quoted part of the document is trying to say and I agree with the greater the density, the more screen pixels per dp, but I think the way it is expressed is confusing:
px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels.

IF px = 1 physical pixel and dp = 1 density independent pixel
Solving the equation algebraically for 240 dpi
px=dp*1.5 (240/160)
therefore
dp = px/1.5
but to satisfy the statement in red
it would have to be dp=px * 1.5
or
more physical pixels per dp.
That's why I thought px and dp should be exchanged in the equation

Is my logic wrong?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
therefore
dp = px/1.5
Correct for a 240dpi scale 1.5 screen!
but to satisfy the statement in red
it would have to be dp=px * 1.5
Wrong I'm afraid. Device independent pixels are meant to display approximately the same physical size on all screens so the higher the dpi of the screen the more physical pixels are needed for each dp.
 
Upvote 0
Top