1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util; 18 19 import android.os.SystemProperties; 20 21 22 /** 23 * A structure describing general information about a display, such as its 24 * size, density, and font scaling. 25 * <p>To access the DisplayMetrics members, initialize an object like this:</p> 26 * <pre> DisplayMetrics metrics = new DisplayMetrics(); 27 * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre> 28 */ 29 public class DisplayMetrics { 30 /** 31 * Standard quantized DPI for low-density screens. 32 */ 33 public static final int DENSITY_LOW = 120; 34 35 /** 36 * Standard quantized DPI for medium-density screens. 37 */ 38 public static final int DENSITY_MEDIUM = 160; 39 40 /** 41 * This is a secondary density, added for some common screen configurations. 42 * It is recommended that applications not generally target this as a first 43 * class density -- that is, don't supply specific graphics for this 44 * density, instead allow the platform to scale from other densities 45 * (typically {@link #DENSITY_HIGH}) as 46 * appropriate. In most cases (such as using bitmaps in 47 * {@link android.graphics.drawable.Drawable}) the platform 48 * can perform this scaling at load time, so the only cost is some slight 49 * startup runtime overhead. 50 * 51 * <p>This density was original introduced to correspond with a 52 * 720p TV screen: the density for 1080p televisions is 53 * {@link #DENSITY_XHIGH}, and the value here provides the same UI 54 * size for a TV running at 720p. It has also found use in 7" tablets, 55 * when these devices have 1280x720 displays. 56 */ 57 public static final int DENSITY_TV = 213; 58 59 /** 60 * Standard quantized DPI for high-density screens. 61 */ 62 public static final int DENSITY_HIGH = 240; 63 64 /** 65 * Standard quantized DPI for extra-high-density screens. 66 */ 67 public static final int DENSITY_XHIGH = 320; 68 69 /** 70 * Standard quantized DPI for extra-extra-high-density screens. Applications 71 * should not generally worry about this density; relying on XHIGH graphics 72 * being scaled up to it should be sufficient for almost all cases. 73 */ 74 public static final int DENSITY_XXHIGH = 480; 75 76 /** 77 * Standard quantized DPI for extra-extra-extra-high-density screens. Applications 78 * should not generally worry about this density; relying on XHIGH graphics 79 * being scaled up to it should be sufficient for almost all cases. A typical 80 * use of this density would be 4K television screens -- 3840x2160, which 81 * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH. 82 */ 83 public static final int DENSITY_XXXHIGH = 640; 84 85 /** 86 * The reference density used throughout the system. 87 */ 88 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; 89 90 /** 91 * Scaling factor to convert a density in DPI units to the density scale. 92 * @hide 93 */ 94 public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT; 95 96 /** 97 * The device's density. 98 * @hide because eventually this should be able to change while 99 * running, so shouldn't be a constant. 100 * @deprecated There is no longer a static density; you can find the 101 * density for a display in {@link #densityDpi}. 102 */ 103 @Deprecated 104 public static int DENSITY_DEVICE = getDeviceDensity(); 105 106 /** 107 * The absolute width of the display in pixels. 108 */ 109 public int widthPixels; 110 /** 111 * The absolute height of the display in pixels. 112 */ 113 public int heightPixels; 114 /** 115 * The logical density of the display. This is a scaling factor for the 116 * Density Independent Pixel unit, where one DIP is one pixel on an 117 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 118 * providing the baseline of the system's display. Thus on a 160dpi screen 119 * this density value will be 1; on a 120 dpi screen it would be .75; etc. 120 * 121 * <p>This value does not exactly follow the real screen size (as given by 122 * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of 123 * the overall UI in steps based on gross changes in the display dpi. For 124 * example, a 240x320 screen will have a density of 1 even if its width is 125 * 1.8", 1.3", etc. However, if the screen resolution is increased to 126 * 320x480 but the screen size remained 1.5"x2" then the density would be 127 * increased (probably to 1.5). 128 * 129 * @see #DENSITY_DEFAULT 130 */ 131 public float density; 132 /** 133 * The screen density expressed as dots-per-inch. May be either 134 * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. 135 */ 136 public int densityDpi; 137 /** 138 * A scaling factor for fonts displayed on the display. This is the same 139 * as {@link #density}, except that it may be adjusted in smaller 140 * increments at runtime based on a user preference for the font size. 141 */ 142 public float scaledDensity; 143 /** 144 * The exact physical pixels per inch of the screen in the X dimension. 145 */ 146 public float xdpi; 147 /** 148 * The exact physical pixels per inch of the screen in the Y dimension. 149 */ 150 public float ydpi; 151 152 /** 153 * The reported display width prior to any compatibility mode scaling 154 * being applied. 155 * @hide 156 */ 157 public int noncompatWidthPixels; 158 /** 159 * The reported display height prior to any compatibility mode scaling 160 * being applied. 161 * @hide 162 */ 163 public int noncompatHeightPixels; 164 /** 165 * The reported display density prior to any compatibility mode scaling 166 * being applied. 167 * @hide 168 */ 169 public float noncompatDensity; 170 /** 171 * The reported display density prior to any compatibility mode scaling 172 * being applied. 173 * @hide 174 */ 175 public int noncompatDensityDpi; 176 /** 177 * The reported scaled density prior to any compatibility mode scaling 178 * being applied. 179 * @hide 180 */ 181 public float noncompatScaledDensity; 182 /** 183 * The reported display xdpi prior to any compatibility mode scaling 184 * being applied. 185 * @hide 186 */ 187 public float noncompatXdpi; 188 /** 189 * The reported display ydpi prior to any compatibility mode scaling 190 * being applied. 191 * @hide 192 */ 193 public float noncompatYdpi; 194 DisplayMetrics()195 public DisplayMetrics() { 196 } 197 setTo(DisplayMetrics o)198 public void setTo(DisplayMetrics o) { 199 widthPixels = o.widthPixels; 200 heightPixels = o.heightPixels; 201 density = o.density; 202 densityDpi = o.densityDpi; 203 scaledDensity = o.scaledDensity; 204 xdpi = o.xdpi; 205 ydpi = o.ydpi; 206 noncompatWidthPixels = o.noncompatWidthPixels; 207 noncompatHeightPixels = o.noncompatHeightPixels; 208 noncompatDensity = o.noncompatDensity; 209 noncompatDensityDpi = o.noncompatDensityDpi; 210 noncompatScaledDensity = o.noncompatScaledDensity; 211 noncompatXdpi = o.noncompatXdpi; 212 noncompatYdpi = o.noncompatYdpi; 213 } 214 setToDefaults()215 public void setToDefaults() { 216 widthPixels = 0; 217 heightPixels = 0; 218 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; 219 densityDpi = DENSITY_DEVICE; 220 scaledDensity = density; 221 xdpi = DENSITY_DEVICE; 222 ydpi = DENSITY_DEVICE; 223 noncompatWidthPixels = widthPixels; 224 noncompatHeightPixels = heightPixels; 225 noncompatDensity = density; 226 noncompatDensityDpi = densityDpi; 227 noncompatScaledDensity = scaledDensity; 228 noncompatXdpi = xdpi; 229 noncompatYdpi = ydpi; 230 } 231 232 @Override equals(Object o)233 public boolean equals(Object o) { 234 return o instanceof DisplayMetrics && equals((DisplayMetrics)o); 235 } 236 237 /** 238 * Returns true if these display metrics equal the other display metrics. 239 * 240 * @param other The display metrics with which to compare. 241 * @return True if the display metrics are equal. 242 */ equals(DisplayMetrics other)243 public boolean equals(DisplayMetrics other) { 244 return equalsPhysical(other) 245 && scaledDensity == other.scaledDensity 246 && noncompatScaledDensity == other.noncompatScaledDensity; 247 } 248 249 /** 250 * Returns true if the physical aspects of the two display metrics 251 * are equal. This ignores the scaled density, which is a logical 252 * attribute based on the current desired font size. 253 * 254 * @param other The display metrics with which to compare. 255 * @return True if the display metrics are equal. 256 * @hide 257 */ equalsPhysical(DisplayMetrics other)258 public boolean equalsPhysical(DisplayMetrics other) { 259 return other != null 260 && widthPixels == other.widthPixels 261 && heightPixels == other.heightPixels 262 && density == other.density 263 && densityDpi == other.densityDpi 264 && xdpi == other.xdpi 265 && ydpi == other.ydpi 266 && noncompatWidthPixels == other.noncompatWidthPixels 267 && noncompatHeightPixels == other.noncompatHeightPixels 268 && noncompatDensity == other.noncompatDensity 269 && noncompatDensityDpi == other.noncompatDensityDpi 270 && noncompatXdpi == other.noncompatXdpi 271 && noncompatYdpi == other.noncompatYdpi; 272 } 273 274 @Override hashCode()275 public int hashCode() { 276 return widthPixels * heightPixels * densityDpi; 277 } 278 279 @Override toString()280 public String toString() { 281 return "DisplayMetrics{density=" + density + ", width=" + widthPixels + 282 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + 283 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; 284 } 285 getDeviceDensity()286 private static int getDeviceDensity() { 287 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density 288 // when running in the emulator, allowing for dynamic configurations. 289 // The reason for this is that ro.sf.lcd_density is write-once and is 290 // set by the init process when it parses build.prop before anything else. 291 return SystemProperties.getInt("qemu.sf.lcd_density", 292 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); 293 } 294 } 295