Added in API level 35

FontScaleConverter


public interface FontScaleConverter

android.content.res.FontScaleConverter


A converter for non-linear font scaling. Converts font sizes given in "sp" dimensions to a "dp" dimension according to a non-linear curve.

This is meant to improve readability at larger font scales: larger fonts will scale up more slowly than smaller fonts, so we don't get ridiculously huge fonts that don't fit on the screen.

The thinking here is that large fonts are already big enough to read, but we still want to scale them slightly to preserve the visual hierarchy when compared to smaller fonts.

Summary

Public methods

abstract float convertDpToSp(float dp)

Converts a dimension in "dp" back to "sp".

abstract float convertSpToDp(float sp)

Converts a dimension in "sp" to "dp".

static FontScaleConverter forScale(float fontScale)

Finds a matching FontScaleConverter for the given fontScale factor.

static boolean isNonLinearFontScalingActive(float fontScale)

Returns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling.

Public methods

convertDpToSp

Added in API level 35
public abstract float convertDpToSp (float dp)

Converts a dimension in "dp" back to "sp".

Parameters
dp float

Returns
float

convertSpToDp

Added in API level 35
public abstract float convertSpToDp (float sp)

Converts a dimension in "sp" to "dp".

Parameters
sp float

Returns
float

forScale

Added in API level 35
public static FontScaleConverter forScale (float fontScale)

Finds a matching FontScaleConverter for the given fontScale factor. Generally you shouldn't need this; you can use TypedValue.applyDimension(int, float, DisplayMetrics) directly and it will do the scaling conversion for you. Dimens and resources loaded from XML will also be automatically converted. But for UI frameworks or other situations where you need to do the conversion without an Android Context, you can use this method.
This method is safe to call from any thread.

Parameters
fontScale float: the scale factor, usually from Configuration#fontScale.

Returns
FontScaleConverter a converter for the given scale, or null if non-linear scaling should not be used.

isNonLinearFontScalingActive

Added in API level 35
public static boolean isNonLinearFontScalingActive (float fontScale)

Returns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling.

Example usage: isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)
This method is safe to call from any thread.

Parameters
fontScale float

Returns
boolean