TypedValueCompat


public class TypedValueCompat


Container for a dynamically typed data value. Primarily used with android.content.res.Resources for holding resource values.

Used to convert between dimension values like DP and SP to pixels, and vice versa.

Summary

Public methods

static float
deriveDimension(
    int unitToConvertTo,
    float pixelValue,
    @NonNull DisplayMetrics metrics
)

Converts a pixel value to the given dimension, e.g. PX to DP.

static float
dpToPx(float dpValue, @NonNull DisplayMetrics metrics)

Converts a density-independent pixels (DP) value to pixels

static int
getUnitFromComplexDimension(int complexDimension)

Return the complex unit type for the given complex dimension.

static float
pxToDp(float pixelValue, @NonNull DisplayMetrics metrics)

Converts a pixel value to density-independent pixels (DP)

static float
pxToSp(float pixelValue, @NonNull DisplayMetrics metrics)

Converts a pixel value to scaled pixels (SP)

static float
spToPx(float spValue, @NonNull DisplayMetrics metrics)

Converts a scaled pixels (SP) value to pixels

Public methods

deriveDimension

Added in 1.12.0
public static float deriveDimension(
    int unitToConvertTo,
    float pixelValue,
    @NonNull DisplayMetrics metrics
)

Converts a pixel value to the given dimension, e.g. PX to DP.

This is the inverse of applyDimension

Parameters
int unitToConvertTo

The unit to convert to.

float pixelValue

The raw pixels value to convert from.

@NonNull DisplayMetrics metrics

Current display metrics to use in the conversion -- supplies display density and scaling information.

Returns
float

A dimension value equivalent to the given number of pixels

Throws
java.lang.IllegalArgumentException

if unitToConvertTo is not valid.

dpToPx

Added in 1.12.0
public static float dpToPx(float dpValue, @NonNull DisplayMetrics metrics)

Converts a density-independent pixels (DP) value to pixels

This is a convenience function for applyDimension

Parameters
float dpValue

The value in DP to convert from.

@NonNull DisplayMetrics metrics

Current display metrics to use in the conversion -- supplies display density and scaling information.

Returns
float

A raw pixel value

getUnitFromComplexDimension

Added in 1.12.0
public static int getUnitFromComplexDimension(int complexDimension)

Return the complex unit type for the given complex dimension. For example, a dimen type with value 12sp will return COMPLEX_UNIT_SP.

Parameters
int complexDimension

the dimension, typically data

Returns
int

The complex unit type

pxToDp

Added in 1.12.0
public static float pxToDp(float pixelValue, @NonNull DisplayMetrics metrics)

Converts a pixel value to density-independent pixels (DP)

This is a convenience function for deriveDimension

Parameters
float pixelValue

The raw pixels value to convert from.

@NonNull DisplayMetrics metrics

Current display metrics to use in the conversion -- supplies display density and scaling information.

Returns
float

A dimension value (in DP) representing the given number of pixels.

pxToSp

Added in 1.12.0
public static float pxToSp(float pixelValue, @NonNull DisplayMetrics metrics)

Converts a pixel value to scaled pixels (SP)

This is a convenience function for deriveDimension

Parameters
float pixelValue

The raw pixels value to convert from.

@NonNull DisplayMetrics metrics

Current display metrics to use in the conversion -- supplies display density and scaling information.

Returns
float

A dimension value (in SP) representing the given number of pixels.

spToPx

Added in 1.12.0
public static float spToPx(float spValue, @NonNull DisplayMetrics metrics)

Converts a scaled pixels (SP) value to pixels

This is a convenience function for applyDimension

Parameters
float spValue

The value in SP to convert from.

@NonNull DisplayMetrics metrics

Current display metrics to use in the conversion -- supplies display density and scaling information.

Returns
float

A raw pixel value