Stay organized with collections
Save and categorize content based on your preferences.
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
public abstract float convertDpToSp (float dp)
Converts a dimension in "dp" back to "sp".
convertSpToDp
public abstract float convertSpToDp (float sp)
Converts a dimension in "sp" to "dp".
forScale
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.
Returns |
FontScaleConverter |
a converter for the given scale, or null if non-linear scaling should not be used. |
isNonLinearFontScalingActive
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 |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# FontScaleConverter\n\nAdded in [API level 35](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nFontScaleConverter\n==================\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/content/res/FontScaleConverter \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nFontScaleConverter\n`\n\n\n`\n\n\n`\n\n|----------------------------------------|\n| android.content.res.FontScaleConverter |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA converter for non-linear font scaling. Converts font sizes given in \"sp\" dimensions to a\n\"dp\" dimension according to a non-linear curve.\n\nThis is meant to improve readability at larger font scales: larger fonts will scale up more\nslowly than smaller fonts, so we don't get ridiculously huge fonts that don't fit on the screen.\n\nThe thinking here is that large fonts are already big enough to read, but we still want to\nscale them slightly to preserve the visual hierarchy when compared to smaller fonts.\n\nSummary\n-------\n\n| ### Public methods ||\n|-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract float` | ` `[convertDpToSp](/reference/android/content/res/FontScaleConverter#convertDpToSp(float))`(float dp) ` Converts a dimension in \"dp\" back to \"sp\". |\n| ` abstract float` | ` `[convertSpToDp](/reference/android/content/res/FontScaleConverter#convertSpToDp(float))`(float sp) ` Converts a dimension in \"sp\" to \"dp\". |\n| ` static `[FontScaleConverter](/reference/android/content/res/FontScaleConverter) | ` `[forScale](/reference/android/content/res/FontScaleConverter#forScale(float))`(float fontScale) ` Finds a matching FontScaleConverter for the given fontScale factor. |\n| ` static boolean` | ` `[isNonLinearFontScalingActive](/reference/android/content/res/FontScaleConverter#isNonLinearFontScalingActive(float))`(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. |\n\nPublic methods\n--------------\n\n### convertDpToSp\n\nAdded in [API level 35](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract float convertDpToSp (float dp)\n```\n\nConverts a dimension in \"dp\" back to \"sp\".\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------|----------------|\n| `dp` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |\n\n### convertSpToDp\n\nAdded in [API level 35](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract float convertSpToDp (float sp)\n```\n\nConverts a dimension in \"sp\" to \"dp\".\n\n\u003cbr /\u003e\n\n| Parameters ||\n|------|----------------|\n| `sp` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|---------|--------|\n| `float` | \u003cbr /\u003e |\n\n### forScale\n\nAdded in [API level 35](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic static FontScaleConverter forScale (float fontScale)\n```\n\nFinds a matching FontScaleConverter for the given fontScale factor.\n\nGenerally you shouldn't need this; you can use [TypedValue.applyDimension(int, float, DisplayMetrics)](/reference/android/util/TypedValue#applyDimension(int,%20float,%20android.util.DisplayMetrics)) directly and it will do\nthe scaling conversion for you. Dimens and resources loaded from XML will also be\nautomatically converted. But for UI frameworks or other situations where you need to do the\nconversion without an Android Context, you can use this method.\n\n\u003cbr /\u003e\n\n\nThis method is safe to call from any thread.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------|\n| `fontScale` | `float`: the scale factor, usually from [Configuration.fontScale](/reference/android/content/res/Configuration#fontScale). \u003cbr /\u003e |\n\n| Returns ||\n|-------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|\n| [FontScaleConverter](/reference/android/content/res/FontScaleConverter) | a converter for the given scale, or null if non-linear scaling should not be used. \u003cbr /\u003e |\n\n### isNonLinearFontScalingActive\n\nAdded in [API level 35](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic static boolean isNonLinearFontScalingActive (float fontScale)\n```\n\nReturns true if non-linear font scaling curves would be in effect for the given scale, false\nif the scaling would follow a linear curve or for no scaling.\n\nExample usage: `isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)`\n\n\u003cbr /\u003e\n\n\nThis method is safe to call from any thread.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-------------|----------------|\n| `fontScale` | `float` \u003cbr /\u003e |\n\n| Returns ||\n|-----------|--------|\n| `boolean` | \u003cbr /\u003e |"]]