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