Skip to content

Commit 9fae0ca

Browse files
committed
[Internal] Make letter spacing dependent on text size in CollapsingTextHelper
1 parent 8ec6b77 commit 9fae0ca

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -712,17 +712,6 @@ private void calculateOffsets(final float fraction) {
712712
textPaint.setColor(getCurrentCollapsedTextColor());
713713
}
714714

715-
if (collapsedLetterSpacing != expandedLetterSpacing) {
716-
textPaint.setLetterSpacing(
717-
lerp(
718-
expandedLetterSpacing,
719-
collapsedLetterSpacing,
720-
fraction,
721-
AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
722-
} else {
723-
textPaint.setLetterSpacing(collapsedLetterSpacing);
724-
}
725-
726715
// Calculates paint parameters for shadow layer.
727716
currentShadowRadius = lerp(expandedShadowRadius, collapsedShadowRadius, fraction, null);
728717
currentShadowDx = lerp(expandedShadowDx, collapsedShadowDx, fraction, null);
@@ -1099,7 +1088,6 @@ private void calculateUsingTextSize(final float fraction, boolean forceRecalcula
10991088
newTypeface = collapsedTypeface;
11001089
} else {
11011090
newTextSize = expandedTextSize;
1102-
newLetterSpacing = expandedLetterSpacing;
11031091
newTypeface = expandedTypeface;
11041092
if (isClose(fraction, /* targetValue= */ 0)) {
11051093
// If we're close to the expanded text size, snap to it and use a scale of 1
@@ -1111,6 +1099,11 @@ private void calculateUsingTextSize(final float fraction, boolean forceRecalcula
11111099
/ expandedTextSize;
11121100
}
11131101

1102+
newLetterSpacing = lerp(
1103+
expandedLetterSpacing, collapsedLetterSpacing,
1104+
1f, collapsedTextSize / expandedTextSize,
1105+
scale);
1106+
11141107
float textSizeRatio = collapsedTextSize / expandedTextSize;
11151108
// This is the size of the expanded bounds when it is scaled to match the
11161109
// collapsed text size
@@ -1320,11 +1313,11 @@ public void setStaticLayoutBuilderConfigurer(
13201313
}
13211314

13221315
/**
1323-
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
1316+
* Returns true if {@code value1} is 'close' to {@code value2}. Close is currently
13241317
* defined as it's difference being < 0.00001.
13251318
*/
1326-
private static boolean isClose(float value, float targetValue) {
1327-
return Math.abs(value - targetValue) < 0.00001f;
1319+
private static boolean isClose(float value1, float value2) {
1320+
return Math.abs(value1 - value2) < 0.00001f;
13281321
}
13291322

13301323
public ColorStateList getExpandedTextColor() {
@@ -1367,6 +1360,22 @@ private static float lerp(
13671360
return AnimationUtils.lerp(startValue, endValue, fraction);
13681361
}
13691362

1363+
private static float lerp(
1364+
float outputStart, float outputEnd,
1365+
float inputStart, float inputEnd,
1366+
float inputValue) {
1367+
if (isClose(inputEnd, inputStart)) {
1368+
if (isClose(outputEnd, outputStart)) {
1369+
return outputStart;
1370+
} else {
1371+
throw new RuntimeException("\"input\" range is empty, but \"output\" is not");
1372+
}
1373+
}
1374+
1375+
float value = (inputValue - inputStart) / (inputEnd - inputStart);
1376+
return outputStart + (outputEnd - outputStart) * value;
1377+
}
1378+
13701379
private static boolean rectEquals(@NonNull Rect r, int left, int top, int right, int bottom) {
13711380
return !(r.left != left || r.top != top || r.right != right || r.bottom != bottom);
13721381
}

0 commit comments

Comments
 (0)