FileDocCategorySizeDatePackage
TypedValue.javaAPI DocAndroid 5.1 API20234Thu Mar 12 22:22:10 GMT 2015android.util

TypedValue

public class TypedValue extends Object
Container for a dynamically typed data value. Primarily used with {@link android.content.res.Resources} for holding resource values.

Fields Summary
public static final int
TYPE_NULL
The value contains no data.
public static final int
TYPE_REFERENCE
The data field holds a resource identifier.
public static final int
TYPE_ATTRIBUTE
The data field holds an attribute resource identifier (referencing an attribute in the current theme style, not a resource entry).
public static final int
TYPE_STRING
The string field holds string data. In addition, if data is non-zero then it is the string block index of the string and assetCookie is the set of assets the string came from.
public static final int
TYPE_FLOAT
The data field holds an IEEE 754 floating point number.
public static final int
TYPE_DIMENSION
The data field holds a complex number encoding a dimension value.
public static final int
TYPE_FRACTION
The data field holds a complex number encoding a fraction of a container.
public static final int
TYPE_FIRST_INT
Identifies the start of plain integer values. Any type value from this to {@link #TYPE_LAST_INT} means the data field holds a generic integer value.
public static final int
TYPE_INT_DEC
The data field holds a number that was originally specified in decimal.
public static final int
TYPE_INT_HEX
The data field holds a number that was originally specified in hexadecimal (0xn).
public static final int
TYPE_INT_BOOLEAN
The data field holds 0 or 1 that was originally specified as "false" or "true".
public static final int
TYPE_FIRST_COLOR_INT
Identifies the start of integer values that were specified as color constants (starting with '#').
public static final int
TYPE_INT_COLOR_ARGB8
The data field holds a color that was originally specified as #aarrggbb.
public static final int
TYPE_INT_COLOR_RGB8
The data field holds a color that was originally specified as #rrggbb.
public static final int
TYPE_INT_COLOR_ARGB4
The data field holds a color that was originally specified as #argb.
public static final int
TYPE_INT_COLOR_RGB4
The data field holds a color that was originally specified as #rgb.
public static final int
TYPE_LAST_COLOR_INT
Identifies the end of integer values that were specified as color constants.
public static final int
TYPE_LAST_INT
Identifies the end of plain integer values.
public static final int
COMPLEX_UNIT_SHIFT
Complex data: bit location of unit information.
public static final int
COMPLEX_UNIT_MASK
Complex data: mask to extract unit information (after shifting by {@link #COMPLEX_UNIT_SHIFT}). This gives us 16 possible types, as defined below.
public static final int
COMPLEX_UNIT_PX
{@link #TYPE_DIMENSION} complex unit: Value is raw pixels.
public static final int
COMPLEX_UNIT_DIP
{@link #TYPE_DIMENSION} complex unit: Value is Device Independent Pixels.
public static final int
COMPLEX_UNIT_SP
{@link #TYPE_DIMENSION} complex unit: Value is a scaled pixel.
public static final int
COMPLEX_UNIT_PT
{@link #TYPE_DIMENSION} complex unit: Value is in points.
public static final int
COMPLEX_UNIT_IN
{@link #TYPE_DIMENSION} complex unit: Value is in inches.
public static final int
COMPLEX_UNIT_MM
{@link #TYPE_DIMENSION} complex unit: Value is in millimeters.
public static final int
COMPLEX_UNIT_FRACTION
{@link #TYPE_FRACTION} complex unit: A basic fraction of the overall size.
public static final int
COMPLEX_UNIT_FRACTION_PARENT
{@link #TYPE_FRACTION} complex unit: A fraction of the parent size.
public static final int
COMPLEX_RADIX_SHIFT
Complex data: where the radix information is, telling where the decimal place appears in the mantissa.
public static final int
COMPLEX_RADIX_MASK
Complex data: mask to extract radix information (after shifting by {@link #COMPLEX_RADIX_SHIFT}). This give us 4 possible fixed point representations as defined below.
public static final int
COMPLEX_RADIX_23p0
Complex data: the mantissa is an integral number -- i.e., 0xnnnnnn.0
public static final int
COMPLEX_RADIX_16p7
Complex data: the mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
public static final int
COMPLEX_RADIX_8p15
Complex data: the mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
public static final int
COMPLEX_RADIX_0p23
Complex data: the mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
public static final int
COMPLEX_MANTISSA_SHIFT
Complex data: bit location of mantissa information.
public static final int
COMPLEX_MANTISSA_MASK
Complex data: mask to extract mantissa information (after shifting by {@link #COMPLEX_MANTISSA_SHIFT}). This gives us 23 bits of precision; the top bit is the sign.
public static final int
DATA_NULL_UNDEFINED
{@link #TYPE_NULL} data indicating the value was not specified.
public static final int
DATA_NULL_EMPTY
{@link #TYPE_NULL} data indicating the value was explicitly set to null.
public static final int
DENSITY_DEFAULT
If {@link #density} is equal to this value, then the density should be treated as the system's default density value: {@link DisplayMetrics#DENSITY_DEFAULT}.
public static final int
DENSITY_NONE
If {@link #density} is equal to this value, then there is no density associated with the resource and it should not be scaled.
public int
type
The type held by this value, as defined by the constants here. This tells you how to interpret the other fields in the object.
public CharSequence
string
If the value holds a string, this is it.
public int
data
Basic data in the value, interpreted according to {@link #type}
public int
assetCookie
Additional information about where the value came from; only set for strings.
public int
resourceId
If Value came from a resource, this holds the corresponding resource id.
public int
changingConfigurations
If Value came from a resource, these are the configurations for which its contents can change.
public int
density
If the Value came from a resource, this holds the corresponding pixel density.
private static final float
MANTISSA_MULT
private static final float[]
RADIX_MULTS
private static final String[]
DIMENSION_UNIT_STRS
private static final String[]
FRACTION_UNIT_STRS
Constructors Summary
Methods Summary
public static floatapplyDimension(int unit, float value, DisplayMetrics metrics)
Converts an unpacked complex data value holding a dimension to its final floating point value. The two parameters unit and value are as in {@link #TYPE_DIMENSION}.

param
unit The unit to convert from.
param
value The value to apply the unit to.
param
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
return
The complex floating point value multiplied by the appropriate metrics depending on its unit.

        switch (unit) {
        case COMPLEX_UNIT_PX:
            return value;
        case COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f/72);
        case COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f/25.4f);
        }
        return 0;
    
public final java.lang.CharSequencecoerceToString()
Regardless of the actual type of the value, try to convert it to a string value. For example, a color type will be converted to a string of the form #aarrggbb.

return
CharSequence The coerced string value. If the value is null or the type is not known, null is returned.

        int t = type;
        if (t == TYPE_STRING) {
            return string;
        }
        return coerceToString(t, data);
    
public static final java.lang.StringcoerceToString(int type, int data)
Perform type conversion as per {@link #coerceToString()} on an explicitly supplied type and data.

param
type The data type identifier.
param
data The data value.
return
String The coerced string value. If the value is null or the type is not known, null is returned.


                                                                 
           
    
        switch (type) {
        case TYPE_NULL:
            return null;
        case TYPE_REFERENCE:
            return "@" + data;
        case TYPE_ATTRIBUTE:
            return "?" + data;
        case TYPE_FLOAT:
            return Float.toString(Float.intBitsToFloat(data));
        case TYPE_DIMENSION:
            return Float.toString(complexToFloat(data)) + DIMENSION_UNIT_STRS[
                (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
        case TYPE_FRACTION:
            return Float.toString(complexToFloat(data)*100) + FRACTION_UNIT_STRS[
                (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK];
        case TYPE_INT_HEX:
            return "0x" + Integer.toHexString(data);
        case TYPE_INT_BOOLEAN:
            return data != 0 ? "true" : "false";
        }

        if (type >= TYPE_FIRST_COLOR_INT && type <= TYPE_LAST_COLOR_INT) {
            return "#" + Integer.toHexString(data);
        } else if (type >= TYPE_FIRST_INT && type <= TYPE_LAST_INT) {
            return Integer.toString(data);
        }

        return null;
    
public static floatcomplexToDimension(int data, DisplayMetrics metrics)
Converts a complex data value holding a dimension to its final floating point value. The given data must be structured as a {@link #TYPE_DIMENSION}.

param
data A complex data value holding a unit, magnitude, and mantissa.
param
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
return
The complex floating point value multiplied by the appropriate metrics depending on its unit.

        return applyDimension(
            (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
            complexToFloat(data),
            metrics);
    
public static floatcomplexToDimensionNoisy(int data, DisplayMetrics metrics)

hide
Was accidentally exposed in API level 1 for debugging purposes. Kept for compatibility just in case although the debugging code has been removed.

        return complexToDimension(data, metrics);
    
public static intcomplexToDimensionPixelOffset(int data, DisplayMetrics metrics)
Converts a complex data value holding a dimension to its final value as an integer pixel offset. This is the same as {@link #complexToDimension}, except the raw floating point value is truncated to an integer (pixel) value. The given data must be structured as a {@link #TYPE_DIMENSION}.

param
data A complex data value holding a unit, magnitude, and mantissa.
param
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
return
The number of pixels specified by the data and its desired multiplier and units.

        return (int)applyDimension(
                (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
                complexToFloat(data),
                metrics);
    
public static intcomplexToDimensionPixelSize(int data, DisplayMetrics metrics)
Converts a complex data value holding a dimension to its final value as an integer pixel size. This is the same as {@link #complexToDimension}, except the raw floating point value is converted to an integer (pixel) value for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size. The given data must be structured as a {@link #TYPE_DIMENSION}.

param
data A complex data value holding a unit, magnitude, and mantissa.
param
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
return
The number of pixels specified by the data and its desired multiplier and units.

        final float value = complexToFloat(data);
        final float f = applyDimension(
                (data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK,
                value,
                metrics);
        final int res = (int)(f+0.5f);
        if (res != 0) return res;
        if (value == 0) return 0;
        if (value > 0) return 1;
        return -1;
    
public static floatcomplexToFloat(int complex)
Retrieve the base value from a complex data integer. This uses the {@link #COMPLEX_MANTISSA_MASK} and {@link #COMPLEX_RADIX_MASK} fields of the data to compute a floating point representation of the number they describe. The units are ignored.

param
complex A complex data value.
return
A floating point value corresponding to the complex data.


                                                                      
        
    
        return (complex&(TypedValue.COMPLEX_MANTISSA_MASK
                   <<TypedValue.COMPLEX_MANTISSA_SHIFT))
            * RADIX_MULTS[(complex>>TypedValue.COMPLEX_RADIX_SHIFT)
                            & TypedValue.COMPLEX_RADIX_MASK];
    
public static floatcomplexToFraction(int data, float base, float pbase)
Converts a complex data value holding a fraction to its final floating point value. The given data must be structured as a {@link #TYPE_FRACTION}.

param
data A complex data value holding a unit, magnitude, and mantissa.
param
base The base value of this fraction. In other words, a standard fraction is multiplied by this value.
param
pbase The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
return
The complex floating point value multiplied by the appropriate base value depending on its unit.

        switch ((data>>COMPLEX_UNIT_SHIFT)&COMPLEX_UNIT_MASK) {
        case COMPLEX_UNIT_FRACTION:
            return complexToFloat(data) * base;
        case COMPLEX_UNIT_FRACTION_PARENT:
            return complexToFloat(data) * pbase;
        }
        return 0;
    
public intgetComplexUnit()
Return the complex unit type for this value. For example, a dimen type with value 12sp will return {@link #COMPLEX_UNIT_SP}. Only use for values whose type is {@link #TYPE_DIMENSION}.

return
The complex unit type.

         return COMPLEX_UNIT_MASK & (data>>TypedValue.COMPLEX_UNIT_SHIFT);
     
public floatgetDimension(DisplayMetrics metrics)
Return the data for this value as a dimension. Only use for values whose type is {@link #TYPE_DIMENSION}.

param
metrics Current display metrics to use in the conversion -- supplies display density and scaling information.
return
The complex floating point value multiplied by the appropriate metrics depending on its unit.

        return complexToDimension(data, metrics);
    
public final floatgetFloat()
Return the data for this value as a float. Only use for values whose type is {@link #TYPE_FLOAT}.


    /* ------------------------------------------------------------ */

                         
        
        return Float.intBitsToFloat(data);
    
public floatgetFraction(float base, float pbase)
Return the data for this value as a fraction. Only use for values whose type is {@link #TYPE_FRACTION}.

param
base The base value of this fraction. In other words, a standard fraction is multiplied by this value.
param
pbase The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
return
The complex floating point value multiplied by the appropriate base value depending on its unit.

        return complexToFraction(data, base, pbase);
    
public voidsetTo(android.util.TypedValue other)

        type = other.type;
        string = other.string;
        data = other.data;
        assetCookie = other.assetCookie;
        resourceId = other.resourceId;
        density = other.density;
    
public java.lang.StringtoString()

        StringBuilder sb = new StringBuilder();
        sb.append("TypedValue{t=0x").append(Integer.toHexString(type));
        sb.append("/d=0x").append(Integer.toHexString(data));
        if (type == TYPE_STRING) {
            sb.append(" \"").append(string != null ? string : "<null>").append("\"");
        }
        if (assetCookie != 0) {
            sb.append(" a=").append(assetCookie);
        }
        if (resourceId != 0) {
            sb.append(" r=0x").append(Integer.toHexString(resourceId));
        }
        sb.append("}");
        return sb.toString();