Android源码阅读:DisplayMetrics类
概述
显示单元
Px (Pixel像素)
也称为图像元素,是作为图像构成的基本单元,单个像素的大小并不固定,跟随屏幕大小和像素数量的关系变化(屏幕越大,像素越低,单个像素越大,反之亦然)。所以在使用像素作为设计单位时,在不同的设备上可能会有缩放或拉伸的情况。
Resolution(分辨率)
是指屏幕的垂直和水平方向的像素数量,如果分辨率是 1920 * 1080 ,那就是垂直方向有 1920 个像素,水平方向有 1080 个像素。
Dpi(像素密度)
是指屏幕上每英寸(1英寸 = 2.54 厘米)距离中有多少个像素点。如果屏幕为 320*240,屏幕长 2 英寸宽 1.5 英寸,Dpi = 320 / 2 = 240 / 1.5 = 160。
dip / dp (适配不同屏幕下的像素数量)
统一使用dp,长度单位,同一个单位在不同的设备上有不同的显示效果,具体效果根据设备的密度有关,公式为:
d p = p x / ( d p i / 160 ) . dp= px / (dpi / 160). dp=px/(dpi/160).
DisplayMetrics类主要是用来描述设备显示的一些信息,像是设备大小、密度以及字体的缩放。
常量
设备密度Density的相关数值
public static final int DENSITY_LOW = 120;
public static final int DENSITY_140 = 140;
public static final int DENSITY_MEDIUM = 160;
public static final int DENSITY_180 = 180;
public static final int DENSITY_200 = 200;
public static final int DENSITY_TV = 213;
public static final int DENSITY_220 = 220;
public static final int DENSITY_HIGH = 240;
public static final int DENSITY_260 = 260;
public static final int DENSITY_280 = 280;
public static final int DENSITY_300 = 300;
public static final int DENSITY_XHIGH = 320;
public static final int DENSITY_340 = 340;
public static final int DENSITY_360 = 360;
public static final int DENSITY_400 = 400;
public static final int DENSITY_420 = 420;
public static final int DENSITY_440 = 440;
public static final int DENSITY_450 = 450;
public static final int DENSITY_XXHIGH = 480;
public static final int DENSITY_560 = 560;
public static final int DENSITY_600 = 600;
public static final int DENSITY_XXXHIGH = 640;
由常量定义的数值,我们可以看到
drawable的这些资源目录对应了相应的设备屏幕大小。Android中默认密度是DENSITY_MEDIUM,即160dpi。
// 默认dpi
public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
// 默认scale值
public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
成员变量
成员变量主要是描述了设备显示信息,包括宽高像素、设备密度和dpi。
@Deprecated
@UnsupportedAppUsage
public static int DENSITY_DEVICE = getDeviceDensity();
/**
* The absolute width of the available display size in pixels.
*/
public int widthPixels;
/**
* The absolute height of the available display size 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;
/**
* The reported display width prior to any compatibility mode scaling
* being applied.
* @hide
*/
@UnsupportedAppUsage
public int noncompatWidthPixels;
/**
* The reported display height prior to any compatibility mode scaling
* being applied.
* @hide
*/
@UnsupportedAppUsage
public int noncompatHeightPixels;
/**
* The reported display density prior to any compatibility mode scaling
* being applied.
* @hide
*/
public float noncompatDensity;
/**
* The reported display density prior to any compatibility mode scaling
* being applied.
* @hide
*/
@UnsupportedAppUsage
public int noncompatDensityDpi;
/**
* The reported scaled density prior to any compatibility mode scaling
* being applied.
* @hide
*/
public float noncompatScaledDensity;
/**
* The reported display xdpi prior to any compatibility mode scaling
* being applied.
* @hide
*/
public float noncompatXdpi;
/**
* The reported display ydpi prior to any compatibility mode scaling
* being applied.
* @hide
*/
public float noncompatYdpi;
方法
1、复制(setTo)
public void setTo(DisplayMetrics o) {
if (this == o) {
return;
}
widthPixels = o.widthPixels;
heightPixels = o.heightPixels;
density = o.density;
densityDpi = o.densityDpi;
scaledDensity = o.scaledDensity;
xdpi = o.xdpi;
ydpi = o.ydpi;
noncompatWidthPixels = o.noncompatWidthPixels;
noncompatHeightPixels = o.noncompatHeightPixels;
noncompatDensity = o.noncompatDensity;
noncompatDensityDpi = o.noncompatDensityDpi;
noncompatScaledDensity = o.noncompatScaledDensity;
noncompatXdpi = o.noncompatXdpi;
noncompatYdpi = o.noncompatYdpi;
}
2、设置默认值(setToDefaults)
public void setToDefaults() {
widthPixels = 0;
heightPixels = 0;
density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
densityDpi = DENSITY_DEVICE;
scaledDensity = density;
xdpi = DENSITY_DEVICE;
ydpi = DENSITY_DEVICE;
noncompatWidthPixels = widthPixels;
noncompatHeightPixels = heightPixels;
noncompatDensity = density;
noncompatDensityDpi = densityDpi;
noncompatScaledDensity = scaledDensity;
noncompatXdpi = xdpi;
noncompatYdpi = ydpi;
}
3、判断显示信息是否一致(equals、equalsPhysical)
@Override
public boolean equals(Object obj) {
return obj instanceof DisplayMetrics && equals((DisplayMetrics)obj);
}
/**
* Returns true if these display metrics equal the other display metrics.
*
* @param other The display metrics with which to compare.
* @return True if the display metrics are equal.
*/
public boolean equals(DisplayMetrics other) {
return equalsPhysical(other)
&& scaledDensity == other.scaledDensity
&& noncompatScaledDensity == other.noncompatScaledDensity
}
/**
* Returns true if the physical aspects of the two display metrics
* are equal. This ignores the scaled density, which is a logical
* attribute based on the current desired font size.
*
* @param other The display metrics with which to compare.
* @return True if the display metrics are equal.
* @hide
*/
public boolean equalsPhysical(DisplayMetrics other) {
return other != null
&& widthPixels == other.widthPixels
&& heightPixels == other.heightPixels
&& density == other.density
&& densityDpi == other.densityDpi
&& xdpi == other.xdpi
&& ydpi == other.ydpi
&& noncompatWidthPixels == other.noncompatWidthPixels
&& noncompatHeightPixels == other.noncompatHeightPixels
&& noncompatDensity == other.noncompatDensity
&& noncompatDensityDpi == other.noncompatDensityDpi
&& noncompatXdpi == other.noncompatXdpi
&& noncompatYdpi == other.noncompatYdpi;
}
4、重写hasCode
DisplayMetrics 根据宽高像素点以及像素dpi的乘积生成hashCode。
@Override
public int hashCode() {
return widthPixels * heightPixels * densityDpi;
}
@Override
public String toString() {
return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
}
5、获取设备密度(getDeviceDensity)
由上面的setToDefaults函数可以看到,dpi的值是由DENSITY_DEVICE决定的,而DENSITY_DEVICE是由getDeviceDensity函数获取得到的。
由这个函数我们可以看到,这个值是由设备厂商决定的。首先会先从系统配置文件中读取“qemu.sf.lcd_density”属性对应的值,如果没有找到,则读取“ro.sf.lcd_density”属性对应的值,如果还是没有找到,则使用DENSITY_DEFAULT,即160。
(SystemProperties类是framework源码,暂不深究。)
private static int getDeviceDensity() {
// qemu.sf.lcd_density can be used to override ro.sf.lcd_density
// when running in the emulator, allowing for dynamic configurations.
// The reason for this is that ro.sf.lcd_density is write-once and is
// set by the init process when it parses build.prop before anything else.
return SystemProperties.getInt("qemu.sf.lcd_density",
SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
}