Search in sources :

Example 61 with StaticLayout

use of android.text.StaticLayout in project Notes by Elder-Wu.

the class CountDownView method init.

private void init() {
    circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setDither(true);
    circlePaint.setColor(backgroundColor);
    circlePaint.setStyle(Paint.Style.FILL);
    textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setDither(true);
    textPaint.setColor(textColor);
    textPaint.setTextSize(textSize);
    textPaint.setTextAlign(Paint.Align.CENTER);
    borderPaint = new Paint();
    borderPaint.setAntiAlias(true);
    borderPaint.setDither(true);
    borderPaint.setColor(borderColor);
    borderPaint.setStrokeWidth(borderWidth);
    borderPaint.setStyle(Paint.Style.STROKE);
    int textWidth = (int) textPaint.measureText(text.substring(0, (text.length() + 1) / 2));
    staticLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TextPaint(android.text.TextPaint)

Example 62 with StaticLayout

use of android.text.StaticLayout in project WordPress-Android by wordpress-mobile.

the class AutoResizeTextView method resizeText.

/**
     * Resize the text size with specified width and height
     * @param width
     * @param height
     */
public void resizeText(int width, int height) {
    CharSequence text = getText();
    // Do not resize if the view does not have dimensions or there is no text
    if (text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) {
        return;
    }
    // Get the text view's paint object
    TextPaint textPaint = getPaint();
    // Store the current text size
    float oldTextSize = textPaint.getTextSize();
    // If there is a max text size set, use the lesser of that and the default text size
    float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize;
    // Get the required text height
    int textHeight = getTextHeight(text, textPaint, width, targetTextSize);
    // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes
    while (textHeight > height && targetTextSize > mMinTextSize) {
        targetTextSize = Math.max(targetTextSize - 2, mMinTextSize);
        textHeight = getTextHeight(text, textPaint, width, targetTextSize);
    }
    // If we had reached our minimum text size and still don't fit, append an ellipsis
    if (mAddEllipsis && targetTextSize == mMinTextSize && textHeight > height) {
        // Draw using a static layout
        // modified: use a copy of TextPaint for measuring
        TextPaint paint = new TextPaint(textPaint);
        // Draw using a static layout
        StaticLayout layout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false);
        // Check that we have a least one line of rendered text
        if (layout.getLineCount() > 0) {
            // Since the line at the specific vertical position would be cut off,
            // we must trim up to the previous line
            int lastLine = layout.getLineForVertical(height) - 1;
            // If the text would not even fit on a single line, clear it
            if (lastLine < 0) {
                setText("");
            } else {
                // Otherwise, trim to the previous line and add an ellipsis
                int start = layout.getLineStart(lastLine);
                int end = layout.getLineEnd(lastLine);
                float lineWidth = layout.getLineWidth(lastLine);
                float ellipseWidth = paint.measureText(M_ELLIPSIS);
                // Trim characters off until we have enough room to draw the ellipsis
                while (width < lineWidth + ellipseWidth) {
                    lineWidth = paint.measureText(text.subSequence(start, --end + 1).toString());
                }
                setText(text.subSequence(0, end) + M_ELLIPSIS);
            }
        }
    }
    // Some devices try to auto adjust line spacing, so force default line spacing
    // and invalidate the layout as a side effect
    setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
    setLineSpacing(mSpacingAdd, mSpacingMult);
    // Notify the listener if registered
    if (mTextResizeListener != null) {
        mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize);
    }
    // Reset force resize flag
    mNeedsResize = false;
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 63 with StaticLayout

use of android.text.StaticLayout in project XobotOS by xamarin.

the class TextView method makeNewLayout.

/**
     * The width passed in is now the desired layout width,
     * not the full view width with padding.
     * {@hide}
     */
