• 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.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  */
29 public 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      * 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     /**
77      * The reference density used throughout the system.
78      */
79     public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
80 
81     /**
82      * The device's density.
83      * @hide becase eventually this should be able to change while
84      * running, so shouldn't be a constant.
85      */
86     public static final int DENSITY_DEVICE = getDeviceDensity();
87 
88     /**
89      * The absolute width of the display in pixels.
90      */
91     public int widthPixels;
92     /**
93      * The absolute height of the display in pixels.
94      */
95     public int heightPixels;
96     /**
97      * The logical density of the display.  This is a scaling factor for the
98      * Density Independent Pixel unit, where one DIP is one pixel on an
99      * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
100      * providing the baseline of the system's display. Thus on a 160dpi screen
101      * this density value will be 1; on a 120 dpi screen it would be .75; etc.
102      *
103      * <p>This value does not exactly follow the real screen size (as given by
104      * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
105      * the overall UI in steps based on gross changes in the display dpi.  For
106      * example, a 240x320 screen will have a density of 1 even if its width is
107      * 1.8", 1.3", etc. However, if the screen resolution is increased to
108      * 320x480 but the screen size remained 1.5"x2" then the density would be
109      * increased (probably to 1.5).
110      *
111      * @see #DENSITY_DEFAULT
112      */
113     public float density;
114     /**
115      * The screen density expressed as dots-per-inch.  May be either
116      * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
117      */
118     public int densityDpi;
119     /**
120      * A scaling factor for fonts displayed on the display.  This is the same
121      * as {@link #density}, except that it may be adjusted in smaller
122      * increments at runtime based on a user preference for the font size.
123      */
124     public float scaledDensity;
125     /**
126      * The exact physical pixels per inch of the screen in the X dimension.
127      */
128     public float xdpi;
129     /**
130      * The exact physical pixels per inch of the screen in the Y dimension.
131      */
132     public float ydpi;
133 
134     /**
135      * The reported display width prior to any compatibility mode scaling
136      * being applied.
137      * @hide
138      */
139     public int noncompatWidthPixels;
140     /**
141      * The reported display height prior to any compatibility mode scaling
142      * being applied.
143      * @hide
144      */
145     public int noncompatHeightPixels;
146     /**
147      * The reported display density prior to any compatibility mode scaling
148      * being applied.
149      * @hide
150      */
151     public float noncompatDensity;
152     /**
153      * The reported scaled density prior to any compatibility mode scaling
154      * being applied.
155      * @hide
156      */
157     public float noncompatScaledDensity;
158     /**
159      * The reported display xdpi prior to any compatibility mode scaling
160      * being applied.
161      * @hide
162      */
163     public float noncompatXdpi;
164     /**
165      * The reported display ydpi prior to any compatibility mode scaling
166      * being applied.
167      * @hide
168      */
169     public float noncompatYdpi;
170 
DisplayMetrics()171     public DisplayMetrics() {
172     }
173 
setTo(DisplayMetrics o)174     public void setTo(DisplayMetrics o) {
175         widthPixels = o.widthPixels;
176         heightPixels = o.heightPixels;
177         density = o.density;
178         densityDpi = o.densityDpi;
179         scaledDensity = o.scaledDensity;
180         xdpi = o.xdpi;
181         ydpi = o.ydpi;
182         noncompatWidthPixels = o.noncompatWidthPixels;
183         noncompatHeightPixels = o.noncompatHeightPixels;
184         noncompatDensity = o.noncompatDensity;
185         noncompatScaledDensity = o.noncompatScaledDensity;
186         noncompatXdpi = o.noncompatXdpi;
187         noncompatYdpi = o.noncompatYdpi;
188     }
189 
setToDefaults()190     public void setToDefaults() {
191         widthPixels = 0;
192         heightPixels = 0;
193         density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
194         densityDpi = DENSITY_DEVICE;
195         scaledDensity = density;
196         xdpi = DENSITY_DEVICE;
197         ydpi = DENSITY_DEVICE;
198         noncompatWidthPixels = 0;
199         noncompatHeightPixels = 0;
200     }
201 
202     @Override
toString()203     public String toString() {
204         return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
205             ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
206             ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
207     }
208 
getDeviceDensity()209     private static int getDeviceDensity() {
210         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
211         // when running in the emulator, allowing for dynamic configurations.
212         // The reason for this is that ro.sf.lcd_density is write-once and is
213         // set by the init process when it parses build.prop before anything else.
214         return SystemProperties.getInt("qemu.sf.lcd_density",
215                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
216     }
217 }
218