首页 > 代码库 > 【Android】屏幕尺寸的表示

【Android】屏幕尺寸的表示

1. 单位

- px(pixel): 普通的像素点描述,位图中的一个颜色点(RGBA或者YUV)

- dip(device independent pixels): 设备独立像素。

- dp: 与dip一样。

- pt(point): 标准长度单位。1pt = 1.0 / 72 (in)。

- sp(scaled pixel): 用于字体显示。

- in(inch): 英寸

- mm(millimeter): 毫米

2. 屏幕密度

屏幕类型宽度(pixels)高度(pixels)尺寸(inches)屏幕密度
QVGA2403202.6 - 3.0low
WQVGA2404003.2 - 3.5low
FWQVGA2404323.5 - 3.8low
HVGA3204803.0 - 3.5medium
WVGA4808003.3 - 4.0high
FWVGA4808543.5 - 4.0high
WVGA4808004.8 - 5.5medium
FWVGA4808545.0 - 5.8medium

 

 

 

 

 

 

 

 

 

 

 

 

 

默认屏幕密度:low = 120; medium = 160; high = 240

3. 屏幕单位的换算

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

303    /**304     * Converts an unpacked complex data value holding a dimension to its final floating305     * point value. The two parameters <var>unit</var> and <var>value</var>306     * are as in {@link #TYPE_DIMENSION}.307     *308     * @param unit The unit to convert from.309     * @param value The value to apply the unit to.310     * @param metrics Current display metrics to use in the conversion --311     *                supplies display density and scaling information.312     *313     * @return The complex floating point value multiplied by the appropriate314     * metrics depending on its unit.315     */316    public static float applyDimension(int unit, float value,317                                       DisplayMetrics metrics)318    {319        switch (unit) {320        case COMPLEX_UNIT_PX:321            return value;322        case COMPLEX_UNIT_DIP:323            return value * metrics.density;324        case COMPLEX_UNIT_SP:325            return value * metrics.scaledDensity;326        case COMPLEX_UNIT_PT:327            return value * metrics.xdpi * (1.0f/72);328        case COMPLEX_UNIT_IN:329            return value * metrics.xdpi;330        case COMPLEX_UNIT_MM:331            return value * metrics.xdpi * (1.0f/25.4f);332        }333        return 0;334    }

/frameworks/base/core/java/android/util/DisplayMetrics.java
public static final int DENSITY_LOW = 120;
public static final int DENSITY_MEDIUM = 160;
public static final int DENSITY_TV = 213;
public static final int DENSITY_HIGH = 240;
public static final int DENSITY_XHIGH = 320;
public static final int DENSITY_400 = 400;
public static final int DENSITY_XXHIGH = 480;
public static final int DENSITY_560 = 560;
public static final int DENSITY_XXXHIGH = 640;
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;

public static int DENSITY_DEVICE = getDeviceDensity();
density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;

px = dip * density = dip * DENSITY_DEVICE / DENSITY_DEFAULT.

 

【Android】屏幕尺寸的表示