protected void makeNewLayout(int wantWidth, int hintWidth, BoringLayout.Metrics boring, BoringLayout.Metrics hintBoring, int ellipsisWidth, boolean bringIntoView) {
    stopMarquee();
    // Update "old" cached values
    mOldMaximum = mMaximum;
    mOldMaxMode = mMaxMode;
    mHighlightPathBogus = true;
    if (wantWidth < 0) {
        wantWidth = 0;
    }
    if (hintWidth < 0) {
        hintWidth = 0;
    }
    Layout.Alignment alignment = getLayoutAlignment();
    boolean shouldEllipsize = mEllipsize != null && mInput == null;
    final boolean switchEllipsize = mEllipsize == TruncateAt.MARQUEE && mMarqueeFadeMode != MARQUEE_FADE_NORMAL;
    TruncateAt effectiveEllipsize = mEllipsize;
    if (mEllipsize == TruncateAt.MARQUEE && mMarqueeFadeMode == MARQUEE_FADE_SWITCH_SHOW_ELLIPSIS) {
        effectiveEllipsize = TruncateAt.END_SMALL;
    }
    if (mTextDir == null) {
        resolveTextDirection();
    }
    mLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, shouldEllipsize, effectiveEllipsize, effectiveEllipsize == mEllipsize);
    if (switchEllipsize) {
        TruncateAt oppositeEllipsize = effectiveEllipsize == TruncateAt.MARQUEE ? TruncateAt.END : TruncateAt.MARQUEE;
        mSavedMarqueeModeLayout = makeSingleLayout(wantWidth, boring, ellipsisWidth, alignment, shouldEllipsize, oppositeEllipsize, effectiveEllipsize != mEllipsize);
    }
    shouldEllipsize = mEllipsize != null;
    mHintLayout = null;
    if (mHint != null) {
        if (shouldEllipsize)
            hintWidth = wantWidth;
        if (hintBoring == UNKNOWN_BORING) {
            hintBoring = BoringLayout.isBoring(mHint, mTextPaint, mTextDir, mHintBoring);
            if (hintBoring != null) {
                mHintBoring = hintBoring;
            }
        }
        if (hintBoring != null) {
            if (hintBoring.width <= hintWidth && (!shouldEllipsize || hintBoring.width <= ellipsisWidth)) {
                if (mSavedHintLayout != null) {
                    mHintLayout = mSavedHintLayout.replaceOrMake(mHint, mTextPaint, hintWidth, alignment, mSpacingMult, mSpacingAdd, hintBoring, mIncludePad);
                } else {
                    mHintLayout = BoringLayout.make(mHint, mTextPaint, hintWidth, alignment, mSpacingMult, mSpacingAdd, hintBoring, mIncludePad);
                }
                mSavedHintLayout = (BoringLayout) mHintLayout;
            } else if (shouldEllipsize && hintBoring.width <= hintWidth) {
                if (mSavedHintLayout != null) {
                    mHintLayout = mSavedHintLayout.replaceOrMake(mHint, mTextPaint, hintWidth, alignment, mSpacingMult, mSpacingAdd, hintBoring, mIncludePad, mEllipsize, ellipsisWidth);
                } else {
                    mHintLayout = BoringLayout.make(mHint, mTextPaint, hintWidth, alignment, mSpacingMult, mSpacingAdd, hintBoring, mIncludePad, mEllipsize, ellipsisWidth);
                }
            } else if (shouldEllipsize) {
                mHintLayout = new StaticLayout(mHint, 0, mHint.length(), mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad, mEllipsize, ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE);
            } else {
                mHintLayout = new StaticLayout(mHint, mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad);
            }
        } else if (shouldEllipsize) {
            mHintLayout = new StaticLayout(mHint, 0, mHint.length(), mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad, mEllipsize, ellipsisWidth, mMaxMode == LINES ? mMaximum : Integer.MAX_VALUE);
        } else {
            mHintLayout = new StaticLayout(mHint, mTextPaint, hintWidth, alignment, mTextDir, mSpacingMult, mSpacingAdd, mIncludePad);
        }
    }
    if (bringIntoView) {
        registerForPreDraw();
    }
    if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
        if (!compressText(ellipsisWidth)) {
            final int height = mLayoutParams.height;
            // start the marquee immediately
            if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) {
                startMarquee();
            } else {
                // Defer the start of the marquee until we know our width (see setFrame())
                mRestartMarquee = true;
            }
        }
    }
    // CursorControllers need a non-null mLayout
    prepareCursorControllers();
}
Also used : BoringLayout(android.text.BoringLayout) Layout(android.text.Layout) StaticLayout(android.text.StaticLayout) DynamicLayout(android.text.DynamicLayout) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) TruncateAt(android.text.TextUtils.TruncateAt)

