图像中px和dp之间的差异

时间:2013-05-29 17:34:57

标签: android xml image pixels

我为我的应用程序提供了一个背景,其中包括两个图像。第一个高度为480px;第二个应该显示在第一个的底部。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
        android:src="@drawable/bg_top"
        android:tileMode="repeat" />
    </item>
    <item
        android:top="480px">
      <bitmap
            android:src="@drawable/bg_bottom"
            android:tileMode="repeat" />
    </item>
</layer-list>

但这不适用于我的智能手机 - 它解释了px错误(实际上看起来顶级值约为200px)
我知道建议使用dp,但如何将此px值转换为dp?我无法以编程方式执行此操作,因为它已保存在xml文档中。

2 个答案:

答案 0 :(得分:0)

这可能在默认文件夹(mdpi)上,所以如果你需要480px并且它在xhdpi上工作,那么你可以将它设置为240dp。

答案 1 :(得分:0)

    px
    Pixels - corresponds to actual pixels on the screen.

    in
    Inches - based on the physical size of the screen.
    1 Inch = 2.54 centimeters

    mm
    Millimeters - based on the physical size of the screen.

    pt
    Points - 1/72 of an inch based on the physical size of the screen.

    dp or dip
    Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

    sp
    Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.