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. * * <p>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;
實際設備參數
小米2S,DisplayMetrics中的toString()方法輸出如下:
DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=345.0566, ydpi=342.23157}
參考資料
官網文檔:
本博客之前的討論見:
其他博客: