TypedValueCompat


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 functions

java-static Float
deriveDimension(
    unitToConvertTo: Int,
    pixelValue: Float,
    metrics: DisplayMetrics
)

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

java-static Float
dpToPx(dpValue: Float, metrics: DisplayMetrics)

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

java-static Int
getUnitFromComplexDimension(complexDimension: Int)

Return the complex unit type for the given complex dimension.

java-static Float
pxToDp(pixelValue: Float, metrics: DisplayMetrics)

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

java-static Float
pxToSp(pixelValue: Float, metrics: DisplayMetrics)

Converts a pixel value to scaled pixels (SP)

java-static Float
spToPx(spValue: Float, metrics: DisplayMetrics)

Converts a scaled pixels (SP) value to pixels

Public functions

deriveDimension

Added in 1.12.0
java-static fun deriveDimension(
    unitToConvertTo: Int,
    pixelValue: Float,
    metrics: DisplayMetrics
): Float

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

This is the inverse of applyDimension

Parameters
unitToConvertTo: Int

The unit to convert to.

pixelValue: Float

The raw pixels value to convert from.

metrics: DisplayMetrics

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
java-static fun dpToPx(dpValue: Float, metrics: DisplayMetrics): Float

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

This is a convenience function for applyDimension

Parameters
dpValue: Float

The value in DP to convert from.

metrics: DisplayMetrics

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
java-static fun getUnitFromComplexDimension(complexDimension: Int): Int

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

Parameters
complexDimension: Int

the dimension, typically data

Returns
Int

The complex unit type

pxToDp

Added in 1.12.0
java-static fun pxToDp(pixelValue: Float, metrics: DisplayMetrics): Float

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

This is a convenience function for deriveDimension

Parameters
pixelValue: Float

The raw pixels value to convert from.

metrics: DisplayMetrics

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
java-static fun pxToSp(pixelValue: Float, metrics: DisplayMetrics): Float

Converts a pixel value to scaled pixels (SP)

This is a convenience function for deriveDimension

Parameters
pixelValue: Float

The raw pixels value to convert from.

metrics: DisplayMetrics

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
java-static fun spToPx(spValue: Float, metrics: DisplayMetrics): Float

Converts a scaled pixels (SP) value to pixels

This is a convenience function for applyDimension

Parameters
spValue: Float

The value in SP to convert from.

metrics: DisplayMetrics

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

Returns
Float

A raw pixel value