AutoSize TextView (part 4) - autosize when layout changes
Whenever TextView#onMeasure(...) gets called make sure to also autosize is enabled. Also fixes the available space calculation by taking the drawables padding into consideration too. Bug: 32221168 Bug: 33573642 Test: attached in topic Change-Id: I1a01454f3863e51afd7e16cfa27f544db6ca0783
This commit is contained in:
@@ -680,8 +680,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
private static final int DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX = 1;
|
||||
// Contains the sorted set of desired text sizes in pixels to pick from when auto-sizing text.
|
||||
private int[] mAutoSizeTextSizesInPx;
|
||||
// Specifies if the current TextView needs to be auto-sized.
|
||||
private boolean mNeedsTextAutoResize = false;
|
||||
|
||||
/**
|
||||
* Kick-start the font cache for the zygote process (to pay the cost of
|
||||
@@ -1561,7 +1559,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
|
||||
Arrays.sort(mAutoSizeTextSizesInPx);
|
||||
mNeedsTextAutoResize = true;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException(
|
||||
@@ -7524,7 +7521,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
scrollTo(0, 0);
|
||||
}
|
||||
|
||||
if (mNeedsTextAutoResize) {
|
||||
if (isAutoSizeEnabled()) {
|
||||
// Call auto-size after the width and height have been calculated.
|
||||
autoSizeText();
|
||||
}
|
||||
@@ -7536,20 +7533,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
* Automatically computes and sets the text size.
|
||||
*/
|
||||
private void autoSizeText() {
|
||||
final int maxWidth = getMeasuredWidth() - getTotalPaddingLeft() - getTotalPaddingRight();
|
||||
final int maxHeight = getMeasuredHeight() - getTotalPaddingBottom() - getTotalPaddingTop();
|
||||
|
||||
if (maxWidth <= 0 || maxHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (TEMP_RECTF) {
|
||||
TEMP_RECTF.setEmpty();
|
||||
final int maxWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
|
||||
final int maxHeight = getMeasuredHeight() - getPaddingBottom() - getPaddingTop();
|
||||
|
||||
if (maxWidth <= 0 || maxHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
TEMP_RECTF.right = maxWidth;
|
||||
TEMP_RECTF.bottom = maxHeight;
|
||||
final float textSize = findLargestTextSizeWhichFits(TEMP_RECTF);
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
||||
mNeedsTextAutoResize = false;
|
||||
if (textSize != getTextSize()) {
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7594,13 +7592,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
|
||||
if ((mLayout instanceof BoringLayout) && BoringLayout.isBoring(
|
||||
text, mTempTextPaint, getTextDirectionHeuristic(), mBoring) != null) {
|
||||
return mTempTextPaint.getFontSpacing() + getPaddingTop() + getPaddingBottom()
|
||||
<= availableSpace.bottom
|
||||
&& mTempTextPaint.measureText(text, 0, text.length())
|
||||
+ getPaddingLeft() + getPaddingRight() <= availableSpace.right;
|
||||
return mTempTextPaint.getFontSpacing() <= availableSpace.bottom
|
||||
&& mTempTextPaint.measureText(text, 0, text.length()) <= availableSpace.right;
|
||||
} else {
|
||||
StaticLayout.Builder layoutBuilder = StaticLayout.Builder.obtain(text, 0, text.length(),
|
||||
mTempTextPaint, getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
|
||||
mTempTextPaint,
|
||||
getMeasuredWidth() - getTotalPaddingLeft() - getTotalPaddingRight());
|
||||
layoutBuilder.setAlignment(getLayoutAlignment());
|
||||
layoutBuilder.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier());
|
||||
layoutBuilder.setIncludePad(true);
|
||||
@@ -9270,7 +9267,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@code true} if this TextView supports autosizing text to fit within its container.
|
||||
* @return {@code true} if this widget supports auto-sizing text and has been configured to
|
||||
* auto-size.
|
||||
*/
|
||||
private boolean isAutoSizeEnabled() {
|
||||
return supportsAutoSizeText() && mAutoSizeType != AUTO_SIZE_TYPE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {@code true} if this TextView supports auto-sizing text to fit within its container.
|
||||
* @hide
|
||||
*/
|
||||
protected boolean supportsAutoSizeText() {
|
||||
|
||||
Reference in New Issue
Block a user