Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Make letter spacing dependent on text size in CollapsingTextHelper #4640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -696,17 +696,6 @@ private void calculateOffsets(final float fraction) {
textPaint.setColor(getCurrentCollapsedTextColor());
}

if (collapsedLetterSpacing != expandedLetterSpacing) {
textPaint.setLetterSpacing(
lerp(
expandedLetterSpacing,
collapsedLetterSpacing,
fraction,
AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
} else {
textPaint.setLetterSpacing(collapsedLetterSpacing);
}

// Calculates paint parameters for shadow layer.
currentShadowRadius = lerp(expandedShadowRadius, collapsedShadowRadius, fraction, null);
currentShadowDx = lerp(expandedShadowDx, collapsedShadowDx, fraction, null);
Expand Down Expand Up @@ -1068,7 +1057,6 @@ private void calculateUsingTextSize(final float fraction, boolean forceRecalcula
newTypeface = collapsedTypeface;
} else {
newTextSize = expandedTextSize;
newLetterSpacing = expandedLetterSpacing;
newTypeface = expandedTypeface;
if (isClose(fraction, /* targetValue= */ 0)) {
// If we're close to the expanded text size, snap to it and use a scale of 1
Expand All @@ -1080,6 +1068,11 @@ private void calculateUsingTextSize(final float fraction, boolean forceRecalcula
/ expandedTextSize;
}

newLetterSpacing = lerp(
expandedLetterSpacing, collapsedLetterSpacing,
1f, collapsedTextSize / expandedTextSize,
scale);

float textSizeRatio = collapsedTextSize / expandedTextSize;
// This is the size of the expanded bounds when it is scaled to match the
// collapsed text size
Expand Down Expand Up @@ -1289,11 +1282,11 @@ public void setStaticLayoutBuilderConfigurer(
}

/**
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
* Returns true if {@code value1} is 'close' to {@code value2}. Close is currently
* defined as it's difference being < 0.00001.
*/
private static boolean isClose(float value, float targetValue) {
return Math.abs(value - targetValue) < 0.00001f;
private static boolean isClose(float value1, float value2) {
return Math.abs(value1 - value2) < 0.00001f;
}

public ColorStateList getExpandedTextColor() {
Expand Down Expand Up @@ -1336,6 +1329,22 @@ private static float lerp(
return AnimationUtils.lerp(startValue, endValue, fraction);
}

private static float lerp(
float outputStart, float outputEnd,
float inputStart, float inputEnd,
float inputValue) {
if (isClose(inputEnd, inputStart)) {
if (isClose(outputEnd, outputStart)) {
return outputStart;
} else {
throw new RuntimeException("\"input\" range is empty, but \"output\" is not");
}
}

float value = (inputValue - inputStart) / (inputEnd - inputStart);
return outputStart + (outputEnd - outputStart) * value;
}

private static boolean rectEquals(@NonNull Rect r, int left, int top, int right, int bottom) {
return !(r.left != left || r.top != top || r.right != right || r.bottom != bottom);
}
Expand Down