diff options
| author | 2023-01-27 04:53:16 +0000 | |
|---|---|---|
| committer | 2023-01-27 04:53:16 +0000 | |
| commit | b3b927f56c3ce1f98274abf9f27068619f7642f4 (patch) | |
| tree | 64a68083f9af0a03d4475de9cbd55c963fd48268 | |
| parent | 554cc4d8f7a4bb913a41c85bc2269f25eac2eddd (diff) | |
| parent | 5d9c631462b8f6a46586c8747f54544621a5b22b (diff) | |
Merge "feat(non linear font scaling): add easier-to-find alias functions to TypedValue"
| -rw-r--r-- | core/api/current.txt | 2 | ||||
| -rw-r--r-- | core/java/android/util/TypedValue.java | 42 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/util/TypedValueTest.kt | 8 |
3 files changed, 52 insertions, 0 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index 233be5a325ef..05926b851bfc 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -49373,6 +49373,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 09545fcfa60f..b93e3386b7cc 100644 --- a/core/java/android/util/TypedValue.java +++ b/core/java/android/util/TypedValue.java @@ -495,6 +495,48 @@ public class TypedValue { } /** + * Converts a pixel value to the given dimension, e.g. PX to DP. + * + * <p>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. + * + * <p>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 af01447fc21e..d4e77b505a79 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) } } |