Example usage for android.text TextUtils ellipsize

List of usage examples for android.text TextUtils ellipsize

Introduction

In this page you can find the example usage for android.text TextUtils ellipsize.

Prototype

public static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TruncateAt where) 

Source Link

Document

Returns the original text if it fits in the specified width given the properties of the specified Paint, or, if it does not fit, a truncated copy with ellipsis character added at the specified edge or center.

Usage

From source file:devlight.io.library.ArcProgressStackView.java

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);

    // Save and rotate to start angle
    canvas.save();//w w w .j a  v  a 2 s . co m
    final float radius = mSize * 0.5F;
    canvas.rotate(mStartAngle, radius, radius);

    // Draw all of progress
    for (int i = 0; i < mModels.size(); i++) {
        final Model model = mModels.get(i);
        // Get progress for current model
        float progressFraction = mIsAnimated && !isInEditMode()
                ? (model.mLastProgress + (mAnimatedFraction * (model.getProgress() - model.mLastProgress)))
                        / MAX_PROGRESS
                : model.getProgress() / MAX_PROGRESS;
        if (i != mActionMoveModelIndex && mActionMoveModelIndex != ANIMATE_ALL_INDEX)
            progressFraction = model.getProgress() / MAX_PROGRESS;
        final float progress = progressFraction * mSweepAngle;

        // Check if model have gradient
        final boolean isGradient = model.getColors() != null;
        // Set width of progress
        mProgressPaint.setStrokeWidth(mProgressModelSize);

        // Set model arc progress
        model.mPath.reset();
        model.mPath.addArc(model.mBounds, 0.0F, progress);

        // Draw gradient progress or solid
        resetShadowLayer();
        mProgressPaint.setShader(null);
        mProgressPaint.setStyle(Paint.Style.STROKE);

        if (mIsModelBgEnabled) {
            //noinspection ResourceAsColor
            mProgressPaint.setColor(isInEditMode() ? mPreviewModelBgColor : model.getBgColor());
            canvas.drawArc(model.mBounds, 0.0F, mSweepAngle, false, mProgressPaint);
            if (!isInEditMode())
                mProgressPaint.clearShadowLayer();
        }

        // Check if gradient for draw shadow at first and then gradient progress
        if (isGradient) {
            if (!mIsModelBgEnabled) {
                canvas.drawPath(model.mPath, mProgressPaint);

                if (!isInEditMode())
                    mProgressPaint.clearShadowLayer();
            }

            mProgressPaint.setShader(model.mSweepGradient);
        } else
            mProgressPaint.setColor(model.getColor());

        // Here we draw main progress
        mProgressPaint.setAlpha(255);
        canvas.drawPath(model.mPath, mProgressPaint);

        // Preview mode
        if (isInEditMode())
            continue;

        // Get model title bounds
        mTextPaint.setTextSize(mProgressModelSize * 0.5F);
        mTextPaint.getTextBounds(model.getTitle(), 0, model.getTitle().length(), model.mTextBounds);

        // Draw title at start with offset
        final float titleHorizontalOffset = model.mTextBounds.height() * 0.5F;
        final float progressLength = (float) (Math.PI / 180.0F) * progress * model.mBounds.width() * 0.5F;
        final String title = (String) TextUtils.ellipsize(model.getTitle(), mTextPaint,
                progressLength - titleHorizontalOffset * 2, TextUtils.TruncateAt.END);
        canvas.drawTextOnPath(title, model.mPath, mIsRounded ? 0.0F : titleHorizontalOffset,
                titleHorizontalOffset, mTextPaint);

        // Get pos and tan at final path point
        model.mPathMeasure.setPath(model.mPath, false);
        model.mPathMeasure.getPosTan(model.mPathMeasure.getLength(), model.mPos, model.mTan);

        // Get title width
        final float titleWidth = model.mTextBounds.width();

        // Create model progress like : 23%
        final String percentProgress = String.format("%d%%", (int) model.getProgress());
        // Get progress text bounds
        mTextPaint.setTextSize(mProgressModelSize * 0.35f);
        mTextPaint.getTextBounds(percentProgress, 0, percentProgress.length(), model.mTextBounds);

        // Get pos tan with end point offset and check whether the rounded corners for offset
        final float progressHorizontalOffset = mIndicatorOrientation == IndicatorOrientation.VERTICAL
                ? model.mTextBounds.height() * 0.5F
                : model.mTextBounds.width() * 0.5F;
        final float indicatorProgressOffset = (mIsRounded ? progressFraction : 1.0F)
                * (-progressHorizontalOffset - titleHorizontalOffset
                        - (mIsRounded ? model.mTextBounds.height() * 2.0F : 0.0F));
        model.mPathMeasure.getPosTan(model.mPathMeasure.getLength() + indicatorProgressOffset, model.mPos,
                mIndicatorOrientation == IndicatorOrientation.VERTICAL && !mIsRounded ? new float[2]
                        : model.mTan);

        // Check if there available place for indicator
        if ((titleWidth + model.mTextBounds.height() + titleHorizontalOffset * 2.0F)
                - indicatorProgressOffset < progressLength) {
            // Get rotate indicator progress angle for progress value
            float indicatorProgressAngle = (float) (Math.atan2(model.mTan[1], model.mTan[0])
                    * (180.0F / Math.PI));
            // Get arc angle of progress indicator
            final float indicatorLengthProgressAngle = ((progressLength + indicatorProgressOffset)
                    / (model.mBounds.width() * 0.5F)) * (float) (180.0F / Math.PI);

            // Detect progress indicator position : left or right and then rotate
            if (mIndicatorOrientation == IndicatorOrientation.VERTICAL) {
                // Get X point of arc angle progress indicator
                final float x = (float) (model.mBounds.width() * 0.5F
                        * (Math.cos((indicatorLengthProgressAngle + mStartAngle) * Math.PI / 180.0F)))
                        + model.mBounds.centerX();
                indicatorProgressAngle += (x > radius) ? -90.0F : 90.0F;
            } else {
                // Get Y point of arc angle progress indicator
                final float y = (float) (model.mBounds.height() * 0.5F
                        * (Math.sin((indicatorLengthProgressAngle + mStartAngle) * Math.PI / 180.0F)))
                        + model.mBounds.centerY();
                indicatorProgressAngle += (y > radius) ? 180.0F : 0.0F;
            }

            // Draw progress value
            canvas.save();
            canvas.rotate(indicatorProgressAngle, model.mPos[0], model.mPos[1]);
            canvas.drawText(percentProgress, model.mPos[0] - model.mTextBounds.exactCenterX(),
                    model.mPos[1] - model.mTextBounds.exactCenterY(), mTextPaint);
            canvas.restore();
        }

        // Check if gradient and have rounded corners, because we must to create elevation effect
        // for start progress corner
        if ((isGradient || mIsLeveled) && mIsRounded && progress != 0) {
            model.mPathMeasure.getPosTan(0.0F, model.mPos, model.mTan);

            // Set paint for overlay rounded gradient with shadow
            setLevelShadowLayer();
            //noinspection ResourceAsColor
            mLevelPaint.setColor(isGradient ? model.getColors()[0] : model.getColor());

            // Get bounds of start pump
            final float halfSize = mProgressModelSize * 0.5F;
            final RectF arcRect = new RectF(model.mPos[0] - halfSize, model.mPos[1] - halfSize,
                    model.mPos[0] + halfSize, model.mPos[1] + halfSize + 2.0F);
            canvas.drawArc(arcRect, 0.0F, -180.0F, true, mLevelPaint);
        }
    }

    // Restore after drawing
    canvas.restore();
}

