blob: e856501e54bbb1414d66c8fdecce547e195f3063 [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
Dianne Hackborne2515ee2011-04-27 18:52:56 -040019import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21
22/**
23 * A structure describing general information about a display, such as its
24 * size, density, and font scaling.
Scott Main71d4b282009-08-13 12:45:31 -070025 * <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 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 /**
36 * Standard quantized DPI for medium-density screens.
37 */
38 public static final int DENSITY_MEDIUM = 160;
39
40 /**
Dianne Hackborn493861d2012-06-18 19:29:45 -070041 * 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 Hackbornb96cbbd2011-05-27 13:40:26 -070056 */
57 public static final int DENSITY_TV = 213;
58
59 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070060 * Standard quantized DPI for high-density screens.
61 */
62 public static final int DENSITY_HIGH = 240;
63
64 /**
Dianne Hackborna0b46c92010-10-21 15:32:06 -070065 * Standard quantized DPI for extra-high-density screens.
66 */
67 public static final int DENSITY_XHIGH = 320;
68
69 /**
Dianne Hackbornd96e3df2012-01-25 15:12:23 -080070 * 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 Hackborna53b8282009-07-17 11:13:48 -070077 * The reference density used throughout the system.
78 */
79 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -070081 /**
Dianne Hackborn908aecc2012-07-31 16:37:34 -070082 * Scaling factor to convert a density in DPI units to the density scale.
83 * @hide
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -070084 */
Dianne Hackborn908aecc2012-07-31 16:37:34 -070085 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 Hackborndde331c2012-08-03 14:01:57 -070095 public static int DENSITY_DEVICE = getDeviceDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
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 Dougherty6b13bc02009-10-30 19:05:53 -0700110 * 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 -0800111 *
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 Hackborna53b8282009-07-17 11:13:48 -0700120 * @see #DENSITY_DEFAULT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
122 public float density;
123 /**
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700124 * 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 Project9066cfe2009-03-03 19:31:44 -0800129 * 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 Hackborn81e56d52011-05-26 00:55:58 -0700143 /**
144 * The reported display width prior to any compatibility mode scaling
145 * being applied.
146 * @hide
147 */
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700148 public int noncompatWidthPixels;
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700149 /**
150 * The reported display height prior to any compatibility mode scaling
151 * being applied.
152 * @hide
153 */
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700154 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 Hackborn908aecc2012-07-31 16:37:34 -0700162 * The reported display density prior to any compatibility mode scaling
163 * being applied.
164 * @hide
165 */
166 public int noncompatDensityDpi;
167 /**
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700168 * 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 Hackborne2515ee2011-04-27 18:52:56 -0400185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public DisplayMetrics() {
187 }
188
189 public void setTo(DisplayMetrics o) {
190 widthPixels = o.widthPixels;
191 heightPixels = o.heightPixels;
192 density = o.density;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700193 densityDpi = o.densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 scaledDensity = o.scaledDensity;
195 xdpi = o.xdpi;
196 ydpi = o.ydpi;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700197 noncompatWidthPixels = o.noncompatWidthPixels;
198 noncompatHeightPixels = o.noncompatHeightPixels;
199 noncompatDensity = o.noncompatDensity;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700200 noncompatDensityDpi = o.noncompatDensityDpi;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700201 noncompatScaledDensity = o.noncompatScaledDensity;
202 noncompatXdpi = o.noncompatXdpi;
203 noncompatYdpi = o.noncompatYdpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205
206 public void setToDefaults() {
207 widthPixels = 0;
208 heightPixels = 0;
Jeff Browna492c3a2012-08-23 19:48:44 -0700209 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
210 densityDpi = DENSITY_DEVICE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 scaledDensity = density;
Jeff Browna492c3a2012-08-23 19:48:44 -0700212 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 Hackborn7ac8bbd2012-11-29 11:59:58 -0800235 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 Browna492c3a2012-08-23 19:48:44 -0700250 return other != null
251 && widthPixels == other.widthPixels
252 && heightPixels == other.heightPixels
253 && density == other.density
254 && densityDpi == other.densityDpi
Jeff Browna492c3a2012-08-23 19:48:44 -0700255 && xdpi == other.xdpi
256 && ydpi == other.ydpi
257 && noncompatWidthPixels == other.noncompatWidthPixels
258 && noncompatHeightPixels == other.noncompatHeightPixels
259 && noncompatDensity == other.noncompatDensity
260 && noncompatDensityDpi == other.noncompatDensityDpi
Jeff Browna492c3a2012-08-23 19:48:44 -0700261 && noncompatXdpi == other.noncompatXdpi
262 && noncompatYdpi == other.noncompatYdpi;
263 }
264
265 @Override
266 public int hashCode() {
267 return widthPixels * heightPixels * densityDpi;
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700268 }
269
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700270 @Override
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700271 public String toString() {
272 return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
273 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
274 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
275 }
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200276
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 Hackborna53b8282009-07-17 11:13:48 -0700283 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285}