Example 64 with StaticLayout

use of android.text.StaticLayout in project android_packages_apps_Camera by CyanogenMod.

the class Switch method makeLayout.

private Layout makeLayout(CharSequence text, int maxWidth) {
    int actual_width = (int) Math.ceil(Layout.getDesiredWidth(text, mTextPaint));
    StaticLayout l = new StaticLayout(text, 0, text.length(), mTextPaint, actual_width, Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true, TextUtils.TruncateAt.END, (int) Math.min(actual_width, maxWidth));
    return l;
}
Also used : StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 65 with StaticLayout

use of android.text.StaticLayout in project android_frameworks_base by DirtyUnicorns.

the class SubtitleView method onDraw.

@Override
protected void onDraw(Canvas c) {
    final StaticLayout layout = mLayout;
    if (layout == null) {
        return;
    }
    final int saveCount = c.save();
    final int innerPaddingX = mInnerPaddingX;
    c.translate(mPaddingLeft + innerPaddingX, mPaddingTop);
    final int lineCount = layout.getLineCount();
    final Paint textPaint = mTextPaint;
    final Paint paint = mPaint;
    final RectF bounds = mLineBounds;
    if (Color.alpha(mBackgroundColor) > 0) {
        final float cornerRadius = mCornerRadius;
        float previousBottom = layout.getLineTop(0);
        paint.setColor(mBackgroundColor);
        paint.setStyle(Style.FILL);
        for (int i = 0; i < lineCount; i++) {
            bounds.left = layout.getLineLeft(i) - innerPaddingX;
            bounds.right = layout.getLineRight(i) + innerPaddingX;
            bounds.top = previousBottom;
            bounds.bottom = layout.getLineBottom(i);
            previousBottom = bounds.bottom;
            c.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
        }
    }
    final int edgeType = mEdgeType;
    if (edgeType == CaptionStyle.EDGE_TYPE_OUTLINE) {
        textPaint.setStrokeJoin(Join.ROUND);
        textPaint.setStrokeWidth(mOutlineWidth);
        textPaint.setColor(mEdgeColor);
        textPaint.setStyle(Style.FILL_AND_STROKE);
        for (int i = 0; i < lineCount; i++) {
            layout.drawText(c, i, i);
        }
    } else if (edgeType == CaptionStyle.EDGE_TYPE_DROP_SHADOW) {
        textPaint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mEdgeColor);
    } else if (edgeType == CaptionStyle.EDGE_TYPE_RAISED || edgeType == CaptionStyle.EDGE_TYPE_DEPRESSED) {
        final boolean raised = edgeType == CaptionStyle.EDGE_TYPE_RAISED;
        final int colorUp = raised ? Color.WHITE : mEdgeColor;
        final int colorDown = raised ? mEdgeColor : Color.WHITE;
        final float offset = mShadowRadius / 2f;
        textPaint.setColor(mForegroundColor);
        textPaint.setStyle(Style.FILL);
        textPaint.setShadowLayer(mShadowRadius, -offset, -offset, colorUp);
        for (int i = 0; i < lineCount; i++) {
            layout.drawText(c, i, i);
        }
        textPaint.setShadowLayer(mShadowRadius, offset, offset, colorDown);
    }
    textPaint.setColor(mForegroundColor);
    textPaint.setStyle(Style.FILL);
    for (int i = 0; i < lineCount; i++) {
        layout.drawText(c, i, i);
    }
    textPaint.setShadowLayer(0, 0, 0, 0);
    c.restoreToCount(saveCount);
}
Also used : RectF(android.graphics.RectF) StaticLayout(android.text.StaticLayout) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) 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