DisplayMetrics.java revision 81e56d535c853d73ff537357da5b935f51cb779d
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     * Standard quantized DPI for high-density screens.
42     */
43    public static final int DENSITY_HIGH = 240;
44
45    /**
46     * Standard quantized DPI for extra-high-density screens.
47     */
48    public static final int DENSITY_XHIGH = 320;
49
50    /**
51     * The reference density used throughout the system.
52     */
53    public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
54
55    /**
56     * The device's density.
57     * @hide becase eventually this should be able to change while
58     * running, so shouldn't be a constant.
59     */
60    public static final int DENSITY_DEVICE = getDeviceDensity();
61
62    /**
63     * The absolute width of the display in pixels.
64     */
65    public int widthPixels;
66    /**
67     * The absolute height of the display in pixels.
68     */
69    public int heightPixels;
70    /**
71     * The logical density of the display.  This is a scaling factor for the
72     * Density Independent Pixel unit, where one DIP is one pixel on an
73     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
74     * providing the baseline of the system's display. Thus on a 160dpi screen
75     * this density value will be 1; on a 120 dpi screen it would be .75; etc.
76     *
77     * <p>This value does not exactly follow the real screen size (as given by
78     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
79     * the overall UI in steps based on gross changes in the display dpi.  For
80     * example, a 240x320 screen will have a density of 1 even if its width is
81     * 1.8", 1.3", etc. However, if the screen resolution is increased to
82     * 320x480 but the screen size remained 1.5"x2" then the density would be
83     * increased (probably to 1.5).
84     *
85     * @see #DENSITY_DEFAULT
86     */
87    public float density;
88    /**
89     * The screen density expressed as dots-per-inch.  May be either
90     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
91     */
92    public int densityDpi;
93    /**
94     * A scaling factor for fonts displayed on the display.  This is the same
95     * as {@link #density}, except that it may be adjusted in smaller
96     * increments at runtime based on a user preference for the font size.
97     */
98    public float scaledDensity;
99    /**
100     * The exact physical pixels per inch of the screen in the X dimension.
101     */
102    public float xdpi;
103    /**
104     * The exact physical pixels per inch of the screen in the Y dimension.
105     */
106    public float ydpi;
107
108    /**
109     * The reported display width prior to any compatibility mode scaling
110     * being applied.
111     * @hide
112     */
113    public int unscaledWidthPixels;
114    /**
115     * The reported display height prior to any compatibility mode scaling
116     * being applied.
117     * @hide
118     */
119    public int unscaledHeightPixels;
120
121    public DisplayMetrics() {
122    }
123
124    public void setTo(DisplayMetrics o) {
125        widthPixels = o.widthPixels;
126        heightPixels = o.heightPixels;
127        density = o.density;
128        densityDpi = o.densityDpi;
129        scaledDensity = o.scaledDensity;
130        xdpi = o.xdpi;
131        ydpi = o.ydpi;
132        unscaledWidthPixels = o.unscaledWidthPixels;
133        unscaledHeightPixels = o.unscaledHeightPixels;
134    }
135
136    public void setToDefaults() {
137        widthPixels = 0;
138        heightPixels = 0;
139        density = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
140        densityDpi = DENSITY_DEVICE;
141        scaledDensity = density;
142        xdpi = DENSITY_DEVICE;
143        ydpi = DENSITY_DEVICE;
144        unscaledWidthPixels = 0;
145        unscaledHeightPixels = 0;
146    }
147
148    @Override
149    public String toString() {
150        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
151            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
152            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
153    }
154
155    private static int getDeviceDensity() {
156        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
157        // when running in the emulator, allowing for dynamic configurations.
158        // The reason for this is that ro.sf.lcd_density is write-once and is
159        // set by the init process when it parses build.prop before anything else.
160        return SystemProperties.getInt("qemu.sf.lcd_density",
161                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
162    }
163}
164