From source file:com.android.mail.browse.ConversationItemView.java

SpannableStringBuilder elideParticipants(List<SpannableString> parts) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();
    float totalWidth = 0;
    boolean ellipsize = false;
    float width;/*from  w  ww .ja v  a 2s.  c  o  m*/
    boolean skipToHeader = false;

    // start with "To: " if we're showing recipients
    if (mDisplayedFolder.shouldShowRecipients() && !parts.isEmpty()) {
        final SpannableString toHeader = SendersView.getFormattedToHeader();
        CharacterStyle[] spans = toHeader.getSpans(0, toHeader.length(), CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        totalWidth += sPaint.measureText(toHeader.toString());
        builder.append(toHeader);
        skipToHeader = true;
    }

    final SpannableStringBuilder messageInfoString = mHeader.messageInfoString;
    if (!TextUtils.isEmpty(messageInfoString)) {
        CharacterStyle[] spans = messageInfoString.getSpans(0, messageInfoString.length(),
                CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // Paint the message info string to see if we lose space.
        float messageInfoWidth = sPaint.measureText(messageInfoString.toString());
        totalWidth += messageInfoWidth;
    }
    SpannableString prevSender = null;
    SpannableString ellipsizedText;
    for (SpannableString sender : parts) {
        // There may be null sender strings if there were dupes we had to remove.
        if (sender == null) {
            continue;
        }
        // No more width available, we'll only show fixed fragments.
        if (ellipsize) {
            break;
        }
        CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
        // There is only 1 character style span.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // If there are already senders present in this string, we need to
        // make sure we prepend the dividing token
        if (SendersView.sElidedString.equals(sender.toString())) {
            sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
        } else if (!skipToHeader && builder.length() > 0
                && (prevSender == null || !SendersView.sElidedString.equals(prevSender.toString()))) {
            sender = copyStyles(spans, sSendersSplitToken + sender);
        } else {
            skipToHeader = false;
        }
        prevSender = sender;

        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // Measure the width of the current sender and make sure we have space
        width = (int) sPaint.measureText(sender.toString());
        if (width + totalWidth > mSendersWidth) {
            // The text is too long, new line won't help. We have to
            // ellipsize text.
            ellipsize = true;
            width = mSendersWidth - totalWidth; // ellipsis width?
            ellipsizedText = copyStyles(spans, TextUtils.ellipsize(sender, sPaint, width, TruncateAt.END));
            width = (int) sPaint.measureText(ellipsizedText.toString());
        } else {
            ellipsizedText = null;
        }
        totalWidth += width;

        final CharSequence fragmentDisplayText;
        if (ellipsizedText != null) {
            fragmentDisplayText = ellipsizedText;
        } else {
            fragmentDisplayText = sender;
        }
        builder.append(fragmentDisplayText);
    }
    mHeader.styledMessageInfoStringOffset = builder.length();
    if (!TextUtils.isEmpty(messageInfoString)) {
        builder.append(messageInfoString);
    }
    return builder;
}

From source file:com.tct.mail.browse.ConversationItemView.java

SpannableStringBuilder elideParticipants(List<SpannableString> parts) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();
    float totalWidth = 0;
    boolean ellipsize = false;
    float width;/*w  w w . j a va  2 s .  co  m*/
    boolean skipToHeader = false;

    // start with "To: " if we're showing recipients
    if (mDisplayedFolder.shouldShowRecipients() && !parts.isEmpty()) {
        final SpannableString toHeader = SendersView.getFormattedToHeader();
        CharacterStyle[] spans = toHeader.getSpans(0, toHeader.length(), CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        totalWidth += sPaint.measureText(toHeader.toString());
        builder.append(toHeader);
        skipToHeader = true;
    }

    final SpannableStringBuilder messageInfoString = mHeader.messageInfoString;
    if (messageInfoString.length() > 0) {
        CharacterStyle[] spans = messageInfoString.getSpans(0, messageInfoString.length(),
                CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // Paint the message info string to see if we lose space.
        float messageInfoWidth = sPaint.measureText(messageInfoString.toString());
        totalWidth += messageInfoWidth;
    }
    SpannableString prevSender = null;
    SpannableString ellipsizedText;
    for (SpannableString sender : parts) {
        // There may be null sender strings if there were dupes we had to remove.
        if (sender == null) {
            continue;
        }
        // No more width available, we'll only show fixed fragments.
        if (ellipsize) {
            break;
        }
        CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
        // There is only 1 character style span.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // If there are already senders present in this string, we need to
        // make sure we prepend the dividing token
        if (SendersView.sElidedString.equals(sender.toString())) {
            prevSender = sender;
            sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
        } else if (!skipToHeader && builder.length() > 0
                && (prevSender == null || !SendersView.sElidedString.equals(prevSender.toString()))) {
            prevSender = sender;
            sender = copyStyles(spans, sSendersSplitToken + sender);
        } else {
            prevSender = sender;
            skipToHeader = false;
        }
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_S
        // Measure the width of the current sender and make sure we have space
        width = (int) sPaint.measureText(sender.toString());
        if (width + totalWidth > mCoordinates.sendersWidth) {
            // The text is too long, new line won't help. We have to
            // ellipsize text.
            ellipsize = true;
            width = mCoordinates.sendersWidth - totalWidth; // ellipsis width?
            ellipsizedText = copyStyles(spans, TextUtils.ellipsize(sender, sPaint, width, TruncateAt.END));
            width = (int) sPaint.measureText(ellipsizedText.toString());
        } else {
            ellipsizedText = null;
        }
        totalWidth += width;
        //TS: yanhua.chen 2015-9-2 EMAIL CR_540046 MOD_E

        //[FEATURE]-Add-BEGIN by CDTS.zhonghua.tuo,05/29/2014,FR 670064
        CharSequence fragmentDisplayText;
        if (ellipsizedText != null) {
            fragmentDisplayText = ellipsizedText;
        } else {
            fragmentDisplayText = sender;
        }
        boolean filterSender = false;
        if (mField == UIProvider.LOCAL_SEARCH_ALL || mField == UIProvider.LOCAL_SEARCH_FROM) {
            filterSender = true;
        }
        if (mQueryText != null && filterSender) {
            fragmentDisplayText = TextUtilities.highlightTermsInText(fragmentDisplayText.toString(),
                    mQueryText);
        }
        //[FEATURE]-Add-END by CDTS.zhonghua.tuo
        builder.append(fragmentDisplayText);
    }
    mHeader.styledMessageInfoStringOffset = builder.length();
    builder.append(messageInfoString);
    return builder;
}