blob: 519b98056b9f31e9a3a874a1c70656cd365cf30a [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 Hackbornb96cbbd2011-05-27 13:40:26 -070041 * Standard quantized DPI for 720p TV screens. Applications should
42 * generally not worry about this density, instead targeting
43 * {@link #DENSITY_XHIGH} for 1080p TV screens. For situations where
44 * output is needed for a 720p screen, the UI elements can be scaled
45 * automatically by the platform.
46 */
47 public static final int DENSITY_TV = 213;
48
49 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070050 * Standard quantized DPI for high-density screens.
51 */
52 public static final int DENSITY_HIGH = 240;
53
54 /**
Dianne Hackborna0b46c92010-10-21 15:32:06 -070055 * Standard quantized DPI for extra-high-density screens.
56 */
57 public static final int DENSITY_XHIGH = 320;
58
59 /**
Dianne Hackborna53b8282009-07-17 11:13:48 -070060 * The reference density used throughout the system.
61 */
62 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -070064 /**
65 * The device's density.
Dianne Hackborna53b8282009-07-17 11:13:48 -070066 * @hide becase eventually this should be able to change while
67 * running, so shouldn't be a constant.
Mitsuru Oshima2e3d3b92009-05-06 15:04:28 -070068 */
Dianne Hackborna53b8282009-07-17 11:13:48 -070069 public static final int DENSITY_DEVICE = getDeviceDensity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71 /**
72 * The absolute width of the display in pixels.
73 */
74 public int widthPixels;
75 /**
76 * The absolute height of the display in pixels.
77 */
78 public int heightPixels;
79 /**
80 * The logical density of the display. This is a scaling factor for the
81 * Density Independent Pixel unit, where one DIP is one pixel on an
82 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
83 * providing the baseline of the system's display. Thus on a 160dpi screen
Dirk Dougherty6b13bc02009-10-30 19:05:53 -070084 * 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 -080085 *
86 * <p>This value does not exactly follow the real screen size (as given by
87 * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
88 * the overall UI in steps based on gross changes in the display dpi. For
89 * example, a 240x320 screen will have a density of 1 even if its width is
90 * 1.8", 1.3", etc. However, if the screen resolution is increased to
91 * 320x480 but the screen size remained 1.5"x2" then the density would be
92 * increased (probably to 1.5).
93 *
Dianne Hackborna53b8282009-07-17 11:13:48 -070094 * @see #DENSITY_DEFAULT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
96 public float density;
97 /**
Dianne Hackborn11ea3342009-07-22 21:48:55 -070098 * The screen density expressed as dots-per-inch. May be either
99 * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
100 */
101 public int densityDpi;
102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * A scaling factor for fonts displayed on the display. This is the same
104 * as {@link #density}, except that it may be adjusted in smaller
105 * increments at runtime based on a user preference for the font size.
106 */
107 public float scaledDensity;
108 /**
109 * The exact physical pixels per inch of the screen in the X dimension.
110 */
111 public float xdpi;
112 /**
113 * The exact physical pixels per inch of the screen in the Y dimension.
114 */
115 public float ydpi;
116
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700117 /**
118 * The reported display width prior to any compatibility mode scaling
119 * being applied.
120 * @hide
121 */
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700122 public int noncompatWidthPixels;
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700123 /**
124 * The reported display height prior to any compatibility mode scaling
125 * being applied.
126 * @hide
127 */
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700128 public int noncompatHeightPixels;
129 /**
130 * The reported display density prior to any compatibility mode scaling
131 * being applied.
132 * @hide
133 */
134 public float noncompatDensity;
135 /**
136 * The reported scaled density prior to any compatibility mode scaling
137 * being applied.
138 * @hide
139 */
140 public float noncompatScaledDensity;
141 /**
142 * The reported display xdpi prior to any compatibility mode scaling
143 * being applied.
144 * @hide
145 */
146 public float noncompatXdpi;
147 /**
148 * The reported display ydpi prior to any compatibility mode scaling
149 * being applied.
150 * @hide
151 */
152 public float noncompatYdpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public DisplayMetrics() {
155 }
156
157 public void setTo(DisplayMetrics o) {
158 widthPixels = o.widthPixels;
159 heightPixels = o.heightPixels;
160 density = o.density;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700161 densityDpi = o.densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 scaledDensity = o.scaledDensity;
163 xdpi = o.xdpi;
164 ydpi = o.ydpi;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700165 noncompatWidthPixels = o.noncompatWidthPixels;
166 noncompatHeightPixels = o.noncompatHeightPixels;
167 noncompatDensity = o.noncompatDensity;
168 noncompatScaledDensity = o.noncompatScaledDensity;
169 noncompatXdpi = o.noncompatXdpi;
170 noncompatYdpi = o.noncompatYdpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
172
173 public void setToDefaults() {
174 widthPixels = 0;
175 heightPixels = 0;
Dianne Hackborna53b8282009-07-17 11:13:48 -0700176 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700177 densityDpi = DENSITY_DEVICE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 scaledDensity = density;
Dianne Hackborna53b8282009-07-17 11:13:48 -0700179 xdpi = DENSITY_DEVICE;
180 ydpi = DENSITY_DEVICE;
Dianne Hackborn2b31d532011-06-23 11:58:50 -0700181 noncompatWidthPixels = 0;
182 noncompatHeightPixels = 0;
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700183 }
184
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700185 @Override
Mitsuru Oshima58feea72009-05-11 15:54:27 -0700186 public String toString() {
187 return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
188 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
189 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
190 }
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200191
192 private static int getDeviceDensity() {
193 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
194 // when running in the emulator, allowing for dynamic configurations.
195 // The reason for this is that ro.sf.lcd_density is write-once and is
196 // set by the init process when it parses build.prop before anything else.
197 return SystemProperties.getInt("qemu.sf.lcd_density",
Dianne Hackborna53b8282009-07-17 11:13:48 -0700198 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
David 'Digit' Turner2a578ae52009-06-18 04:30:32 +0200199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200}