The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 19 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | |
| 22 | /** |
| 23 | * A structure describing general information about a display, such as its |
| 24 | * size, density, and font scaling. |
Scott Main | 71d4b28 | 2009-08-13 12:45:31 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class DisplayMetrics { |
| 30 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 31 | * Standard quantized DPI for low-density screens. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | */ |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | 493861d | 2012-06-18 19:29:45 -0700 | [diff] [blame] | 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. |
Dianne Hackborn | b96cbbd | 2011-05-27 13:40:26 -0700 | [diff] [blame] | 56 | */ |
| 57 | public static final int DENSITY_TV = 213; |
| 58 | |
| 59 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 60 | * Standard quantized DPI for high-density screens. |
| 61 | */ |
| 62 | public static final int DENSITY_HIGH = 240; |
| 63 | |
| 64 | /** |
Dianne Hackborn | a0b46c9 | 2010-10-21 15:32:06 -0700 | [diff] [blame] | 65 | * Standard quantized DPI for extra-high-density screens. |
| 66 | */ |
| 67 | public static final int DENSITY_XHIGH = 320; |
| 68 | |
| 69 | /** |
Dianne Hackborn | d96e3df | 2012-01-25 15:12:23 -0800 | [diff] [blame] | 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 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 77 | * The reference density used throughout the system. |
| 78 | */ |
| 79 | public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 81 | /** |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 82 | * Scaling factor to convert a density in DPI units to the density scale. |
| 83 | * @hide |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 84 | */ |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 85 | public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT; |
| 86 | |
| 87 | /** |
| 88 | * The device's density. |
| 89 | * @hide because eventually this should be able to change while |
| 90 | * running, so shouldn't be a constant. |
| 91 | * @deprecated There is no longer a static density; you can find the |
| 92 | * density for a display in {@link #densityDpi}. |
| 93 | */ |
| 94 | @Deprecated |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 95 | public static int DENSITY_DEVICE = getDeviceDensity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * The absolute width of the display in pixels. |
| 99 | */ |
| 100 | public int widthPixels; |
| 101 | /** |
| 102 | * The absolute height of the display in pixels. |
| 103 | */ |
| 104 | public int heightPixels; |
| 105 | /** |
| 106 | * The logical density of the display. This is a scaling factor for the |
| 107 | * Density Independent Pixel unit, where one DIP is one pixel on an |
| 108 | * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), |
| 109 | * providing the baseline of the system's display. Thus on a 160dpi screen |
Dirk Dougherty | 6b13bc0 | 2009-10-30 19:05:53 -0700 | [diff] [blame] | 110 | * this density value will be 1; on a 120 dpi screen it would be .75; etc. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * |
| 112 | * <p>This value does not exactly follow the real screen size (as given by |
| 113 | * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of |
| 114 | * the overall UI in steps based on gross changes in the display dpi. For |
| 115 | * example, a 240x320 screen will have a density of 1 even if its width is |
| 116 | * 1.8", 1.3", etc. However, if the screen resolution is increased to |
| 117 | * 320x480 but the screen size remained 1.5"x2" then the density would be |
| 118 | * increased (probably to 1.5). |
| 119 | * |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 120 | * @see #DENSITY_DEFAULT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | */ |
| 122 | public float density; |
| 123 | /** |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 124 | * The screen density expressed as dots-per-inch. May be either |
| 125 | * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. |
| 126 | */ |
| 127 | public int densityDpi; |
| 128 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | * A scaling factor for fonts displayed on the display. This is the same |
| 130 | * as {@link #density}, except that it may be adjusted in smaller |
| 131 | * increments at runtime based on a user preference for the font size. |
| 132 | */ |
| 133 | public float scaledDensity; |
| 134 | /** |
| 135 | * The exact physical pixels per inch of the screen in the X dimension. |
| 136 | */ |
| 137 | public float xdpi; |
| 138 | /** |
| 139 | * The exact physical pixels per inch of the screen in the Y dimension. |
| 140 | */ |
| 141 | public float ydpi; |
| 142 | |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 143 | /** |
| 144 | * The reported display width prior to any compatibility mode scaling |
| 145 | * being applied. |
| 146 | * @hide |
| 147 | */ |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 148 | public int noncompatWidthPixels; |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 149 | /** |
| 150 | * The reported display height prior to any compatibility mode scaling |
| 151 | * being applied. |
| 152 | * @hide |
| 153 | */ |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 154 | public int noncompatHeightPixels; |
| 155 | /** |
| 156 | * The reported display density prior to any compatibility mode scaling |
| 157 | * being applied. |
| 158 | * @hide |
| 159 | */ |
| 160 | public float noncompatDensity; |
| 161 | /** |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 162 | * The reported display density prior to any compatibility mode scaling |
| 163 | * being applied. |
| 164 | * @hide |
| 165 | */ |
| 166 | public int noncompatDensityDpi; |
| 167 | /** |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 168 | * The reported scaled density prior to any compatibility mode scaling |
| 169 | * being applied. |
| 170 | * @hide |
| 171 | */ |
| 172 | public float noncompatScaledDensity; |
| 173 | /** |
| 174 | * The reported display xdpi prior to any compatibility mode scaling |
| 175 | * being applied. |
| 176 | * @hide |
| 177 | */ |
| 178 | public float noncompatXdpi; |
| 179 | /** |
| 180 | * The reported display ydpi prior to any compatibility mode scaling |
| 181 | * being applied. |
| 182 | * @hide |
| 183 | */ |
| 184 | public float noncompatYdpi; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | public DisplayMetrics() { |
| 187 | } |
| 188 | |
| 189 | public void setTo(DisplayMetrics o) { |
| 190 | widthPixels = o.widthPixels; |
| 191 | heightPixels = o.heightPixels; |
| 192 | density = o.density; |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 193 | densityDpi = o.densityDpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | scaledDensity = o.scaledDensity; |
| 195 | xdpi = o.xdpi; |
| 196 | ydpi = o.ydpi; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 197 | noncompatWidthPixels = o.noncompatWidthPixels; |
| 198 | noncompatHeightPixels = o.noncompatHeightPixels; |
| 199 | noncompatDensity = o.noncompatDensity; |
Dianne Hackborn | 908aecc | 2012-07-31 16:37:34 -0700 | [diff] [blame] | 200 | noncompatDensityDpi = o.noncompatDensityDpi; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 201 | noncompatScaledDensity = o.noncompatScaledDensity; |
| 202 | noncompatXdpi = o.noncompatXdpi; |
| 203 | noncompatYdpi = o.noncompatYdpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | public void setToDefaults() { |
| 207 | widthPixels = 0; |
| 208 | heightPixels = 0; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 209 | density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; |
| 210 | densityDpi = DENSITY_DEVICE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | scaledDensity = density; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 212 | xdpi = DENSITY_DEVICE; |
| 213 | ydpi = DENSITY_DEVICE; |
| 214 | noncompatWidthPixels = widthPixels; |
| 215 | noncompatHeightPixels = heightPixels; |
| 216 | noncompatDensity = density; |
| 217 | noncompatDensityDpi = densityDpi; |
| 218 | noncompatScaledDensity = scaledDensity; |
| 219 | noncompatXdpi = xdpi; |
| 220 | noncompatYdpi = ydpi; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public boolean equals(Object o) { |
| 225 | return o instanceof DisplayMetrics && equals((DisplayMetrics)o); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns true if these display metrics equal the other display metrics. |
| 230 | * |
| 231 | * @param other The display metrics with which to compare. |
| 232 | * @return True if the display metrics are equal. |
| 233 | */ |
| 234 | public boolean equals(DisplayMetrics other) { |
Dianne Hackborn | 7ac8bbd | 2012-11-29 11:59:58 -0800 | [diff] [blame] | 235 | return equalsPhysical(other) |
| 236 | && scaledDensity == other.scaledDensity |
| 237 | && noncompatScaledDensity == other.noncompatScaledDensity; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns true if the physical aspects of the two display metrics |
| 242 | * are equal. This ignores the scaled density, which is a logical |
| 243 | * attribute based on the current desired font size. |
| 244 | * |
| 245 | * @param other The display metrics with which to compare. |
| 246 | * @return True if the display metrics are equal. |
| 247 | * @hide |
| 248 | */ |
| 249 | public boolean equalsPhysical(DisplayMetrics other) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 250 | return other != null |
| 251 | && widthPixels == other.widthPixels |
| 252 | && heightPixels == other.heightPixels |
| 253 | && density == other.density |
| 254 | && densityDpi == other.densityDpi |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 255 | && xdpi == other.xdpi |
| 256 | && ydpi == other.ydpi |
| 257 | && noncompatWidthPixels == other.noncompatWidthPixels |
| 258 | && noncompatHeightPixels == other.noncompatHeightPixels |
| 259 | && noncompatDensity == other.noncompatDensity |
| 260 | && noncompatDensityDpi == other.noncompatDensityDpi |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 261 | && noncompatXdpi == other.noncompatXdpi |
| 262 | && noncompatYdpi == other.noncompatYdpi; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public int hashCode() { |
| 267 | return widthPixels * heightPixels * densityDpi; |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 270 | @Override |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 271 | public String toString() { |
| 272 | return "DisplayMetrics{density=" + density + ", width=" + widthPixels + |
| 273 | ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + |
| 274 | ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; |
| 275 | } |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 276 | |
| 277 | private static int getDeviceDensity() { |
| 278 | // qemu.sf.lcd_density can be used to override ro.sf.lcd_density |
| 279 | // when running in the emulator, allowing for dynamic configurations. |
| 280 | // The reason for this is that ro.sf.lcd_density is write-once and is |
| 281 | // set by the init process when it parses build.prop before anything else. |
| 282 | return SystemProperties.getInt("qemu.sf.lcd_density", |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 283 | SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | } |