Search in sources :

Example 6 with StaticLayout

use of android.text.StaticLayout in project LuaViewSDK by alibaba.

the class AutofitHelper method getAutofitTextSize.

/**
     * Recursive binary search to find the best size for the text.
     */
private static float getAutofitTextSize(CharSequence text, TextPaint paint, float targetWidth, int maxLines, float low, float high, float precision, DisplayMetrics displayMetrics) {
    float mid = (low + high) / 2.0f;
    int lineCount = 1;
    StaticLayout layout = null;
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid, displayMetrics));
    if (maxLines != 1) {
        layout = new StaticLayout(text, paint, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        lineCount = layout.getLineCount();
    }
    if (SPEW)
        Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid + " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
    if (lineCount > maxLines) {
        // For the case that `text` has more newline characters than `maxLines`.
        if ((high - low) < precision) {
            return low;
        }
        return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
    } else if (lineCount < maxLines) {
        return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
    } else {
        float maxLineWidth = 0;
        if (maxLines == 1) {
            maxLineWidth = paint.measureText(text, 0, text.length());
        } else {
            for (int i = 0; i < lineCount; i++) {
                if (layout.getLineWidth(i) > maxLineWidth) {
                    maxLineWidth = layout.getLineWidth(i);
                }
            }
        }
        if ((high - low) < precision) {
            return low;
        } else if (maxLineWidth > targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision, displayMetrics);
        } else if (maxLineWidth < targetWidth) {
            return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision, displayMetrics);
        } else {
            return mid;
        }
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint)

Example 7 with StaticLayout

use of android.text.StaticLayout in project plaid by nickbutcher.

the class ReflowText method createAnimator.

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;
    final View view = endValues.view;
    AnimatorSet transition = new AnimatorSet();
    ReflowData startData = (ReflowData) startValues.values.get(PROPNAME_DATA);
    ReflowData endData = (ReflowData) endValues.values.get(PROPNAME_DATA);
    duration = calculateDuration(startData.bounds, endData.bounds);
    // create layouts & capture a bitmaps of the text in both states
    // (with max lines variants where needed)
    Layout startLayout = createLayout(startData, sceneRoot.getContext(), false);
    Layout endLayout = createLayout(endData, sceneRoot.getContext(), false);
    Layout startLayoutMaxLines = null;
    Layout endLayoutMaxLines = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // StaticLayout maxLines support
        if (startData.maxLines != -1) {
            startLayoutMaxLines = createLayout(startData, sceneRoot.getContext(), true);
        }
        if (endData.maxLines != -1) {
            endLayoutMaxLines = createLayout(endData, sceneRoot.getContext(), true);
        }
    }
    final Bitmap startText = createBitmap(startData, startLayoutMaxLines != null ? startLayoutMaxLines : startLayout);
    final Bitmap endText = createBitmap(endData, endLayoutMaxLines != null ? endLayoutMaxLines : endLayout);
    // temporarily turn off clipping so we can draw outside of our bounds don't draw
    view.setWillNotDraw(true);
    ((ViewGroup) view.getParent()).setClipChildren(false);
    // calculate the runs of text to move together
    List<Run> runs = getRuns(startData, startLayout, startLayoutMaxLines, endData, endLayout, endLayoutMaxLines);
    // create animators for moving, scaling and fading each run of text
    transition.playTogether(createRunAnimators(view, startData, endData, startText, endText, runs));
    if (!freezeFrame) {
        transition.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                // clean up
                view.setWillNotDraw(false);
                view.getOverlay().clear();
                ((ViewGroup) view.getParent()).setClipChildren(true);
                startText.recycle();
                endText.recycle();
            }
        });
    }
    return transition;
}
Also used : Bitmap(android.graphics.Bitmap) Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) StaticLayout(android.text.StaticLayout) Layout(android.text.Layout) ViewGroup(android.view.ViewGroup) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) AnimatorSet(android.animation.AnimatorSet) View(android.view.View) TextView(android.widget.TextView)

Example 8 with StaticLayout

use of android.text.StaticLayout in project plaid by nickbutcher.

the class DynamicTextView method fitMultiline.

