DisplayMetrics.java revision e5fb328825995aa33b5b7ecf8b5bee2b17f81715
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.content.res.CompatibilityInfo; 20import android.content.res.Configuration; 21import android.os.*; 22 23 24/** 25 * A structure describing general information about a display, such as its 26 * size, density, and font scaling. 27 */ 28public class DisplayMetrics { 29 /** 30 * The reference density used throughout the system. 31 * 32 * @hide Pending API council approval 33 */ 34 public static final int DEFAULT_DENSITY = 160; 35 36 /** 37 * The device's density. 38 * @hide 39 */ 40 public static final int DEVICE_DENSITY = SystemProperties.getInt("ro.sf.lcd_density", 41 DEFAULT_DENSITY); 42 43 /** 44 * The absolute width of the display in pixels. 45 */ 46 public int widthPixels; 47 /** 48 * The absolute height of the display in pixels. 49 */ 50 public int heightPixels; 51 /** 52 * The logical density of the display. This is a scaling factor for the 53 * Density Independent Pixel unit, where one DIP is one pixel on an 54 * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), 55 * providing the baseline of the system's display. Thus on a 160dpi screen 56 * this density value will be 1; on a 106 dpi screen it would be .75; etc. 57 * 58 * <p>This value does not exactly follow the real screen size (as given by 59 * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of 60 * the overall UI in steps based on gross changes in the display dpi. For 61 * example, a 240x320 screen will have a density of 1 even if its width is 62 * 1.8", 1.3", etc. However, if the screen resolution is increased to 63 * 320x480 but the screen size remained 1.5"x2" then the density would be 64 * increased (probably to 1.5). 65 * 66 * @see #DEFAULT_DENSITY 67 */ 68 public float density; 69 /** 70 * A scaling factor for fonts displayed on the display. This is the same 71 * as {@link #density}, except that it may be adjusted in smaller 72 * increments at runtime based on a user preference for the font size. 73 */ 74 public float scaledDensity; 75 /** 76 * The exact physical pixels per inch of the screen in the X dimension. 77 */ 78 public float xdpi; 79 /** 80 * The exact physical pixels per inch of the screen in the Y dimension. 81 */ 82 public float ydpi; 83 84 public DisplayMetrics() { 85 } 86 87 public void setTo(DisplayMetrics o) { 88 widthPixels = o.widthPixels; 89 heightPixels = o.heightPixels; 90 density = o.density; 91 scaledDensity = o.scaledDensity; 92 xdpi = o.xdpi; 93 ydpi = o.ydpi; 94 } 95 96 public void setToDefaults() { 97 widthPixels = 0; 98 heightPixels = 0; 99 density = DEVICE_DENSITY / (float) DEFAULT_DENSITY; 100 scaledDensity = density; 101 xdpi = DEVICE_DENSITY; 102 ydpi = DEVICE_DENSITY; 103 } 104 105 /** 106 * Update the display metrics based on the compatibility info and orientation 107 * {@hide} 108 */ 109 public void updateMetrics(CompatibilityInfo compatibilityInfo, int orientation) { 110 if (compatibilityInfo.mScalingRequired) { 111 float invertedRatio = compatibilityInfo.mApplicationInvertedScale; 112 density *= invertedRatio; 113 scaledDensity *= invertedRatio; 114 xdpi *= invertedRatio; 115 ydpi *= invertedRatio; 116 widthPixels *= invertedRatio; 117 heightPixels *= invertedRatio; 118 } 119 if (!compatibilityInfo.mConfiguredExpandable) { 120 // Note: this assume that configuration is updated before calling 121 // updateMetrics method. 122 int defaultWidth; 123 int defaultHeight; 124 switch (orientation) { 125 case Configuration.ORIENTATION_LANDSCAPE: { 126 defaultWidth = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_HEIGHT * density); 127 defaultHeight = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_WIDTH * density); 128 break; 129 } 130 case Configuration.ORIENTATION_PORTRAIT: 131 case Configuration.ORIENTATION_SQUARE: 132 default: { 133 defaultWidth = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_WIDTH * density); 134 defaultHeight = (int)(CompatibilityInfo.DEFAULT_PORTRAIT_HEIGHT * density); 135 break; 136 } 137 case Configuration.ORIENTATION_UNDEFINED: { 138 // don't change 139 return; 140 } 141 } 142 143 if (defaultWidth == widthPixels && defaultHeight == heightPixels) { 144 // the screen size is same as expected size. make it expandable 145 compatibilityInfo.mExpandable = true; 146 } else { 147 compatibilityInfo.mExpandable = false; 148 // adjust the size only when the device's screen is bigger. 149 if (defaultWidth < widthPixels) { 150 widthPixels = defaultWidth; 151 } 152 if (defaultHeight < heightPixels) { 153 heightPixels = defaultHeight; 154 } 155 } 156 } 157 } 158 159 public String toString() { 160 return "DisplayMetrics{density=" + density + ", width=" + widthPixels + 161 ", height=" + heightPixels + ", scaledDensity=" + scaledDensity + 162 ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}"; 163 } 164} 165