Android中dpi 和density到底是什么关系?

源码:

/frameworks/base/core/java/android/util/DisplayMetrics.java

194    /**
195     * The logical density of the display.  This is a scaling factor for the
196     * Density Independent Pixel unit, where one DIP is one pixel on an
197     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
198     * providing the baseline of the system's display. Thus on a 160dpi screen
199     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
200     *
201     * <p>This value does not exactly follow the real screen size (as given by
202     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
203     * the overall UI in steps based on gross changes in the display dpi.  For
204     * example, a 240x320 screen will have a density of 1 even if its width is
205     * 1.8", 1.3", etc. However, if the screen resolution is increased to
206     * 320x480 but the screen size remained 1.5"x2" then the density would be
207     * increased (probably to 1.5).
208     *
209     * @see #DENSITY_DEFAULT
210     */
211    public float density;

默认:

299    public void setToDefaults() {
300        widthPixels = 0;
301        heightPixels = 0;
302        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
303        densityDpi =  DENSITY_DEVICE;
304        scaledDensity = density;
305        xdpi = DENSITY_DEVICE;
306        ydpi = DENSITY_DEVICE;
307        noncompatWidthPixels = widthPixels;
308        noncompatHeightPixels = heightPixels;
309        noncompatDensity = density;
310        noncompatDensityDpi = densityDpi;
311        noncompatScaledDensity = scaledDensity;
312        noncompatXdpi = xdpi;
313        noncompatYdpi = ydpi;
314    }

注释:

默认情况:density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;

DENSITY_DEVICE和DENSITY_DEFAULT在DisplayMetrics.java都有说明:

35    /**
36     * Standard quantized DPI for medium-density screens.
37     */
38    public static final int DENSITY_MEDIUM = 160;
...
155    public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
...
162
163    /**
164     * The device's current density.
165     * <p>
166     * This value reflects any changes made to the device density. To obtain
167     * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
168     *
169     * @hide This value should not be used.
170     * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
171     *             device density or {@link #densityDpi} to obtain the current
172     *             density for a specific display.
173     */
174    @Deprecated
175    public static int DENSITY_DEVICE = getDeviceDensity();
...
370    private static int getDeviceDensity() {
371        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
372        // when running in the emulator, allowing for dynamic configurations.
373        // The reason for this is that ro.sf.lcd_density is write-once and is
374        // set by the init process when it parses build.prop before anything else.
375        return SystemProperties.getInt("qemu.sf.lcd_density",
376                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
377    }

即:dpi为160时,density为1
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT_熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值