Android 尺寸单位转换和屏幕适配相关


 

Android 尺寸单位转换和屏幕适配相关

各种尺寸单位的意义

     dp: Density-independent Pixels   一个抽象的单元,基于屏幕的物理密度。   (dp和dip的意义相同,所以不用区别对待)。   这些单元是相对于160dpi(dots per inch)的屏幕说的,在160dpi的屏幕上,1dp粗略地等于1px。     当运行在更高密度的屏幕上的时候,要绘制1dp的像素数量会放大一个比例,这个比例就是和屏幕密度(dpi)相关。   类似的,在一个低密度的屏幕上,像素数目会缩小一个比例。     dp到px的这个比例将会随着屏幕的密度变化,而不是直接的比例关系。   用dp单位,而不是px,是一种简单的屏幕密度适配解决方式。   换句话说,它提供了一种方式,可以在多种设备上维持真实尺寸一致性。     sp:Scale-independent Pixels   这个有点像dp单位,但是它也根据用户的字体设置(font preference)缩放尺寸。   建议用这种尺寸单位来标注字体尺寸,这样它们将会因为屏幕密度和用户设定而调整。     pt   Points 1/72 inch(英寸),根据屏幕的物理尺寸。     px: Pixels   相应于真实的像素。   这种单位不被建议,因为真实的表达会根据设备的不同相差很远。   每个设备上每英寸的像素数不同(密度不同),并且屏幕上总的像素数也不同(整体大小不同)。     mm:Millimeters  

资源类型

  图片文件通常会分多个文件夹保存,这多个文件夹的后缀名其实表示的是不同的屏幕密度。   以m为基准,屏幕密度(dots per inch)基准和需要图像资源的大小比例如下   l: low density (120dpi) 0.75   m: medium density (160dpi) 1.0 baseline   h: high density (240dpi) 1.5   x: extra-high density (320dpi) 2.0       xx: extra-extra-high density (480dpi)  

尺寸单位转换 工具类

  可以写工具类对尺寸单位进行转换,比如:

package com.mengdd.dimen;

import android.content.Context;

public class DimenUtils {

    public static int sp2px(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dip2px(Context context, int dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}


  Android中的DisplayMetrics这个类描述了关于显示的各种信息,可以利用它查看设备的状态,上述关于屏幕密度的标准的常量也是从这个类中看到的。

   DisplayMetrics的toString()方法如下:

    @Override
    public String toString() {
        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
    }

  其中各个变量解释如下:

    /**
     * The absolute width of the display in pixels.
     */
    public int widthPixels;
    /**
     * The absolute height of the display in pixels.
     */
    public int heightPixels;
    /**
     * The logical density of the display.  This is a scaling factor for the
     * Density Independent Pixel unit, where one DIP is one pixel on an
     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 
     * providing the baseline of the system's display. Thus on a 160dpi screen 
     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
     *  
     * 

This value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of * the overall UI in steps based on gross changes in the display dpi. For * example, a 240x320 screen will have a density of 1 even if its width is * 1.8", 1.3", etc. However, if the screen resolution is increased to * 320x480 but the screen size remained 1.5"x2" then the density would be * increased (probably to 1.5). * * @see #DENSITY_DEFAULT */ public float density; /** * The screen density expressed as dots-per-inch. May be either * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. */ public int densityDpi; /** * A scaling factor for fonts displayed on the display. This is the same * as {@link #density}, except that it may be adjusted in smaller * increments at runtime based on a user preference for the font size. */ public float scaledDensity; /** * The exact physical pixels per inch of the screen in the X dimension. */ public float xdpi; /** * The exact physical pixels per inch of the screen in the Y dimension. */ public float ydpi;

实际设备参数 

  小米2SDisplayMetrics中的toString()方法输出如下:

DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=345.0566, ydpi=342.23157}

参考资料

  官网文档:   http://developer.android.com/guide/topics/resources/more-resources.html     http://developer.android.com/training/basics/supporting-devices/screens.html   http://developer.android.com/training/multiscreen/index.html     本博客之前的讨论见:   http://www.cnblogs.com/mengdd/archive/2012/12/18/2823208.html   http://www.cnblogs.com/mengdd/archive/2013/01/05/2846640.html     其他博客:   http://www.imyukin.com/?p=277