From 5d9c631462b8f6a46586c8747f54544621a5b22b Mon Sep 17 00:00:00 2001 From: Tyler Freeman Date: Tue, 24 Jan 2023 15:30:05 -0800 Subject: [PATCH] feat(non linear font scaling): add easier-to-find alias functions to TypedValue Add alias functions for applyDimension() and deriveDimension() with more descriptive names. Bug: 264252156 Test: atest TypedValueTest \ && atest tests/tests/content/src/android/content/res/cts/ResourcesTest.java Change-Id: I2cfa87befa4d5d5f161fcbb6bb75819b7457b4d2 --- core/api/current.txt | 2 + core/java/android/util/TypedValue.java | 42 +++++++++++++++++++ .../src/android/util/TypedValueTest.kt | 8 ++++ 3 files changed, 52 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index 653dda3d721fb..452fc6f736521 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -49156,6 +49156,8 @@ package android.util { method public static int complexToDimensionPixelSize(int, android.util.DisplayMetrics); method public static float complexToFloat(int); method public static float complexToFraction(int, float, float); + method public static float convertDimensionToPixels(int, float, @NonNull android.util.DisplayMetrics); + method public static float convertPixelsToDimension(int, float, @NonNull android.util.DisplayMetrics); method public static float deriveDimension(int, float, @NonNull android.util.DisplayMetrics); method public int getComplexUnit(); method public float getDimension(android.util.DisplayMetrics); diff --git a/core/java/android/util/TypedValue.java b/core/java/android/util/TypedValue.java index 09545fcfa60f8..b93e3386b7cca 100644 --- a/core/java/android/util/TypedValue.java +++ b/core/java/android/util/TypedValue.java @@ -494,6 +494,48 @@ public class TypedValue { } } + /** + * Converts a pixel value to the given dimension, e.g. PX to DP. + * + *

This is just an alias of {@link #deriveDimension(int, float, DisplayMetrics)} with an + * easier-to-find name. + * + * @param unitToConvertTo The unit to convert to. + * @param pixelValue The raw pixels value to convert from. + * @param metrics Current display metrics to use in the conversion -- + * supplies display density and scaling information. + * + * @return A dimension value equivalent to the given number of pixels + * @throws IllegalArgumentException if unitToConvertTo is not valid. + */ + public static float convertPixelsToDimension( + @ComplexDimensionUnit int unitToConvertTo, + float pixelValue, + @NonNull DisplayMetrics metrics) { + return deriveDimension(unitToConvertTo, pixelValue, metrics); + } + + /** + * Converts a dimension value to raw pixels, e.g. DP to PX. + * + *

This is just an alias of {@link #applyDimension(int, float, DisplayMetrics)} with an + * easier-to-find name. + * + * @param unitToConvertFrom The unit to convert from. + * @param value The dimension value to apply the unit to. + * @param metrics Current display metrics to use in the conversion -- + * supplies display density and scaling information. + * + * @return The equivalent pixel value—i.e. the complex floating point value multiplied by the + * appropriate metrics depending on its unit—or zero if unit is not valid. + */ + public static float convertDimensionToPixels( + @ComplexDimensionUnit int unitToConvertFrom, + float value, + @NonNull DisplayMetrics metrics) { + return applyDimension(unitToConvertFrom, value, metrics); + } + /** * Return the data for this value as a dimension. Only use for values * whose type is {@link #TYPE_DIMENSION}. diff --git a/core/tests/coretests/src/android/util/TypedValueTest.kt b/core/tests/coretests/src/android/util/TypedValueTest.kt index af01447fc21e2..d4e77b505a793 100644 --- a/core/tests/coretests/src/android/util/TypedValueTest.kt +++ b/core/tests/coretests/src/android/util/TypedValueTest.kt @@ -276,5 +276,13 @@ class TypedValueTest { assertThat(dimenValueToTest) .isWithin(0.05f) .of(actualDimenValue) + + // Also test the alias functions + assertThat(TypedValue.convertDimensionToPixels(dimenType, dimenValueToTest, metrics)) + .isWithin(0.05f) + .of(actualPx) + assertThat(TypedValue.convertPixelsToDimension(dimenType, actualPx, metrics)) + .isWithin(0.05f) + .of(actualDimenValue) } }