The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 19 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | |
| 22 | /** |
| 23 | * A structure describing general information about a display, such as its |
| 24 | * size, density, and font scaling. |
Scott Main | 71d4b28 | 2009-08-13 12:45:31 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | */ |
| 29 | public class DisplayMetrics { |
| 30 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 31 | * Standard quantized DPI for low-density screens. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | */ |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 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 | /** |
Dianne Hackborn | b96cbbd | 2011-05-27 13:40:26 -0700 | [diff] [blame] | 41 | * 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 Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 50 | * Standard quantized DPI for high-density screens. |
| 51 | */ |
| 52 | public static final int DENSITY_HIGH = 240; |
| 53 | |
| 54 | /** |
Dianne Hackborn | a0b46c9 | 2010-10-21 15:32:06 -0700 | [diff] [blame] | 55 | * Standard quantized DPI for extra-high-density screens. |
| 56 | */ |
| 57 | public static final int DENSITY_XHIGH = 320; |
| 58 | |
| 59 | /** |
Dianne Hackborn | b663a4b | 2012-01-25 15:12:23 -0800 | [diff] [blame^] | 60 | * Standard quantized DPI for extra-extra-high-density screens. Applications |
| 61 | * should not generally worry about this density; relying on XHIGH graphics |
| 62 | * being scaled up to it should be sufficient for almost all cases. |
| 63 | */ |
| 64 | public static final int DENSITY_XXHIGH = 480; |
| 65 | |
| 66 | /** |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 67 | * The reference density used throughout the system. |
| 68 | */ |
| 69 | public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 71 | /** |
| 72 | * The device's density. |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 73 | * @hide becase eventually this should be able to change while |
| 74 | * running, so shouldn't be a constant. |
Mitsuru Oshima | 2e3d3b9 | 2009-05-06 15:04:28 -0700 | [diff] [blame] | 75 | */ |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 76 | public static final int DENSITY_DEVICE = getDeviceDensity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * The absolute width of the display in pixels. |
| 80 | */ |
| 81 | public int widthPixels; |
| 82 | /** |
| 83 | * The absolute height of the display in pixels. |
| 84 | */ |
| 85 | public int heightPixels; |
| 86 | /** |
| 87 | * The logical density of the display. This is a scaling factor for the |
| 88 | * Density Independent Pixel unit, where one DIP is one pixel on an |
| 89 | * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), |
| 90 | * providing the baseline of the system's display. Thus on a 160dpi screen |
Dirk Dougherty | 6b13bc0 | 2009-10-30 19:05:53 -0700 | [diff] [blame] | 91 | * this density value will be 1; on a 120 dpi screen it would be .75; etc. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | * |
| 93 | * <p>This value does not exactly follow the real screen size (as given by |
| 94 | * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of |
| 95 | * the overall UI in steps based on gross changes in the display dpi. For |
| 96 | * example, a 240x320 screen will have a density of 1 even if its width is |
| 97 | * 1.8", 1.3", etc. However, if the screen resolution is increased to |
| 98 | * 320x480 but the screen size remained 1.5"x2" then the density would be |
| 99 | * increased (probably to 1.5). |
| 100 | * |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 101 | * @see #DENSITY_DEFAULT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | */ |
| 103 | public float density; |
| 104 | /** |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 105 | * The screen density expressed as dots-per-inch. May be either |
| 106 | * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. |
| 107 | */ |
| 108 | public int densityDpi; |
| 109 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | * A scaling factor for fonts displayed on the display. This is the same |
| 111 | * as {@link #density}, except that it may be adjusted in smaller |
| 112 | * increments at runtime based on a user preference for the font size. |
| 113 | */ |
| 114 | public float scaledDensity; |
| 115 | /** |
| 116 | * The exact physical pixels per inch of the screen in the X dimension. |
| 117 | */ |
| 118 | public float xdpi; |
| 119 | /** |
| 120 | * The exact physical pixels per inch of the screen in the Y dimension. |
| 121 | */ |
| 122 | public float ydpi; |
| 123 | |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 124 | /** |
| 125 | * The reported display width prior to any compatibility mode scaling |
| 126 | * being applied. |
| 127 | * @hide |
| 128 | */ |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 129 | public int noncompatWidthPixels; |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame] | 130 | /** |
| 131 | * The reported display height prior to any compatibility mode scaling |
| 132 | * being applied. |
| 133 | * @hide |
| 134 | */ |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 135 | public int noncompatHeightPixels; |
| 136 | /** |
| 137 | * The reported display density prior to any compatibility mode scaling |
| 138 | * being applied. |
| 139 | * @hide |
| 140 | */ |
| 141 | public float noncompatDensity; |
| 142 | /** |
| 143 | * The reported scaled density prior to any compatibility mode scaling |
| 144 | * being applied. |
| 145 | * @hide |
| 146 | */ |
| 147 | public float noncompatScaledDensity; |
| 148 | /** |
| 149 | * The reported display xdpi prior to any compatibility mode scaling |
| 150 | * being applied. |
| 151 | * @hide |
| 152 | */ |
| 153 | public float noncompatXdpi; |
| 154 | /** |
| 155 | * The reported display ydpi prior to any compatibility mode scaling |
| 156 | * being applied. |
| 157 | * @hide |
| 158 | */ |
| 159 | public float noncompatYdpi; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | public DisplayMetrics() { |
| 162 | } |
| 163 | |
| 164 | public void setTo(DisplayMetrics o) { |
| 165 | widthPixels = o.widthPixels; |
| 166 | heightPixels = o.heightPixels; |
| 167 | density = o.density; |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 168 | densityDpi = o.densityDpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | scaledDensity = o.scaledDensity; |
| 170 | xdpi = o.xdpi; |
| 171 | ydpi = o.ydpi; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 172 | noncompatWidthPixels = o.noncompatWidthPixels; |
| 173 | noncompatHeightPixels = o.noncompatHeightPixels; |
| 174 | noncompatDensity = o.noncompatDensity; |
| 175 | noncompatScaledDensity = o.noncompatScaledDensity; |
| 176 | noncompatXdpi = o.noncompatXdpi; |
| 177 | noncompatYdpi = o.noncompatYdpi; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public void setToDefaults() { |
| 181 | widthPixels = 0; |
| 182 | heightPixels = 0; |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 183 | density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 184 | densityDpi = DENSITY_DEVICE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | scaledDensity = density; |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 186 | xdpi = DENSITY_DEVICE; |
| 187 | ydpi = DENSITY_DEVICE; |
Dianne Hackborn | 2b31d53 | 2011-06-23 11:58:50 -0700 | [diff] [blame] | 188 | noncompatWidthPixels = 0; |
| 189 | noncompatHeightPixels = 0; |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 192 | @Override |
Mitsuru Oshima | 58feea7 | 2009-05-11 15:54:27 -0700 | [diff] [blame] | 193 | public String toString() { |
| 194 | return "DisplayMetrics{density=" + density + ", width=" + widthPixels + |
| 195 | ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + |
| 196 | ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; |
| 197 | } |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 198 | |
| 199 | private static int getDeviceDensity() { |
| 200 | // qemu.sf.lcd_density can be used to override ro.sf.lcd_density |
| 201 | // when running in the emulator, allowing for dynamic configurations. |
| 202 | // The reason for this is that ro.sf.lcd_density is write-once and is |
| 203 | // set by the init process when it parses build.prop before anything else. |
| 204 | return SystemProperties.getInt("qemu.sf.lcd_density", |
Dianne Hackborn | a53b828 | 2009-07-17 11:13:48 -0700 | [diff] [blame] | 205 | SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); |
David 'Digit' Turner | 2a578ae5 | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 206 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |