Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79ada301 authored by Andrei Stingaceanu's avatar Andrei Stingaceanu Committed by Android (Google) Code Review
Browse files

Merge "AutoSize TextView (part 4) - autosize when layout changes"

parents 3c9c96d8 7c8ba07e
Loading
Loading
Loading
Loading
+24 −19
Original line number Original line Diff line number Diff line
@@ -681,8 +681,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    private static final int DEFAULT_AUTO_SIZE_GRANULARITY_IN_PX = 1;
    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.
    // Contains the sorted set of desired text sizes in pixels to pick from when auto-sizing text.
    private int[] mAutoSizeTextSizesInPx;
    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
     * Kick-start the font cache for the zygote process (to pay the cost of
@@ -1562,7 +1560,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                    }
                    }


                    Arrays.sort(mAutoSizeTextSizesInPx);
                    Arrays.sort(mAutoSizeTextSizesInPx);
                    mNeedsTextAutoResize = true;
                    break;
                    break;
                default:
                default:
                    throw new IllegalArgumentException(
                    throw new IllegalArgumentException(
@@ -7525,7 +7522,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            scrollTo(0, 0);
            scrollTo(0, 0);
        }
        }


        if (mNeedsTextAutoResize) {
        if (isAutoSizeEnabled()) {
            // Call auto-size after the width and height have been calculated.
            // Call auto-size after the width and height have been calculated.
            autoSizeText();
            autoSizeText();
        }
        }
@@ -7537,20 +7534,21 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     * Automatically computes and sets the text size.
     * Automatically computes and sets the text size.
     */
     */
    private void autoSizeText() {
    private void autoSizeText() {
        synchronized (TEMP_RECTF) {
        final int maxWidth = getMeasuredWidth() - getTotalPaddingLeft() - getTotalPaddingRight();
            TEMP_RECTF.setEmpty();
        final int maxHeight = getMeasuredHeight() - getTotalPaddingBottom() - getTotalPaddingTop();
            final int maxWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
            final int maxHeight = getMeasuredHeight() - getPaddingBottom() - getPaddingTop();


        if (maxWidth <= 0 || maxHeight <= 0) {
        if (maxWidth <= 0 || maxHeight <= 0) {
            return;
            return;
        }
        }


        synchronized (TEMP_RECTF) {
            TEMP_RECTF.setEmpty();
            TEMP_RECTF.right = maxWidth;
            TEMP_RECTF.right = maxWidth;
            TEMP_RECTF.bottom = maxHeight;
            TEMP_RECTF.bottom = maxHeight;
            final float textSize = findLargestTextSizeWhichFits(TEMP_RECTF);
            final float textSize = findLargestTextSizeWhichFits(TEMP_RECTF);
            if (textSize != getTextSize()) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            mNeedsTextAutoResize = false;
            }
        }
        }
    }
    }


@@ -7595,13 +7593,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener


        if ((mLayout instanceof BoringLayout) && BoringLayout.isBoring(
        if ((mLayout instanceof BoringLayout) && BoringLayout.isBoring(
                text, mTempTextPaint, getTextDirectionHeuristic(), mBoring) != null) {
                text, mTempTextPaint, getTextDirectionHeuristic(), mBoring) != null) {
            return mTempTextPaint.getFontSpacing() + getPaddingTop() + getPaddingBottom()
            return mTempTextPaint.getFontSpacing() <= availableSpace.bottom
                    <= availableSpace.bottom
                    && mTempTextPaint.measureText(text, 0, text.length()) <= availableSpace.right;
                    && mTempTextPaint.measureText(text, 0, text.length())
                    + getPaddingLeft() + getPaddingRight() <= availableSpace.right;
        } else {
        } else {
            StaticLayout.Builder layoutBuilder = StaticLayout.Builder.obtain(text, 0, text.length(),
            StaticLayout.Builder layoutBuilder = StaticLayout.Builder.obtain(text, 0, text.length(),
                    mTempTextPaint, getMeasuredWidth() - getPaddingLeft() - getPaddingRight());
                    mTempTextPaint,
                    getMeasuredWidth() - getTotalPaddingLeft() - getTotalPaddingRight());
            layoutBuilder.setAlignment(getLayoutAlignment());
            layoutBuilder.setAlignment(getLayoutAlignment());
            layoutBuilder.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier());
            layoutBuilder.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier());
            layoutBuilder.setIncludePad(true);
            layoutBuilder.setIncludePad(true);
@@ -9271,7 +9268,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
     * @hide
     */
     */
    protected boolean supportsAutoSizeText() {
    protected boolean supportsAutoSizeText() {