From 44d4c3ea3fa6d81f127c6d92ed76df232afb45cc Mon Sep 17 00:00:00 2001 From: conradchen Date: Wed, 10 Nov 2021 13:10:44 -0500 Subject: [PATCH] [TopAppBar] Correctly set letter spacing when expanding titles We recreated title text layout when expanding from the collapsed state but didn't correctly set the letter spacing to the expanded one. The letter spacing being used will be a interpolated value between collapsed and expanded letter spacing (close to the collapsed one due to the implementation). This will cause the text layout incorrectly calculate the line count from the wrong letter spacing and result in a broken layout. Correctly sets the letter spacing to fix the issue. Resolves https://github.com/material-components/material-components-android/issues/2463 PiperOrigin-RevId: 408910555 --- .../material/internal/CollapsingTextHelper.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java index 6d171a24130..90e82e524fd 100644 --- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java +++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java @@ -153,6 +153,7 @@ public final class CollapsingTextHelper { private float collapsedLetterSpacing; private float expandedLetterSpacing; + private float currentLetterSpacing; private StaticLayout textLayout; private float collapsedTextWidth; @@ -907,10 +908,12 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula float availableWidth; float newTextSize; + float newLetterSpacing; boolean updateDrawText = false; if (isClose(textSize, collapsedTextSize)) { newTextSize = collapsedTextSize; + newLetterSpacing = collapsedLetterSpacing; scale = 1f; if (currentTypeface != collapsedTypeface) { currentTypeface = collapsedTypeface; @@ -919,6 +922,7 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula availableWidth = collapsedWidth; } else { newTextSize = expandedTextSize; + newLetterSpacing = expandedLetterSpacing; if (currentTypeface != expandedTypeface) { currentTypeface = expandedTypeface; updateDrawText = true; @@ -953,14 +957,20 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula } if (availableWidth > 0) { - updateDrawText = (currentTextSize != newTextSize) || boundsChanged || updateDrawText; + boolean textSizeChanged = currentTextSize != newTextSize; + boolean letterSpacingChanged = currentLetterSpacing != newLetterSpacing; + updateDrawText = textSizeChanged || letterSpacingChanged || boundsChanged || updateDrawText; currentTextSize = newTextSize; + currentLetterSpacing = newLetterSpacing; boundsChanged = false; } if (textToDraw == null || updateDrawText) { textPaint.setTextSize(currentTextSize); textPaint.setTypeface(currentTypeface); + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + textPaint.setLetterSpacing(currentLetterSpacing); + } // Use linear text scaling if we're scaling the canvas textPaint.setLinearText(scale != 1f);