• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.UnsupportedAppUsage;
20 import android.os.SystemProperties;
21 
22 
23 /**
24  * A structure describing general information about a display, such as its
25  * size, density, and font scaling.
26  * <p>To access the DisplayMetrics members, initialize an object like this:</p>
27  * <pre> DisplayMetrics metrics = new DisplayMetrics();
28  * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
29  */
30 public class DisplayMetrics {
31     /**
32      * Standard quantized DPI for low-density screens.
33      */
34     public static final int DENSITY_LOW = 120;
35 
36     /**
37      * Intermediate density for screens that sit between {@link #DENSITY_LOW} (120dpi) and
38      * {@link #DENSITY_MEDIUM} (160dpi). This is not a density that applications should target,
39      * instead relying on the system to scale their {@link #DENSITY_MEDIUM} assets for them.
40      */
41     public static final int DENSITY_140 = 140;
42 
43     /**
44      * Standard quantized DPI for medium-density screens.
45      */
46     public static final int DENSITY_MEDIUM = 160;
47 
48     /**
49      * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
50      * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
51      * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
52      */
53     public static final int DENSITY_180 = 180;
54 
55     /**
56      * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
57      * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
58      * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
59      */
60     public static final int DENSITY_200 = 200;
61 
62     /**
63      * This is a secondary density, added for some common screen configurations.
64      * It is recommended that applications not generally target this as a first
65      * class density -- that is, don't supply specific graphics for this
66      * density, instead allow the platform to scale from other densities
67      * (typically {@link #DENSITY_HIGH}) as
68      * appropriate.  In most cases (such as using bitmaps in
69      * {@link android.graphics.drawable.Drawable}) the platform
70      * can perform this scaling at load time, so the only cost is some slight
71      * startup runtime overhead.
72      *
73      * <p>This density was original introduced to correspond with a
74      * 720p TV screen: the density for 1080p televisions is
75      * {@link #DENSITY_XHIGH}, and the value here provides the same UI
76      * size for a TV running at 720p.  It has also found use in 7" tablets,
77      * when these devices have 1280x720 displays.
78      */
79     public static final int DENSITY_TV = 213;
80 
81     /**
82      * Intermediate density for screens that sit between {@link #DENSITY_MEDIUM} (160dpi) and
83      * {@link #DENSITY_HIGH} (240dpi). This is not a density that applications should target,
84      * instead relying on the system to scale their {@link #DENSITY_HIGH} assets for them.
85      */
86     public static final int DENSITY_220 = 220;
87 
88     /**
89      * Standard quantized DPI for high-density screens.
90      */
91     public static final int DENSITY_HIGH = 240;
92 
93     /**
94      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
95      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
96      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
97      */
98     public static final int DENSITY_260 = 260;
99 
100     /**
101      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
102      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
103      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
104      */
105     public static final int DENSITY_280 = 280;
106 
107     /**
108      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
109      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
110      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
111      */
112     public static final int DENSITY_300 = 300;
113 
114     /**
115      * Standard quantized DPI for extra-high-density screens.
116      */
117     public static final int DENSITY_XHIGH = 320;
118 
119     /**
120      * Intermediate density for screens that sit somewhere between
121      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
122      * This is not a density that applications should target, instead relying
123      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
124      */
125     public static final int DENSITY_340 = 340;
126 
127     /**
128      * Intermediate density for screens that sit somewhere between
129      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
130      * This is not a density that applications should target, instead relying
131      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
132      */
133     public static final int DENSITY_360 = 360;
134 
135     /**
136      * Intermediate density for screens that sit somewhere between
137      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
138      * This is not a density that applications should target, instead relying
139      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
140      */
141     public static final int DENSITY_400 = 400;
142 
143     /**
144      * Intermediate density for screens that sit somewhere between
145      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
146      * This is not a density that applications should target, instead relying
147      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
148      */
149     public static final int DENSITY_420 = 420;
150 
151     /**
152      * Intermediate density for screens that sit somewhere between
153      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
154      * This is not a density that applications should target, instead relying
155      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
156      */
157     public static final int DENSITY_440 = 440;
158 
159     /**
160      * Intermediate density for screens that sit somewhere between
161      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
162      * This is not a density that applications should target, instead relying
163      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
164      */
165     public static final int DENSITY_450 = 450;
166 
167     /**
168      * Standard quantized DPI for extra-extra-high-density screens.
169      */
170     public static final int DENSITY_XXHIGH = 480;
171 
172     /**
173      * Intermediate density for screens that sit somewhere between
174      * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
175      * This is not a density that applications should target, instead relying
176      * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
177      */
178     public static final int DENSITY_560 = 560;
179 
180     /**
181      * Intermediate density for screens that sit somewhere between
182      * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
183      * This is not a density that applications should target, instead relying
184      * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
185      */
186     public static final int DENSITY_600 = 600;
187 
188     /**
189      * Standard quantized DPI for extra-extra-extra-high-density screens.  Applications
190      * should not generally worry about this density; relying on XHIGH graphics
191      * being scaled up to it should be sufficient for almost all cases.  A typical
192      * use of this density would be 4K television screens -- 3840x2160, which
193      * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
194      */
195     public static final int DENSITY_XXXHIGH = 640;
196 
197     /**
198      * The reference density used throughout the system.
199      */
200     public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
201 
202     /**
203      * Scaling factor to convert a density in DPI units to the density scale.
204      * @hide
205      */
206     public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
207 
208     /**
209      * The device's current density.
210      * <p>
211      * This value reflects any changes made to the device density. To obtain
212      * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
213      *
214      * @hide This value should not be used.
215      * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
216      *             device density or {@link #densityDpi} to obtain the current
217      *             density for a specific display.
218      */
219     @Deprecated
220     @UnsupportedAppUsage
221     public static int DENSITY_DEVICE = getDeviceDensity();
222 
223     /**
224      * The device's stable density.
225      * <p>
226      * This value is constant at run time and may not reflect the current
227      * display density. To obtain the current density for a specific display,
228      * use {@link #densityDpi}.
229      */
230     public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
231 
232     /**
233      * The absolute width of the available display size in pixels.
234      */
235     public int widthPixels;
236     /**
237      * The absolute height of the available display size in pixels.
238      */
239     public int heightPixels;
240     /**
241      * The logical density of the display.  This is a scaling factor for the
242      * Density Independent Pixel unit, where one DIP is one pixel on an
243      * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
244      * providing the baseline of the system's display. Thus on a 160dpi screen
245      * this density value will be 1; on a 120 dpi screen it would be .75; etc.
246      *
247      * <p>This value does not exactly follow the real screen size (as given by
248      * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
249      * the overall UI in steps based on gross changes in the display dpi.  For
250      * example, a 240x320 screen will have a density of 1 even if its width is
251      * 1.8", 1.3", etc. However, if the screen resolution is increased to
252      * 320x480 but the screen size remained 1.5"x2" then the density would be
253      * increased (probably to 1.5).
254      *
255      * @see #DENSITY_DEFAULT
256      */
257     public float density;
258     /**
259      * The screen density expressed as dots-per-inch.  May be either
260      * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
261      */
262     public int densityDpi;
263     /**
264      * A scaling factor for fonts displayed on the display.  This is the same
265      * as {@link #density}, except that it may be adjusted in smaller
266      * increments at runtime based on a user preference for the font size.
267      */
268     public float scaledDensity;
269     /**
270      * The exact physical pixels per inch of the screen in the X dimension.
271      */
272     public float xdpi;
273     /**
274      * The exact physical pixels per inch of the screen in the Y dimension.
275      */
276     public float ydpi;
277 
278     /**
279      * The reported display width prior to any compatibility mode scaling
280      * being applied.
281      * @hide
282      */
283     @UnsupportedAppUsage
284     public int noncompatWidthPixels;
285     /**
286      * The reported display height prior to any compatibility mode scaling
287      * being applied.
288      * @hide
289      */
290     @UnsupportedAppUsage
291     public int noncompatHeightPixels;
292     /**
293      * The reported display density prior to any compatibility mode scaling
294      * being applied.
295      * @hide
296      */
297     public float noncompatDensity;
298     /**
299      * The reported display density prior to any compatibility mode scaling
300      * being applied.
301      * @hide
302      */
303     @UnsupportedAppUsage
304     public int noncompatDensityDpi;
305     /**
306      * The reported scaled density prior to any compatibility mode scaling
307      * being applied.
308      * @hide
309      */
310     public float noncompatScaledDensity;
311     /**
312      * The reported display xdpi prior to any compatibility mode scaling
313      * being applied.
314      * @hide
315      */
316     public float noncompatXdpi;
317     /**
318      * The reported display ydpi prior to any compatibility mode scaling
319      * being applied.
320      * @hide
321      */
322     public float noncompatYdpi;
323 
DisplayMetrics()324     public DisplayMetrics() {
325     }
326 
setTo(DisplayMetrics o)327     public void setTo(DisplayMetrics o) {
328         if (this == o) {
329             return;
330         }
331 
332         widthPixels = o.widthPixels;
333         heightPixels = o.heightPixels;
334         density = o.density;
335         densityDpi = o.densityDpi;
336         scaledDensity = o.scaledDensity;
337         xdpi = o.xdpi;
338         ydpi = o.ydpi;
339         noncompatWidthPixels = o.noncompatWidthPixels;
340         noncompatHeightPixels = o.noncompatHeightPixels;
341         noncompatDensity = o.noncompatDensity;
342         noncompatDensityDpi = o.noncompatDensityDpi;
343         noncompatScaledDensity = o.noncompatScaledDensity;
344         noncompatXdpi = o.noncompatXdpi;
345         noncompatYdpi = o.noncompatYdpi;
346     }
347 
setToDefaults()348     public void setToDefaults() {
349         widthPixels = 0;
350         heightPixels = 0;
351         density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
352         densityDpi =  DENSITY_DEVICE;
353         scaledDensity = density;
354         xdpi = DENSITY_DEVICE;
355         ydpi = DENSITY_DEVICE;
356         noncompatWidthPixels = widthPixels;
357         noncompatHeightPixels = heightPixels;
358         noncompatDensity = density;
359         noncompatDensityDpi = densityDpi;
360         noncompatScaledDensity = scaledDensity;
361         noncompatXdpi = xdpi;
362         noncompatYdpi = ydpi;
363     }
364 
365     @Override
equals(Object o)366     public boolean equals(Object o) {
367         return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
368     }
369 
370     /**
371      * Returns true if these display metrics equal the other display metrics.
372      *
373      * @param other The display metrics with which to compare.
374      * @return True if the display metrics are equal.
375      */
equals(DisplayMetrics other)376     public boolean equals(DisplayMetrics other) {
377         return equalsPhysical(other)
378                 && scaledDensity == other.scaledDensity
379                 && noncompatScaledDensity == other.noncompatScaledDensity;
380     }
381 
382     /**
383      * Returns true if the physical aspects of the two display metrics
384      * are equal.  This ignores the scaled density, which is a logical
385      * attribute based on the current desired font size.
386      *
387      * @param other The display metrics with which to compare.
388      * @return True if the display metrics are equal.
389      * @hide
390      */
equalsPhysical(DisplayMetrics other)391     public boolean equalsPhysical(DisplayMetrics other) {
392         return other != null
393                 && widthPixels == other.widthPixels
394                 && heightPixels == other.heightPixels
395                 && density == other.density
396                 && densityDpi == other.densityDpi
397                 && xdpi == other.xdpi
398                 && ydpi == other.ydpi
399                 && noncompatWidthPixels == other.noncompatWidthPixels
400                 && noncompatHeightPixels == other.noncompatHeightPixels
401                 && noncompatDensity == other.noncompatDensity
402                 && noncompatDensityDpi == other.noncompatDensityDpi
403                 && noncompatXdpi == other.noncompatXdpi
404                 && noncompatYdpi == other.noncompatYdpi;
405     }
406 
407     @Override
hashCode()408     public int hashCode() {
409         return widthPixels * heightPixels * densityDpi;
410     }
411 
412     @Override
toString()413     public String toString() {
414         return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
415             ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
416             ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
417     }
418 
getDeviceDensity()419     private static int getDeviceDensity() {
420         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
421         // when running in the emulator, allowing for dynamic configurations.
422         // The reason for this is that ro.sf.lcd_density is write-once and is
423         // set by the init process when it parses build.prop before anything else.
424         return SystemProperties.getInt("qemu.sf.lcd_density",
425                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
426     }
427 }
428