private void fitMultiline() {
    int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    int targetHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    if (targetWidth > 0 && targetHeight > 0) {
        int textSize = mMaxTextSize;
        TextPaint paint = getPaint();
        paint.setTextSize(textSize);
        StaticLayout staticLayout = new StaticLayout(getText(), paint, targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        int currentHeight = staticLayout.getHeight();
        while (currentHeight > targetHeight && textSize > mMinTextSize) {
            textSize--;
            paint.setTextSize(textSize);
            staticLayout = new StaticLayout(getText(), paint, targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
            currentHeight = staticLayout.getHeight();
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
        mCalculated = true;
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 9 with StaticLayout

use of android.text.StaticLayout in project plaid by nickbutcher.

the class DynamicTextView method fitSnappedMultiLine.

private void fitSnappedMultiLine() {
    int targetWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    int targetHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    if (targetWidth > 0 && targetHeight > 0) {
        int style = 0;
        MaterialTypeStyle currentStyle = mStyles[style];
        TextPaint paint = getPaint();
        StaticLayout staticLayout = null;
        int currentHeight = Integer.MAX_VALUE;
        int lines = 0;
        boolean maxLinesSet = getMaxLines() != Integer.MAX_VALUE;
        while ((currentHeight > targetHeight || (maxLinesSet && lines > getMaxLines())) && style <= mStyles.length - 1 && currentStyle.size * scaledDensity >= mMinTextSize && currentStyle.size * scaledDensity <= mMaxTextSize) {
            currentStyle = mStyles[style];
            paint.setTextSize(currentStyle.size * scaledDensity);
            paint.setTypeface(Typeface.create(currentStyle.fontFamily, Typeface.NORMAL));
            staticLayout = new StaticLayout(getText(), paint, targetWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
            currentHeight = staticLayout.getHeight();
            lines = staticLayout.getLineCount();
            style++;
        }
        super.setTextSize(TypedValue.COMPLEX_UNIT_SP, currentStyle.size);
        setTypeface(Typeface.create(currentStyle.fontFamily, Typeface.NORMAL));
        int currentColour = getCurrentTextColor();
        setTextColor(Color.argb(currentStyle.opacity, Color.red(currentColour), Color.green(currentColour), Color.blue(currentColour)));
        if (style == mStyles.length) {
            setEllipsize(TextUtils.TruncateAt.END);
        }
        if (currentStyle.size * scaledDensity < mMinTextSize) {
            // wanted to make text smaller but hit min text size.  Need to set max lines.
            setMaxLines((int) Math.floor((((float) targetHeight / (float) currentHeight) * lines)));
            setEllipsize(TextUtils.TruncateAt.END);
        }
        setTextAlignment(TEXT_ALIGNMENT_TEXT_START);
        mCalculated = true;
    }
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 10 with StaticLayout

use of android.text.StaticLayout in project MaterialEditText by rengwuxian.

the class MaterialAutoCompleteTextView method adjustBottomLines.

/**
   * @return True, if adjustments were made that require the view to be invalidated.
   */
private boolean adjustBottomLines() {
    // Bail out if we have a zero width; lines will be adjusted during next layout.
    if (getWidth() == 0) {
        return false;
    }
    int destBottomLines;
    textPaint.setTextSize(bottomTextSize);
    if (tempErrorText != null || helperText != null) {
        Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ? Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
        textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
        destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
    } else {
        destBottomLines = minBottomLines;
    }
    if (bottomLines != destBottomLines) {
        getBottomLinesAnimator(destBottomLines).start();
    }
    bottomLines = destBottomLines;
    return true;
}
Also used : StaticLayout(android.text.StaticLayout) Layout(android.text.Layout) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

StaticLayout (android.text.StaticLayout)80 TextPaint (android.text.TextPaint)56 Paint (android.graphics.Paint)45 Layout (android.text.Layout)21 DynamicLayout (android.text.DynamicLayout)9 RectF (android.graphics.RectF)6 Bitmap (android.graphics.Bitmap)3 NonNull (android.support.annotation.NonNull)3 BoringLayout (android.text.BoringLayout)3 SpannableStringBuilder (android.text.SpannableStringBuilder)3 KeyEvent (android.view.KeyEvent)3 MotionEvent (android.view.MotionEvent)3 AccessibilityEvent (android.view.accessibility.AccessibilityEvent)3 SuppressLint (android.annotation.SuppressLint)2 Canvas (android.graphics.Canvas)2 Rect (android.graphics.Rect)2 Spanned (android.text.Spanned)2 TruncateAt (android.text.TextUtils.TruncateAt)2 StyleSpan (android.text.style.StyleSpan)2 Animator (android.animation.Animator)1