Difference between android dimension: pt and dp

前端 未结 7 1939
迷失自我
迷失自我 2020-11-30 18:28

The documentation says that 160 dp (density-independent) equals 1 inch. And 72 pt is also 1 inch. So I don\'t see why android define a dp measurement while it seems to work

7条回答
  •  日久生厌
    2020-11-30 19:07

    I've struggled with dimensions but I think I've found a way of looking at it that makes sense to me. There's a conversion formula in Wei-Meng Lee's "Beginning Android 4 Application Development" (pg 111):

    Actual pixels = dp * ( dpi / 160 ), where dpi is either 120, 160, 240 or 320

    So, using that formula, I can find which dpi for my phone/emulator is closest to one of those four values and that'll determine the ratio (3/4, 1, 3/2 or 2) used in the formula to convert dp to pixels. It's important to me that dpi in the formula can only assume one of those four values despite the actual device pixel density.

    Referring to: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density, for a Nexus S with a pixel density of 235 dpi the conversion is:

    pixels = dp * 3/2

    So a 160dp button, say, would be 240px (a little wider than an inch with the device's 235 dpi)

    For an HTC Legend, with a pixel density of 181 dpi the formula is:

    pixels = dp * 1

    (because 181 is closest to 160). So that 160dp button would be 160pixels, or slightly less than an inch on the device with its pixel density of 181dpi.

    This helps me understand the incorrectness of the previous Android documentation "1 dp will always equal 1/160in, regardless of screen density".

    These are the two main points I'm trying to get in my head :)

    1. a range of actual device pixel densities (for example: 141-199) will result in the same ratio (1) in the conversion formula
    2. but unless the pixel density of a particular device is exactly 160 dpi, the 160 dp won't be an inch on the device...close above or below, but not exactly

提交回复
热议问题