DisplayMetrics.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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.*;
20
21
22/**
23 * A structure describing general information about a display, such as its
24 * size, density, and font scaling.
25 */
26public class DisplayMetrics {
27    /**
28     * The reference density used throughout the system.
29     *
30     * @hide Pending API council approval
31     */
32    public static final int DEFAULT_DENSITY = 160;
33
34    private static final int sLcdDensity = SystemProperties.getInt("ro.sf.lcd_density",
35            DEFAULT_DENSITY);
36
37    /**
38     * The absolute width of the display in pixels.
39     */
40    public int widthPixels;
41    /**
42     * The absolute height of the display in pixels.
43     */
44    public int heightPixels;
45    /**
46     * The logical density of the display.  This is a scaling factor for the
47     * Density Independent Pixel unit, where one DIP is one pixel on an
48     * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
49     * providing the baseline of the system's display. Thus on a 160dpi screen
50     * this density value will be 1; on a 106 dpi screen it would be .75; etc.
51     *
52     * <p>This value does not exactly follow the real screen size (as given by
53     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
54     * the overall UI in steps based on gross changes in the display dpi.  For
55     * example, a 240x320 screen will have a density of 1 even if its width is
56     * 1.8", 1.3", etc. However, if the screen resolution is increased to
57     * 320x480 but the screen size remained 1.5"x2" then the density would be
58     * increased (probably to 1.5).
59     *
60     * @see #DEFAULT_DENSITY
61     */
62    public float density;
63    /**
64     * A scaling factor for fonts displayed on the display.  This is the same
65     * as {@link #density}, except that it may be adjusted in smaller
66     * increments at runtime based on a user preference for the font size.
67     */
68    public float scaledDensity;
69    /**
70     * The exact physical pixels per inch of the screen in the X dimension.
71     */
72    public float xdpi;
73    /**
74     * The exact physical pixels per inch of the screen in the Y dimension.
75     */
76    public float ydpi;
77
78    public DisplayMetrics() {
79    }
80
81    public void setTo(DisplayMetrics o) {
82        widthPixels = o.widthPixels;
83        heightPixels = o.heightPixels;
84        density = o.density;
85        scaledDensity = o.scaledDensity;
86        xdpi = o.xdpi;
87        ydpi = o.ydpi;
88    }
89
90    public void setToDefaults() {
91        widthPixels = 0;
92        heightPixels = 0;
93        density = sLcdDensity / (float) DEFAULT_DENSITY;
94        scaledDensity = density;
95        xdpi = sLcdDensity;
96        ydpi = sLcdDensity;
97    }
98}
99