TypedValue.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/* 2 * Copyright (C) 2007 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 19/** 20 * Container for a dynamically typed data value. Primarily used with 21 * {@link android.content.res.Resources} for holding resource values. 22 */ 23public class TypedValue { 24 /** The value contains no data. */ 25 public static final int TYPE_NULL = 0x00; 26 27 /** The <var>data</var> field holds a resource identifier. */ 28 public static final int TYPE_REFERENCE = 0x01; 29 /** The <var>data</var> field holds an attribute resource 30 * identifier (referencing an attribute in the current theme 31 * style, not a resource entry). */ 32 public static final int TYPE_ATTRIBUTE = 0x02; 33 /** The <var>string</var> field holds string data. In addition, if 34 * <var>data</var> is non-zero then it is the string block 35 * index of the string and <var>assetCookie</var> is the set of 36 * assets the string came from. */ 37 public static final int TYPE_STRING = 0x03; 38 /** The <var>data</var> field holds an IEEE 754 floating point number. */ 39 public static final int TYPE_FLOAT = 0x04; 40 /** The <var>data</var> field holds a complex number encoding a 41 * dimension value. */ 42 public static final int TYPE_DIMENSION = 0x05; 43 /** The <var>data</var> field holds a complex number encoding a fraction 44 * of a container. */ 45 public static final int TYPE_FRACTION = 0x06; 46 47 /** Identifies the start of plain integer values. Any type value 48 * from this to {@link #TYPE_LAST_INT} means the 49 * <var>data</var> field holds a generic integer value. */ 50 public static final int TYPE_FIRST_INT = 0x10; 51 52 /** The <var>data</var> field holds a number that was 53 * originally specified in decimal. */ 54 public static final int TYPE_INT_DEC = 0x10; 55 /** The <var>data</var> field holds a number that was 56 * originally specified in hexadecimal (0xn). */ 57 public static final int TYPE_INT_HEX = 0x11; 58 /** The <var>data</var> field holds 0 or 1 that was originally 59 * specified as "false" or "true". */ 60 public static final int TYPE_INT_BOOLEAN = 0x12; 61 62 /** Identifies the start of integer values that were specified as 63 * color constants (starting with '#'). */ 64 public static final int TYPE_FIRST_COLOR_INT = 0x1c; 65 66 /** The <var>data</var> field holds a color that was originally 67 * specified as #aarrggbb. */ 68 public static final int TYPE_INT_COLOR_ARGB8 = 0x1c; 69 /** The <var>data</var> field holds a color that was originally 70 * specified as #rrggbb. */ 71 public static final int TYPE_INT_COLOR_RGB8 = 0x1d; 72 /** The <var>data</var> field holds a color that was originally 73 * specified as #argb. */ 74 public static final int TYPE_INT_COLOR_ARGB4 = 0x1e; 75 /** The <var>data</var> field holds a color that was originally 76 * specified as #rgb. */ 77 public static final int TYPE_INT_COLOR_RGB4 = 0x1f; 78 79 /** Identifies the end of integer values that were specified as color 80 * constants. */ 81 public static final int TYPE_LAST_COLOR_INT = 0x1f; 82 83 /** Identifies the end of plain integer values. */ 84 public static final int TYPE_LAST_INT = 0x1f; 85 86 /* ------------------------------------------------------------ */ 87 88 /** Complex data: bit location of unit information. */ 89 public static final int COMPLEX_UNIT_SHIFT = 0; 90 /** Complex data: mask to extract unit information (after shifting by 91 * {@link #COMPLEX_UNIT_SHIFT}). This gives us 16 possible types, as 92 * defined below. */ 93 public static final int COMPLEX_UNIT_MASK = 0xf; 94 95 /** {@link #TYPE_DIMENSION} complex unit: Value is raw pixels. */ 96 public static final int COMPLEX_UNIT_PX = 0; 97 /** {@link #TYPE_DIMENSION} complex unit: Value is Device Independent 98 * Pixels. */ 99 public static final int COMPLEX_UNIT_DIP = 1; 100 /** {@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */ 101 public static final int COMPLEX_UNIT_SP = 2; 102 /** {@link #TYPE_DIMENSION} complex unit: Value is in points. */ 103 public static final int COMPLEX_UNIT_PT = 3; 104 /** {@link #TYPE_DIMENSION} complex unit: Value is in inches. */ 105 public static final int COMPLEX_UNIT_IN = 4; 106 /** {@link #TYPE_DIMENSION} complex unit: Value is in millimeters. */ 107 public static final int COMPLEX_UNIT_MM = 5; 108 109 /** {@link #TYPE_FRACTION} complex unit: A basic fraction of the overall 110 * size. */ 111 public static final int COMPLEX_UNIT_FRACTION = 0; 112 /** {@link #TYPE_FRACTION} complex unit: A fraction of the parent size. */ 113 public static final int COMPLEX_UNIT_FRACTION_PARENT = 1; 114 115 /** Complex data: where the radix information is, telling where the decimal 116 * place appears in the mantissa. */ 117 public static final int COMPLEX_RADIX_SHIFT = 4; 118 /** Complex data: mask to extract radix information (after shifting by 119 * {@link #COMPLEX_RADIX_SHIFT}). This give us 4 possible fixed point 120 * representations as defined below. */ 121 public static final int COMPLEX_RADIX_MASK = 0x3; 122 123 /** Complex data: the mantissa is an integral number -- i.e., 0xnnnnnn.0 */ 124 public static final int COMPLEX_RADIX_23p0 = 0; 125 /** Complex data: the mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn */ 126 public static final int COMPLEX_RADIX_16p7 = 1; 127 /** Complex data: the mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn */ 128 public static final int COMPLEX_RADIX_8p15 = 2; 129 /** Complex data: the mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn */ 130 public static final int COMPLEX_RADIX_0p23 = 3; 131 132 /** Complex data: bit location of mantissa information. */ 133 public static final int COMPLEX_MANTISSA_SHIFT = 8; 134 /** Complex data: mask to extract mantissa information (after shifting by 135 * {@link #COMPLEX_MANTISSA_SHIFT}). This gives us 23 bits of precision; 136 * the top bit is the sign. */ 137 public static final int COMPLEX_MANTISSA_MASK = 0xffffff; 138 139 /* ------------------------------------------------------------ */ 140 141 /** 142 * If {@link #density} is equal to this value, then the density should be 143 * treated as the system's default density value: {@link DisplayMetrics#DEFAULT_DENSITY}. 144 * 145 * @hide Pending API council approval 146 */ 147 public static final int DENSITY_DEFAULT = 0; 148 149 /* ------------------------------------------------------------ */ 150 151 /** The type held by this value, as defined by the constants here. 152 * This tells you how to interpret the other fields in the object. */ 153 public int type; 154 155 /** If the value holds a string, this is it. */ 156 public CharSequence string; 157 158 /** Basic data in the value, interpreted according to {@link #type} */ 159 public int data; 160 161 /** Additional information about where the value came from; only 162 * set for strings. */ 163 public int assetCookie; 164 165 /** If Value came from a resource, this holds the corresponding resource id. */ 166 public int resourceId; 167 168 /** If Value came from a resource, these are the configurations for which 169 * its contents can change. */ 170 public int changingConfigurations = -1; 171 172 /** 173 * If the Value came from a resource, this holds the corresponding pixel density. 174 * 175 * @hide Pending API council approval 176 * */ 177 public int density; 178 179 /* ------------------------------------------------------------ */ 180 181 /** Return the data for this value as a float. Only use for values 182 * whose type is {@link #TYPE_FLOAT}. */ 183 public final float getFloat() { 184 return Float.intBitsToFloat(data); 185 } 186 187 private static final float MANTISSA_MULT = 188 1.0f / (1<<TypedValue.COMPLEX_MANTISSA_SHIFT); 189 private static final float[] RADIX_MULTS = new float[] { 190 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT, 191 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT 192 }; 193 194 /** 195 * Retrieve the base value from a complex data integer. This uses the 196 * {@link #COMPLEX_MANTISSA_MASK} and {@link #COMPLEX_RADIX_MASK} fields of 197 * the data to compute a floating point representation of the number they 198 * describe. The units are ignored. 199 * 200 * @param complex A complex data value. 201 * 202 * @return A floating point value corresponding to the complex data. 203 */ 204 public static float complexToFloat(int complex) 205 { 206 return (complex&(TypedValue.COMPLEX_MANTISSA_MASK 207 <<TypedValue.COMPLEX_MANTISSA_SHIFT)) 208 * RADIX_MULTS[(complex>>TypedValue.COMPLEX_RADIX_SHIFT) 209 & TypedValue.COMPLEX_RADIX_MASK]; 210 } 211 212 /** 213 * Converts a complex data value holding a dimension to its final floating 214 * point value. The given <var>data</var> must be structured as a 215 * {@link #TYPE_DIMENSION}. 216 * 217 * @param data A complex data value holding a unit, magnitude, and 218 * mantissa. 219 * @param metrics Current display metrics to use in the conversion -- 220 * supplies display density and scaling information. 221 * 222 * @return The complex floating point value multiplied by the appropriate 223 * metrics depending on its unit. 224 */ 225 public static float complexToDimension(int data, DisplayMetrics metrics) 226 { 227 return applyDimension( 228 (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK, 229 complexToFloat(data), 230 metrics); 231 } 232 233 /** 234 * Converts a complex data value holding a dimension to its final value 235 * as an integer pixel offset. This is the same as 236 * {@link #complexToDimension}, except the raw floating point value is 237 * truncated to an integer (pixel) value. 238 * The given <var>data</var> must be structured as a 239 * {@link #TYPE_DIMENSION}. 240 * 241 * @param data A complex data value holding a unit, magnitude, and 242 * mantissa. 243 * @param metrics Current display metrics to use in the conversion -- 244 * supplies display density and scaling information. 245 * 246 * @return The number of pixels specified by the data and its desired 247 * multiplier and units. 248 */ 249 public static int complexToDimensionPixelOffset(int data, 250 DisplayMetrics metrics) 251 { 252 return (int)applyDimension( 253 (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK, 254 complexToFloat(data), 255 metrics); 256 } 257 258 /** 259 * Converts a complex data value holding a dimension to its final value 260 * as an integer pixel size. This is the same as 261 * {@link #complexToDimension}, except the raw floating point value is 262 * converted to an integer (pixel) value for use as a size. A size 263 * conversion involves rounding the base value, and ensuring that a 264 * non-zero base value is at least one pixel in size. 265 * The given <var>data</var> must be structured as a 266 * {@link #TYPE_DIMENSION}. 267 * 268 * @param data A complex data value holding a unit, magnitude, and 269 * mantissa. 270 * @param metrics Current display metrics to use in the conversion -- 271 * supplies display density and scaling information. 272 * 273 * @return The number of pixels specified by the data and its desired 274 * multiplier and units. 275 */ 276 public static int complexToDimensionPixelSize(int data, 277 DisplayMetrics metrics) 278 { 279 final float value = complexToFloat(data); 280 final float f = applyDimension( 281 (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK, 282 value, 283 metrics); 284 final int res = (int)(f+0.5f); 285 if (res != 0) return res; 286 if (value == 0) return 0; 287 if (value > 0) return 1; 288 return -1; 289 } 290 291 public static float complexToDimensionNoisy(int data, DisplayMetrics metrics) 292 { 293 float res = complexToDimension(data, metrics); 294 System.out.println( 295 "Dimension (0x" + ((data>>TypedValue.COMPLEX_MANTISSA_SHIFT) 296 & TypedValue.COMPLEX_MANTISSA_MASK) 297 + "*" + (RADIX_MULTS[(data>>TypedValue.COMPLEX_RADIX_SHIFT) 298 & TypedValue.COMPLEX_RADIX_MASK] / MANTISSA_MULT) 299 + ")" + DIMENSION_UNIT_STRS[(data>>COMPLEX_UNIT_SHIFT) 300 & COMPLEX_UNIT_MASK] 301 + " = " + res); 302 return res; 303 } 304 305 /** 306 * Converts an unpacked complex data value holding a dimension to its final floating 307 * point value. The two parameters <var>unit</var> and <var>value</var> 308 * are as in {@link #TYPE_DIMENSION}. 309 * 310 * @param unit The unit to convert from. 311 * @param value The value to apply the unit to. 312 * @param metrics Current display metrics to use in the conversion -- 313 * supplies display density and scaling information. 314 * 315 * @return The complex floating point value multiplied by the appropriate 316 * metrics depending on its unit. 317 */ 318 public static float applyDimension(int unit, float value, 319 DisplayMetrics metrics) 320 { 321 switch (unit) { 322 case COMPLEX_UNIT_PX: 323 return value; 324 case COMPLEX_UNIT_DIP: 325 return value * metrics.density; 326 case COMPLEX_UNIT_SP: 327 return value * metrics.scaledDensity; 328 case COMPLEX_UNIT_PT: 329 return value * metrics.xdpi * (1.0f/72); 330 case COMPLEX_UNIT_IN: 331 return value * metrics.xdpi; 332 case COMPLEX_UNIT_MM: 333 return value * metrics.xdpi * (1.0f/25.4f); 334 } 335 return 0; 336 } 337 338 /** 339 * Return the data for this value as a dimension. Only use for values 340 * whose type is {@link #TYPE_DIMENSION}. 341 * 342 * @param metrics Current display metrics to use in the conversion -- 343 * supplies display density and scaling information. 344 * 345 * @return The complex floating point value multiplied by the appropriate 346 * metrics depending on its unit. 347 */ 348 public float getDimension(DisplayMetrics metrics) 349 { 350 return complexToDimension(data, metrics); 351 } 352 353 /** 354 * Converts a complex data value holding a fraction to its final floating 355 * point value. The given <var>data</var> must be structured as a 356 * {@link #TYPE_FRACTION}. 357 * 358 * @param data A complex data value holding a unit, magnitude, and 359 * mantissa. 360 * @param base The base value of this fraction. In other words, a 361 * standard fraction is multiplied by this value. 362 * @param pbase The parent base value of this fraction. In other 363 * words, a parent fraction (nn%p) is multiplied by this 364 * value. 365 * 366 * @return The complex floating point value multiplied by the appropriate 367 * base value depending on its unit. 368 */ 369 public static float complexToFraction(int data, float base, float pbase) 370 { 371 switch ((data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK) { 372 case COMPLEX_UNIT_FRACTION: 373 return complexToFloat(data) * base; 374 case COMPLEX_UNIT_FRACTION_PARENT: 375 return complexToFloat(data) * pbase; 376 } 377 return 0; 378 } 379 380 /** 381 * Return the data for this value as a fraction. Only use for values whose 382 * type is {@link #TYPE_FRACTION}. 383 * 384 * @param base The base value of this fraction. In other words, a 385 * standard fraction is multiplied by this value. 386 * @param pbase The parent base value of this fraction. In other 387 * words, a parent fraction (nn%p) is multiplied by this 388 * value. 389 * 390 * @return The complex floating point value multiplied by the appropriate 391 * base value depending on its unit. 392 */ 393 public float getFraction(float base, float pbase) 394 { 395 return complexToFraction(data, base, pbase); 396 } 397 398 /** 399 * Regardless of the actual type of the value, try to convert it to a 400 * string value. For example, a color type will be converted to a 401 * string of the form #aarrggbb. 402 * 403 * @return CharSequence The coerced string value. If the value is 404 * null or the type is not known, null is returned. 405 */ 406 public final CharSequence coerceToString() 407 { 408 int t = type; 409 if (t == TYPE_STRING) { 410 return string; 411 } 412 return coerceToString(t, data); 413 } 414 415 private static final String[] DIMENSION_UNIT_STRS = new String[] { 416 "px", "dip", "sp", "pt", "in", "mm" 417 }; 418 private static final String[] FRACTION_UNIT_STRS = new String[] { 419 "%", "%p" 420 }; 421 422 /** 423 * Perform type conversion as per {@link #coerceToString()} on an 424 * explicitly supplied type and data. 425 * 426 * @param type The data type identifier. 427 * @param data The data value. 428 * 429 * @return String The coerced string value. If the value is 430 * null or the type is not known, null is returned. 431 */ 432 public static final String coerceToString(int type, int data) 433 { 434 switch (type) { 435 case TYPE_NULL: 436 return null; 437 case TYPE_REFERENCE: 438 return "@" + data; 439 case TYPE_ATTRIBUTE: 440 return "?" + data; 441 case TYPE_FLOAT: 442 return Float.toString(Float.intBitsToFloat(data)); 443 case TYPE_DIMENSION: 444 return Float.toString(complexToFloat(data)) + DIMENSION_UNIT_STRS[ 445 (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK]; 446 case TYPE_FRACTION: 447 return Float.toString(complexToFloat(data)*100) + FRACTION_UNIT_STRS[ 448 (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK]; 449 case TYPE_INT_HEX: 450 return "0x" + Integer.toHexString(data); 451 case TYPE_INT_BOOLEAN: 452 return data != 0 ? "true" : "false"; 453 } 454 455 if (type >= TYPE_FIRST_COLOR_INT && type <= TYPE_LAST_COLOR_INT) { 456 return "#" + Integer.toHexString(data); 457 } else if (type >= TYPE_FIRST_INT && type <= TYPE_LAST_INT) { 458 return Integer.toString(data); 459 } 460 461 return null; 462 } 463 464 public void setTo(TypedValue other) 465 { 466 type = other.type; 467 string = other.string; 468 data = other.data; 469 assetCookie = other.assetCookie; 470 resourceId = other.resourceId; 471 density = other.density; 472 } 473 474 public String toString() 475 { 476 StringBuilder sb = new StringBuilder(); 477 sb.append("TypedValue{t=0x").append(Integer.toHexString(type)); 478 sb.append("/d=0x").append(Integer.toHexString(data)); 479 if (type == TYPE_STRING) { 480 sb.append(" \"").append(string != null ? string : "<null>").append("\""); 481 } 482 if (assetCookie != 0) { 483 sb.append(" a=").append(assetCookie); 484 } 485 if (resourceId != 0) { 486 sb.append(" r=0x").append(Integer.toHexString(resourceId)); 487 } 488 sb.append("}"); 489 return sb.toString(); 490 } 491}; 492 493