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
(cherry picked from commit db86a6b047)
This commit is contained in:
@@ -1918,19 +1918,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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user