From db86a6b047dda704a1b8a6ff2d5b28b00febd444 Mon Sep 17 00:00:00 2001 From: Siyamed Sinir Date: Thu, 15 Mar 2018 18:00:07 -0700 Subject: [PATCH] Fix rounding error related to autoSize setupAutoSize fills in the possible text sizes that can be generated between a min and max value. In order to do that, it counts the number of steps starting from minSize until maxSize. However, while counting it rounds the initial value, which causes rounding error at the final step. Test: Change system font scale to 1.1 via adb shell settings put system font_scale 1.1 Test: atest android.widget.cts.TextViewTest Bug: 73917559 Bug: 75266270 Change-Id: I61811db28ef01262bd48f5042d783d75c71c3614 --- core/java/android/widget/TextView.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index d358c95ba09bf..1714d26dc5986 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -1917,19 +1917,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // Calculate the sizes set based on minimum size, maximum size and step size if we do // not have a predefined set of sizes or if the current sizes array is empty. if (!mHasPresetAutoSizeValues || mAutoSizeTextSizesInPx.length == 0) { - int autoSizeValuesLength = 1; - float currentSize = Math.round(mAutoSizeMinTextSizeInPx); - while (Math.round(currentSize + mAutoSizeStepGranularityInPx) - <= Math.round(mAutoSizeMaxTextSizeInPx)) { - autoSizeValuesLength++; - currentSize += mAutoSizeStepGranularityInPx; - } - - int[] autoSizeTextSizesInPx = new int[autoSizeValuesLength]; - float sizeToAdd = mAutoSizeMinTextSizeInPx; + final int autoSizeValuesLength = ((int) Math.floor((mAutoSizeMaxTextSizeInPx + - mAutoSizeMinTextSizeInPx) / mAutoSizeStepGranularityInPx)) + 1; + final int[] autoSizeTextSizesInPx = new int[autoSizeValuesLength]; for (int i = 0; i < autoSizeValuesLength; i++) { - autoSizeTextSizesInPx[i] = Math.round(sizeToAdd); - sizeToAdd += mAutoSizeStepGranularityInPx; + autoSizeTextSizesInPx[i] = Math.round( + mAutoSizeMinTextSizeInPx + (i * mAutoSizeStepGranularityInPx)); } mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(autoSizeTextSizesInPx); }