Skip to content

Commit

Permalink
[TextField] Correctly calculate hint text offset
Browse files Browse the repository at this point in the history
We changes letter spacing during expanding/collapsing animations, which causes us use letter spacing during animation while calculating final expanded/collapsed offset instead of final letter spacing. This in turn causes hints be drawn at the wrong positions. Uses final letter spacing during calculating offset instead to fix the problem.

Resolves #2277

PiperOrigin-RevId: 408904991
  • Loading branch information
drchen committed Nov 10, 2021
1 parent f898ba8 commit 34cce18
Showing 1 changed file with 17 additions and 14 deletions.
Expand Up @@ -665,12 +665,8 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
TextUtils.ellipsize(textToDraw, textPaint, textLayout.getWidth(), TruncateAt.END);
}
if (textToDrawCollapsed != null) {
TextPaint collapsedTextPaint = new TextPaint(textPaint);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
collapsedTextPaint.setLetterSpacing(collapsedLetterSpacing);
}
collapsedTextWidth =
collapsedTextPaint.measureText(textToDrawCollapsed, 0, textToDrawCollapsed.length());
getTextPaintCollapsed(tmpPaint);
collapsedTextWidth = measureTextWidth(tmpPaint, textToDrawCollapsed);
} else {
collapsedTextWidth = 0;
}
Expand Down Expand Up @@ -708,12 +704,15 @@ private void calculateBaseOffsets(boolean forceRecalculate) {

calculateUsingTextSize(expandedTextSize, forceRecalculate);
float expandedTextHeight = textLayout != null ? textLayout.getHeight() : 0;
float expandedTextWidth = 0;
if (textLayout != null && maxLines > 1) {
expandedTextWidth = textLayout.getWidth();
} else if (textToDraw != null) {
getTextPaintExpanded(tmpPaint);
expandedTextWidth = measureTextWidth(tmpPaint, textToDraw);
}
expandedLineCount = textLayout != null ? textLayout.getLineCount() : 0;

float measuredWidth = textToDraw != null
? textPaint.measureText(textToDraw, 0, textToDraw.length()) : 0;
float width = textLayout != null && maxLines > 1 ? textLayout.getWidth() : measuredWidth;

final int expandedAbsGravity =
GravityCompat.getAbsoluteGravity(
expandedTextGravity,
Expand All @@ -734,10 +733,10 @@ private void calculateBaseOffsets(boolean forceRecalculate) {

switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
expandedDrawX = expandedBounds.centerX() - (width / 2);
expandedDrawX = expandedBounds.centerX() - (expandedTextWidth / 2);
break;
case Gravity.RIGHT:
expandedDrawX = expandedBounds.right - width;
expandedDrawX = expandedBounds.right - expandedTextWidth;
break;
case Gravity.LEFT:
default:
Expand All @@ -751,6 +750,10 @@ private void calculateBaseOffsets(boolean forceRecalculate) {
setInterpolatedTextSize(currentTextSize);
}

private float measureTextWidth(TextPaint textPaint, CharSequence textToDraw) {
return textPaint.measureText(textToDraw, 0, textToDraw.length());
}

private void interpolateBounds(float fraction) {
if (fadeModeEnabled) {
currentBounds.set(fraction < fadeModeThresholdFraction ? expandedBounds : collapsedBounds);
Expand Down Expand Up @@ -787,9 +790,9 @@ public void draw(@NonNull Canvas canvas) {
if (DEBUG_DRAW) {
// Just a debug tool, which drawn a magenta rect in the text bounds
canvas.drawRect(
currentBounds.left,
x,
y,
currentBounds.right,
x + textLayout.getWidth() * scale,
y + textLayout.getHeight() * scale,
DEBUG_DRAW_PAINT);
}
Expand Down

0 comments on commit 34cce18

Please sign in to comment.