Merge "fix(non linear font scaling): add a setLineHeight() that accepts explicit units." into udc-dev

This commit is contained in:
Tyler Freeman
2023-03-29 03:50:45 +00:00
committed by Android (Google) Code Review
2 changed files with 34 additions and 3 deletions

View File

@@ -60403,6 +60403,7 @@ package android.widget {
method public void setLineBreakStyle(int);
method public void setLineBreakWordStyle(int);
method public void setLineHeight(@IntRange(from=0) @Px int);
method public void setLineHeight(int, @FloatRange(from=0) float);
method public void setLineSpacing(float, float);
method public void setLines(int);
method public final void setLinkTextColor(@ColorInt int);

View File

@@ -4604,7 +4604,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
}
private void setTextSizeInternal(int unit, float size, boolean shouldRequestLayout) {
@NonNull
private DisplayMetrics getDisplayMetricsOrSystem() {
Context c = getContext();
Resources r;
@@ -4614,8 +4615,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
r = c.getResources();
}
return r.getDisplayMetrics();
}
private void setTextSizeInternal(int unit, float size, boolean shouldRequestLayout) {
mTextSizeUnit = unit;
setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()),
setRawTextSize(TypedValue.applyDimension(unit, size, getDisplayMetricsOrSystem()),
shouldRequestLayout);
}
@@ -6197,16 +6202,41 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
*/
@android.view.RemotableViewMethod
public void setLineHeight(@Px @IntRange(from = 0) int lineHeight) {
Preconditions.checkArgumentNonnegative(lineHeight);
setLineHeightPx(lineHeight);
}
private void setLineHeightPx(@Px @FloatRange(from = 0) float lineHeight) {
Preconditions.checkArgumentNonnegative((int) lineHeight);
final int fontHeight = getPaint().getFontMetricsInt(null);
// Make sure we don't setLineSpacing if it's not needed to avoid unnecessary redraw.
// TODO(b/274974975): should this also check if lineSpacing needs to change?
if (lineHeight != fontHeight) {
// Set lineSpacingExtra by the difference of lineSpacing with lineHeight
setLineSpacing(lineHeight - fontHeight, 1f);
}
}
/**
* Sets an explicit line height to a given unit and value for this TextView. This is equivalent
* to the vertical distance between subsequent baselines in the TextView. See {@link
* TypedValue} for the possible dimension units.
*
* @param unit The desired dimension unit. SP units are strongly recommended so that line height
* stays proportional to the text size when fonts are scaled up for accessibility.
* @param lineHeight The desired line height in the given units.
*
* @see #setLineSpacing(float, float)
* @see #getLineSpacingExtra()
*
* @attr ref android.R.styleable#TextView_lineHeight
*/
@android.view.RemotableViewMethod
public void setLineHeight(int unit, @FloatRange(from = 0) float lineHeight) {
setLineHeightPx(
TypedValue.applyDimension(unit, lineHeight, getDisplayMetricsOrSystem()));
}
/**
* Set Highlights
*