Skip to content

Commit 89d80d0

Browse files
drchenpaulfthomas
authored andcommitted
[TopAppBar] Fix expanded title margin not applied
In the original logic we compares interpolated text size to decide if the available width should be the collapsed one or the expanded one. This logic wouldn't work if the collapsed text size and the expanded text size are the same. Refactors the logic to make the decision by interpolating fraction directly. Resolves #2459 PiperOrigin-RevId: 410901171
1 parent 1d50ed7 commit 89d80d0

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

lib/java/com/google/android/material/internal/CollapsingTextHelper.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -599,21 +599,20 @@ private void calculateOffsets(final float fraction) {
599599
currentDrawX = expandedDrawX;
600600
currentDrawY = expandedDrawY;
601601

602-
setInterpolatedTextSize(expandedTextSize);
602+
setInterpolatedTextSize(/* fraction= */ 0);
603603
} else {
604604
textBlendFraction = 1F;
605605
currentDrawX = collapsedDrawX;
606606
currentDrawY = collapsedDrawY - max(0, currentOffsetY);
607607

608-
setInterpolatedTextSize(collapsedTextSize);
608+
setInterpolatedTextSize(/* fraction= */ 1);
609609
}
610610
} else {
611611
textBlendFraction = fraction;
612612
currentDrawX = lerp(expandedDrawX, collapsedDrawX, fraction, positionInterpolator);
613613
currentDrawY = lerp(expandedDrawY, collapsedDrawY, fraction, positionInterpolator);
614614

615-
setInterpolatedTextSize(
616-
lerp(expandedTextSize, collapsedTextSize, fraction, textSizeInterpolator));
615+
setInterpolatedTextSize(fraction);
617616
}
618617

619618
setCollapsedTextBlend(
@@ -703,10 +702,8 @@ private int getCurrentColor(@Nullable ColorStateList colorStateList) {
703702
}
704703

705704
private void calculateBaseOffsets(boolean forceRecalculate) {
706-
final float currentTextSize = this.currentTextSize;
707-
708705
// We then calculate the collapsed text size, using the same logic
709-
calculateUsingTextSize(collapsedTextSize, forceRecalculate);
706+
calculateUsingTextSize(/* fraction= */ 1, forceRecalculate);
710707
if (textToDraw != null && textLayout != null) {
711708
textToDrawCollapsed =
712709
TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), TruncateAt.END);
@@ -748,7 +745,7 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
748745
break;
749746
}
750747

751-
calculateUsingTextSize(expandedTextSize, forceRecalculate);
748+
calculateUsingTextSize(/* fraction= */ 0, forceRecalculate);
752749
float expandedTextHeight = textLayout != null ? textLayout.getHeight() : 0;
753750
float expandedTextWidth = 0;
754751
if (textLayout != null && maxLines > 1) {
@@ -792,7 +789,7 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
792789
// The bounds have changed so we need to clear the texture
793790
clearTexture();
794791
// Now reset the text size back to the original
795-
setInterpolatedTextSize(currentTextSize);
792+
setInterpolatedTextSize(expandedFraction);
796793
}
797794

798795
private float measureTextWidth(TextPaint textPaint, CharSequence textToDraw) {
@@ -923,8 +920,8 @@ private boolean isTextDirectionHeuristicsIsRtl(@NonNull CharSequence text, boole
923920
.isRtl(text, 0, text.length());
924921
}
925922

926-
private void setInterpolatedTextSize(float textSize) {
927-
calculateUsingTextSize(textSize);
923+
private void setInterpolatedTextSize(float fraction) {
924+
calculateUsingTextSize(fraction);
928925

929926
// Use our texture if the scale isn't 1.0
930927
useTexture = USE_SCALING_TEXTURE && scale != 1f;
@@ -937,12 +934,12 @@ private void setInterpolatedTextSize(float textSize) {
937934
ViewCompat.postInvalidateOnAnimation(view);
938935
}
939936

940-
private void calculateUsingTextSize(final float textSize) {
941-
calculateUsingTextSize(textSize, /* forceRecalculate= */ false);
937+
private void calculateUsingTextSize(final float fraction) {
938+
calculateUsingTextSize(fraction, /* forceRecalculate= */ false);
942939
}
943940

944941
@SuppressWarnings("ReferenceEquality") // Matches the Typeface comparison in TextView
945-
private void calculateUsingTextSize(final float textSize, boolean forceRecalculate) {
942+
private void calculateUsingTextSize(final float fraction, boolean forceRecalculate) {
946943
if (text == null) {
947944
return;
948945
}
@@ -955,7 +952,7 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
955952
float newLetterSpacing;
956953
boolean updateDrawText = false;
957954

958-
if (isClose(textSize, collapsedTextSize)) {
955+
if (isClose(fraction, /* targetValue= */ 1)) {
959956
newTextSize = collapsedTextSize;
960957
newLetterSpacing = collapsedLetterSpacing;
961958
scale = 1f;
@@ -971,12 +968,14 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
971968
currentTypeface = expandedTypeface;
972969
updateDrawText = true;
973970
}
974-
if (isClose(textSize, expandedTextSize)) {
971+
if (isClose(fraction, /* targetValue= */ 0)) {
975972
// If we're close to the expanded text size, snap to it and use a scale of 1
976973
scale = 1f;
977974
} else {
978975
// Else, we'll scale down from the expanded text size
979-
scale = textSize / expandedTextSize;
976+
scale =
977+
lerp(expandedTextSize, collapsedTextSize, fraction, textSizeInterpolator)
978+
/ expandedTextSize;
980979
}
981980

982981
float textSizeRatio = collapsedTextSize / expandedTextSize;
@@ -1185,10 +1184,10 @@ public int getHyphenationFrequency() {
11851184

11861185
/**
11871186
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
1188-
* defined as it's difference being < 0.001.
1187+
* defined as it's difference being < 0.00001.
11891188
*/
11901189
private static boolean isClose(float value, float targetValue) {
1191-
return Math.abs(value - targetValue) < 0.001f;
1190+
return Math.abs(value - targetValue) < 0.00001f;
11921191
}
11931192

11941193
public ColorStateList getExpandedTextColor() {

0 commit comments

Comments
 (0)