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