blob: 451a669d98fddfd46b17c6676fb321461c14cd1a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.util;
18
Mathew Inwoodb4075682018-08-14 17:32:44 +010019import android.annotation.UnsupportedAppUsage;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040020import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22
23/**
24 * A structure describing general information about a display, such as its
25 * size, density, and font scaling.
Joshua Baxter6b5e85d2019-07-18 20:45:53 +000026 * <p>To access the DisplayMetrics members, retrieve display metrics like this:</p>
27 * <pre>context.getResources().getDisplayMetrics();</pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028 */
29public class DisplayMetrics {
30 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070031 * Standard quantized DPI for low-density screens.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 */
Dianne Hackborna53b8282009-07-17 11:13:48 -070033 public static final int DENSITY_LOW = 120;
34
35 /**
Harry Slatyere4eb7b42019-04-09 08:58:02 +100036 * Intermediate density for screens that sit between {@link #DENSITY_LOW} (120dpi) and
37 * {@link #DENSITY_MEDIUM} (160dpi). This is not a density that applications should target,
38 * instead relying on the system to scale their {@link #DENSITY_MEDIUM} assets for them.
39 */
40 public static final int DENSITY_140 = 140;
41
42 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070043 * Standard quantized DPI for medium-density screens.
44 */
45 public static final int DENSITY_MEDIUM = 160;
46
47 /**
Harry Slatyere4eb7b42019-04-09 08:58:02 +100048 * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
49 * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
50 * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
51 */
52 public static final int DENSITY_180 = 180;
53
54 /**
55 * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
56 * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
57 * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
58 */
59 public static final int DENSITY_200 = 200;
60
61 /**
Dianne Hackborn493861d2012-06-18 19:29:45 -070062 * This is a secondary density, added for some common screen configurations.
63 * It is recommended that applications not generally target this as a first
64 * class density -- that is, don't supply specific graphics for this
65 * density, instead allow the platform to scale from other densities
66 * (typically {@link #DENSITY_HIGH}) as
67 * appropriate. In most cases (such as using bitmaps in
68 * {@link android.graphics.drawable.Drawable}) the platform
69 * can perform this scaling at load time, so the only cost is some slight
70 * startup runtime overhead.
71 *
72 * <p>This density was original introduced to correspond with a
73 * 720p TV screen: the density for 1080p televisions is
74 * {@link #DENSITY_XHIGH}, and the value here provides the same UI
75 * size for a TV running at 720p. It has also found use in 7" tablets,
76 * when these devices have 1280x720 displays.
Dianne Hackbornb96cbbd2011-05-27 13:40:26 -070077 */
78 public static final int DENSITY_TV = 213;
79
80 /**
Harry Slatyere4eb7b42019-04-09 08:58:02 +100081 * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
82 * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
83 * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
84 */
85 public static final int DENSITY_220 = 220;
86
87 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070088 * Standard quantized DPI for high-density screens.
89 */
90 public static final int DENSITY_HIGH = 240;
91
92 /**
Adam Powellf6d40452015-02-11 13:34:43 -080093 * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
94 * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
95 * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
96 */
Erik Wolsheimer178bba42016-06-13 12:51:36 -070097 public static final int DENSITY_260 = 260;
98
99 /**
100 * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
101 * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
102 * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
103 */
Adam Powellf6d40452015-02-11 13:34:43 -0800104 public static final int DENSITY_280 = 280;
105
106 /**
Erik Wolsheimer178bba42016-06-13 12:51:36 -0700107 * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
108 * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
109 * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
110 */
111 public static final int DENSITY_300 = 300;
112
113 /**
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700114 * Standard quantized DPI for extra-high-density screens.
115 */
116 public static final int DENSITY_XHIGH = 320;
117
118 /**
Dianne Hackborn09acb7c2013-10-17 17:28:09 -0700119 * Intermediate density for screens that sit somewhere between
Dianne Hackborna3fb40d2014-08-12 15:06:50 -0700120 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
Dianne Hackborn09acb7c2013-10-17 17:28:09 -0700121 * This is not a density that applications should target, instead relying
122 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
123 */
Erik Wolsheimer178bba42016-06-13 12:51:36 -0700124 public static final int DENSITY_340 = 340;
125
126 /**
127 * Intermediate density for screens that sit somewhere between
128 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
129 * This is not a density that applications should target, instead relying
130 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
131 */
Adam Powellf6d9cd192015-06-03 15:50:48 -0700132 public static final int DENSITY_360 = 360;
133
134 /**
135 * Intermediate density for screens that sit somewhere between
136 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
137 * This is not a density that applications should target, instead relying
138 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
139 */
Dianne Hackborn09acb7c2013-10-17 17:28:09 -0700140 public static final int DENSITY_400 = 400;
141
142 /**
Dianne Hackborn1e9c51a2015-08-11 17:18:53 -0700143 * Intermediate density for screens that sit somewhere between
144 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
145 * This is not a density that applications should target, instead relying
146 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
147 */
148 public static final int DENSITY_420 = 420;
149
150 /**
weijuncheng5185d712018-01-26 13:05:40 +0800151 * Intermediate density for screens that sit somewhere between
152 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
153 * This is not a density that applications should target, instead relying
154 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
155 */
156 public static final int DENSITY_440 = 440;
157
158 /**
bj1211jeonb38f1682019-06-11 14:52:28 +0900159 * Intermediate density for screens that sit somewhere between
160 * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
161 * This is not a density that applications should target, instead relying
162 * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
163 */
164 public static final int DENSITY_450 = 450;
165
166 /**
Jeff Davidson61a5a3f2014-07-16 12:56:26 -0700167 * Standard quantized DPI for extra-extra-high-density screens.
Dianne Hackbornd96e3df2012-01-25 15:12:23 -0800168 */
169 public static final int DENSITY_XXHIGH = 480;
170
171 /**
Dianne Hackborna3fb40d2014-08-12 15:06:50 -0700172 * Intermediate density for screens that sit somewhere between
Philip Nowelld5619132014-09-25 15:40:56 -0700173 * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
Dianne Hackborna3fb40d2014-08-12 15:06:50 -0700174 * This is not a density that applications should target, instead relying
175 * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
176 */
177 public static final int DENSITY_560 = 560;
178
179 /**
st.jid41b9ac2018-07-18 18:15:24 +0900180 * Intermediate density for screens that sit somewhere between
181 * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
182 * This is not a density that applications should target, instead relying
183 * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
184 */
185 public static final int DENSITY_600 = 600;
186
187 /**
Dianne Hackborn56a23012013-02-12 15:41:49 -0800188 * Standard quantized DPI for extra-extra-extra-high-density screens. Applications
189 * should not generally worry about this density; relying on XHIGH graphics
190 * being scaled up to it should be sufficient for almost all cases. A typical
191 * use of this density would be 4K television screens -- 3840x2160, which
192 * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
193 */
194 public static final int DENSITY_XXXHIGH = 640;
195
196 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -0700197 * The reference density used throughout the system.
198 */
199 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -0700201 /**
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700202 * Scaling factor to convert a density in DPI units to the density scale.
203 * @hide
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -0700204 */
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700205 public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
206
207 /**
Alan Viveretted7bbc8a2016-01-29 15:40:38 -0500208 * The device's current density.
209 * <p>
210 * This value reflects any changes made to the device density. To obtain
211 * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
212 *
213 * @hide This value should not be used.
214 * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
215 * device density or {@link #densityDpi} to obtain the current
216 * density for a specific display.
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700217 */
218 @Deprecated
Mathew Inwoodb4075682018-08-14 17:32:44 +0100219 @UnsupportedAppUsage
Dianne Hackborndde331c2012-08-03 14:01:57 -0700220 public static int DENSITY_DEVICE = getDeviceDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 /**
Alan Viveretted7bbc8a2016-01-29 15:40:38 -0500223 * The device's stable density.
224 * <p>
225 * This value is constant at run time and may not reflect the current
226 * display density. To obtain the current density for a specific display,
227 * use {@link #densityDpi}.
228 */
229 public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
230
231 /**
Andrii Kulian2beb8282016-03-10 22:08:14 -0800232 * The absolute width of the available display size in pixels.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 */
234 public int widthPixels;
235 /**
Andrii Kulian2beb8282016-03-10 22:08:14 -0800236 * The absolute height of the available display size in pixels.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public int heightPixels;
239 /**
240 * The logical density of the display. This is a scaling factor for the
241 * Density Independent Pixel unit, where one DIP is one pixel on an
242 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
243 * providing the baseline of the system's display. Thus on a 160dpi screen
Dirk Dougherty6b13bc02009-10-30 19:05:53 -0700244 * this density value will be 1; on a 120 dpi screen it would be .75; etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 *
246 * <p>This value does not exactly follow the real screen size (as given by
Joshua Baxter9c69b402019-07-18 21:16:19 +0000247 * {@link #xdpi} and {@link #ydpi}), but rather is used to scale the size of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * the overall UI in steps based on gross changes in the display dpi. For
249 * example, a 240x320 screen will have a density of 1 even if its width is
250 * 1.8", 1.3", etc. However, if the screen resolution is increased to
251 * 320x480 but the screen size remained 1.5"x2" then the density would be
252 * increased (probably to 1.5).
253 *
Dianne Hackborna53b8282009-07-17 11:13:48 -0700254 * @see #DENSITY_DEFAULT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 */
256 public float density;
257 /**
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700258 * The screen density expressed as dots-per-inch. May be either
259 * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
260 */
261 public int densityDpi;
262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * A scaling factor for fonts displayed on the display. This is the same
264 * as {@link #density}, except that it may be adjusted in smaller
265 * increments at runtime based on a user preference for the font size.
266 */
267 public float scaledDensity;
268 /**
269 * The exact physical pixels per inch of the screen in the X dimension.
270 */
271 public float xdpi;
272 /**
273 * The exact physical pixels per inch of the screen in the Y dimension.
274 */
275 public float ydpi;
276
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700277 /**
278 * The reported display width prior to any compatibility mode scaling
279 * being applied.
280 * @hide
281 */
Mathew Inwoodb4075682018-08-14 17:32:44 +0100282 @UnsupportedAppUsage
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700283 public int noncompatWidthPixels;
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700284 /**
285 * The reported display height prior to any compatibility mode scaling
286 * being applied.
287 * @hide
288 */
Mathew Inwoodb4075682018-08-14 17:32:44 +0100289 @UnsupportedAppUsage
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700290 public int noncompatHeightPixels;
291 /**
292 * The reported display density prior to any compatibility mode scaling
293 * being applied.
294 * @hide
295 */
296 public float noncompatDensity;
297 /**
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700298 * The reported display density prior to any compatibility mode scaling
299 * being applied.
300 * @hide
301 */
Mathew Inwoodb4075682018-08-14 17:32:44 +0100302 @UnsupportedAppUsage
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700303 public int noncompatDensityDpi;
304 /**
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700305 * The reported scaled density prior to any compatibility mode scaling
306 * being applied.
307 * @hide
308 */
309 public float noncompatScaledDensity;
310 /**
311 * The reported display xdpi prior to any compatibility mode scaling
312 * being applied.
313 * @hide
314 */
315 public float noncompatXdpi;
316 /**
317 * The reported display ydpi prior to any compatibility mode scaling
318 * being applied.
319 * @hide
320 */
321 public float noncompatYdpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 public DisplayMetrics() {
324 }
325
326 public void setTo(DisplayMetrics o) {
Adam Lesinski43097212016-10-14 11:10:29 -0700327 if (this == o) {
328 return;
329 }
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 widthPixels = o.widthPixels;
332 heightPixels = o.heightPixels;
333 density = o.density;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700334 densityDpi = o.densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 scaledDensity = o.scaledDensity;
336 xdpi = o.xdpi;
337 ydpi = o.ydpi;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700338 noncompatWidthPixels = o.noncompatWidthPixels;
339 noncompatHeightPixels = o.noncompatHeightPixels;
340 noncompatDensity = o.noncompatDensity;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700341 noncompatDensityDpi = o.noncompatDensityDpi;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700342 noncompatScaledDensity = o.noncompatScaledDensity;
343 noncompatXdpi = o.noncompatXdpi;
344 noncompatYdpi = o.noncompatYdpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 public void setToDefaults() {
348 widthPixels = 0;
349 heightPixels = 0;
Jeff Browna492c3a2012-08-23 19:48:44 -0700350 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
351 densityDpi = DENSITY_DEVICE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 scaledDensity = density;
Jeff Browna492c3a2012-08-23 19:48:44 -0700353 xdpi = DENSITY_DEVICE;
354 ydpi = DENSITY_DEVICE;
355 noncompatWidthPixels = widthPixels;
356 noncompatHeightPixels = heightPixels;
357 noncompatDensity = density;
358 noncompatDensityDpi = densityDpi;
359 noncompatScaledDensity = scaledDensity;
360 noncompatXdpi = xdpi;
361 noncompatYdpi = ydpi;
362 }
363
364 @Override
365 public boolean equals(Object o) {
366 return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
367 }
368
369 /**
370 * Returns true if these display metrics equal the other display metrics.
371 *
372 * @param other The display metrics with which to compare.
373 * @return True if the display metrics are equal.
374 */
375 public boolean equals(DisplayMetrics other) {
Dianne Hackborn7ac8bbd2012-11-29 11:59:58 -0800376 return equalsPhysical(other)
377 && scaledDensity == other.scaledDensity
378 && noncompatScaledDensity == other.noncompatScaledDensity;
379 }
380
381 /**
382 * Returns true if the physical aspects of the two display metrics
383 * are equal. This ignores the scaled density, which is a logical
384 * attribute based on the current desired font size.
385 *
386 * @param other The display metrics with which to compare.
387 * @return True if the display metrics are equal.
388 * @hide
389 */
390 public boolean equalsPhysical(DisplayMetrics other) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700391 return other != null
392 && widthPixels == other.widthPixels
393 && heightPixels == other.heightPixels
394 && density == other.density
395 && densityDpi == other.densityDpi
Jeff Browna492c3a2012-08-23 19:48:44 -0700396 && xdpi == other.xdpi
397 && ydpi == other.ydpi
398 && noncompatWidthPixels == other.noncompatWidthPixels
399 && noncompatHeightPixels == other.noncompatHeightPixels
400 && noncompatDensity == other.noncompatDensity
401 && noncompatDensityDpi == other.noncompatDensityDpi
Jeff Browna492c3a2012-08-23 19:48:44 -0700402 && noncompatXdpi == other.noncompatXdpi
403 && noncompatYdpi == other.noncompatYdpi;
404 }
405
406 @Override
407 public int hashCode() {
408 return widthPixels * heightPixels * densityDpi;
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700409 }
410
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700411 @Override
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700412 public String toString() {
413 return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
414 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
415 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
416 }
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200417
418 private static int getDeviceDensity() {
419 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
420 // when running in the emulator, allowing for dynamic configurations.
421 // The reason for this is that ro.sf.lcd_density is write-once and is
422 // set by the init process when it parses build.prop before anything else.
423 return SystemProperties.getInt("qemu.sf.lcd_density",
Dianne Hackborna53b8282009-07-17 11:13:48 -0700424 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426}