DisplayMetrics.java revision e2515eebf42c763c0a2d9f873a153711778cfc17
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 /** @hide */ 109 public int realWidthPixels; 110 /** @hide */ 111 public int realHeightPixels; 112 113 public DisplayMetrics() { 114 } 115 116 public void setTo(DisplayMetrics o) { 117 widthPixels = o.widthPixels; 118 heightPixels = o.heightPixels; 119 density = o.density; 120 densityDpi = o.densityDpi; 121 scaledDensity = o.scaledDensity; 122 xdpi = o.xdpi; 123 ydpi = o.ydpi; 124 realWidthPixels = o.realWidthPixels; 125 realHeightPixels = o.realHeightPixels; 126 } 127 128 public void setToDefaults() { 129 widthPixels = 0; 130 heightPixels = 0; 131 density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; 132 densityDpi = DENSITY_DEVICE; 133 scaledDensity = density; 134 xdpi = DENSITY_DEVICE; 135 ydpi = DENSITY_DEVICE; 136 realWidthPixels = 0; 137 realHeightPixels = 0; 138 } 139 140 @Override 141 public String toString() { 142 return "DisplayMetrics{density=" + density + ", width=" + widthPixels + 143 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + 144 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; 145 } 146 147 private static int getDeviceDensity() { 148 // qemu.sf.lcd_density can be used to override ro.sf.lcd_density 149 // when running in the emulator, allowing for dynamic configurations. 150 // The reason for this is that ro.sf.lcd_density is write-once and is 151 // set by the init process when it parses build.prop before anything else. 152 return SystemProperties.getInt("qemu.sf.lcd_density", 153 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); 154 } 155} 156