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 * Intermediate density for screens that sit somewhere between 71 * {@link #DENSITY_XHIGH} (320dpi) and {@link #DENSITY_XXHIGH} (480 dpi). 72 * This is not a density that applications should target, instead relying 73 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them. 74 */ 75 public static final int DENSITY_400 = 400; 76 77 /** 78 * Standard quantized DPI for extra-extra-high-density screens. Applications 79 * should not generally worry about this density; relying on XHIGH graphics 80 * being scaled up to it should be sufficient for almost all cases. 81 */ 82 public static final int DENSITY_XXHIGH = 480; 83 84 /** 85 * Standard quantized DPI for extra-extra-extra-high-density screens. Applications 86 * should not generally worry about this density; relying on XHIGH graphics 87 * being scaled up to it should be sufficient for almost all cases. A typical 88 * use of this density would be 4K television screens -- 3840x2160, which 89 * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH. 90 */ 91 public static final int DENSITY_XXXHIGH = 640; 92 93 /** 94 * The reference density used throughout the system. 95 */ 96 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; 97 98 /** 99 * Scaling factor to convert a density in DPI units to the density scale. 100 * @hide 101 */ 102 public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT; 103 104 /** 105 * The device's density. 106 * @hide because eventually this should be able to change while 107 * running, so shouldn't be a constant. 108 * @deprecated There is no longer a static density; you can find the 109 * density for a display in {@link #densityDpi}. 110 */ 111 @Deprecated 112 public static int DENSITY_DEVICE = getDeviceDensity(); 113 114 /** 115 * The absolute width of the display in pixels. 116 */ 117 public int widthPixels; 118 /** 119 * The absolute height of the display in pixels. 120 */ 121 public int heightPixels; 122 /** 123 * The logical density of the display. This is a scaling factor for the 124 * Density Independent Pixel unit, where one DIP is one pixel on an 125 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 126 * providing the baseline of the system's display. Thus on a 160dpi screen 127 * this density value will be 1; on a 120 dpi screen it would be .75; etc. 128 * 129 * <p>This value does not exactly follow the real screen size (as given by 130 * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of 131 * the overall UI in steps based on gross changes in the display dpi. For 132 * example, a 240x320 screen will have a density of 1 even if its width is 133 * 1.8", 1.3", etc. However, if the screen resolution is increased to 134 * 320x480 but the screen size remained 1.5"x2" then the density would be 135 * increased (probably to 1.5). 136 * 137 * @see #DENSITY_DEFAULT 138 */ 139 public float density; 140 /** 141 * The screen density expressed as dots-per-inch. May be either 142 * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. 143 */ 144 public int densityDpi; 145 /** 146 * A scaling factor for fonts displayed on the display. This is the same 147 * as {@link #density}, except that it may be adjusted in smaller 148 * increments at runtime based on a user preference for the font size. 149 */ 150 public float scaledDensity; 151 /** 152 * The exact physical pixels per inch of the screen in the X dimension. 153 */ 154 public float xdpi; 155 /** 156 * The exact physical pixels per inch of the screen in the Y dimension. 157 */ 158 public float ydpi; 159 160 /** 161 * The reported display width prior to any compatibility mode scaling 162 * being applied. 163 * @hide 164 */ 165 public int noncompatWidthPixels; 166 /** 167 * The reported display height prior to any compatibility mode scaling 168 * being applied. 169 * @hide 170 */ 171 public int noncompatHeightPixels; 172 /** 173 * The reported display density prior to any compatibility mode scaling 174 * being applied. 175 * @hide 176 */ 177 public float noncompatDensity; 178 /** 179 * The reported display density prior to any compatibility mode scaling 180 * being applied. 181 * @hide 182 */ 183 public int noncompatDensityDpi; 184 /** 185 * The reported scaled density prior to any compatibility mode scaling 186 * being applied. 187 * @hide 188 */ 189 public float noncompatScaledDensity; 190 /** 191 * The reported display xdpi prior to any compatibility mode scaling 192 * being applied. 193 * @hide 194 */ 195 public float noncompatXdpi; 196 /** 197 * The reported display ydpi prior to any compatibility mode scaling 198 * being applied. 199 * @hide 200 */ 201 public float noncompatYdpi; 202 203 public DisplayMetrics() { 204 } 205 206 public void setTo(DisplayMetrics o) { 207 widthPixels = o.widthPixels; 208 heightPixels = o.heightPixels; 209 density = o.density; 210 densityDpi = o.densityDpi; 211 scaledDensity = o.scaledDensity; 212 xdpi = o.xdpi; 213 ydpi = o.ydpi; 214 noncompatWidthPixels = o.noncompatWidthPixels; 215 noncompatHeightPixels = o.noncompatHeightPixels; 216 noncompatDensity = o.noncompatDensity; 217 noncompatDensityDpi = o.noncompatDensityDpi; 218 noncompatScaledDensity = o.noncompatScaledDensity; 219 noncompatXdpi = o.noncompatXdpi; 220 noncompatYdpi = o.noncompatYdpi; 221 } 222 223 public void setToDefaults() { 224 widthPixels = 0; 225 heightPixels = 0; 226 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; 227 densityDpi = DENSITY_DEVICE; 228 scaledDensity = density; 229 xdpi = DENSITY_DEVICE; 230 ydpi = DENSITY_DEVICE; 231 noncompatWidthPixels = widthPixels; 232 noncompatHeightPixels = heightPixels; 233 noncompatDensity = density; 234 noncompatDensityDpi = densityDpi; 235 noncompatScaledDensity = scaledDensity; 236 noncompatXdpi = xdpi; 237 noncompatYdpi = ydpi; 238 } 239 240 @Override 241 public boolean equals(Object o) { 242 return o instanceof DisplayMetrics && equals((DisplayMetrics)o); 243 } 244 245 /** 246 * Returns true if these display metrics equal the other display metrics. 247 * 248 * @param other The display metrics with which to compare. 249 * @return True if the display metrics are equal. 250 */ 251 public boolean equals(DisplayMetrics other) { 252 return equalsPhysical(other) 253 && scaledDensity == other.scaledDensity 254 && noncompatScaledDensity == other.noncompatScaledDensity; 255 } 256 257 /** 258 * Returns true if the physical aspects of the two display metrics 259 * are equal. This ignores the scaled density, which is a logical 260 * attribute based on the current desired font size. 261 * 262 * @param other The display metrics with which to compare. 263 * @return True if the display metrics are equal. 264 * @hide 265 */ 266 public boolean equalsPhysical(DisplayMetrics other) { 267 return other != null 268 && widthPixels == other.widthPixels 269 && heightPixels == other.heightPixels 270 && density == other.density 271 && densityDpi == other.densityDpi 272 && xdpi == other.xdpi 273 && ydpi == other.ydpi 274 && noncompatWidthPixels == other.noncompatWidthPixels 275 && noncompatHeightPixels == other.noncompatHeightPixels 276 && noncompatDensity == other.noncompatDensity 277 && noncompatDensityDpi == other.noncompatDensityDpi 278 && noncompatXdpi == other.noncompatXdpi 279 && noncompatYdpi == other.noncompatYdpi; 280 } 281 282 @Override 283 public int hashCode() { 284 return widthPixels * heightPixels * densityDpi; 285 } 286 287 @Override 288 public String toString() { 289 return "DisplayMetrics{density=" + density + ", width=" + widthPixels + 290 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + 291 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; 292 } 293 294 private static int getDeviceDensity() { 295 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density 296 // when running in the emulator, allowing for dynamic configurations. 297 // The reason for this is that ro.sf.lcd_density is write-once and is 298 // set by the init process when it parses build.prop before anything else. 299 return SystemProperties.getInt("qemu.sf.lcd_density", 300 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); 301 } 302 } 303