Java Code Examples for android.text.TextUtils.ellipsize()

The following are Jave code examples for showing how to use ellipsize() of the android.text.TextUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: chromium-for-android-56-debug-video   File: TemplatePreservingTextView.java   View Source Code Vote up 7 votes
private CharSequence getTruncatedText(int availWidth) {
    final TextPaint paint = getPaint();

    // Calculate the width the template takes.
    final String emptyTemplate = String.format(mTemplate, "");
    final float emptyTemplateWidth = paint.measureText(emptyTemplate);

    // Calculate the available width for the content.
    final float contentWidth = Math.max(availWidth - emptyTemplateWidth, 0.f);

    // Ellipsize the content to the available width.
    CharSequence clipped = TextUtils.ellipsize(mContent, paint, contentWidth, TruncateAt.END);

    // Build the full string, which should fit within availWidth.
    return String.format(mTemplate, clipped);
}
 
Example 2
Project: airgram   File: SecretPhotoViewer.java   View Source Code Vote up 6 votes
private void updateSecretTimeText() {
    if (currentMessageObject == null) {
        return;
    }
    String str = currentMessageObject.getSecretTimeString();
    if (str == null) {
        return;
    }
    if (currentInfoString == null || !currentInfoString.equals(str)) {
        currentInfoString = str;
        infoWidth = (int)Math.ceil(infoPaint.measureText(currentInfoString));
        CharSequence str2 = TextUtils.ellipsize(currentInfoString, infoPaint, infoWidth, TextUtils.TruncateAt.END);
        infoLayout = new StaticLayout(str2, infoPaint, infoWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        invalidate();
    }
}
 
Example 3
Project: AOSP-Kayboard-7.1.2   File: SuggestionStripLayoutHelper.java   View Source Code Vote up 5 votes
@Nullable
private static CharSequence getEllipsizedTextWithSettingScaleX(
        @Nullable final CharSequence text, final int maxWidth, @Nonnull final TextPaint paint) {
    if (text == null) {
        return null;
    }
    final float scaleX = getTextScaleX(text, maxWidth, paint);
    if (scaleX >= MIN_TEXT_XSCALE) {
        paint.setTextScaleX(scaleX);
        return text;
    }

    // <code>text</code> must be ellipsized with minimum text scale x.
    paint.setTextScaleX(MIN_TEXT_XSCALE);
    final boolean hasBoldStyle = hasStyleSpan(text, BOLD_SPAN);
    final boolean hasUnderlineStyle = hasStyleSpan(text, UNDERLINE_SPAN);
    // TextUtils.ellipsize erases any span object existed after ellipsized point.
    // We have to restore these spans afterward.
    final CharSequence ellipsizedText = TextUtils.ellipsize(
            text, paint, maxWidth, TextUtils.TruncateAt.MIDDLE);
    if (!hasBoldStyle && !hasUnderlineStyle) {
        return ellipsizedText;
    }
    final Spannable spannableText = (ellipsizedText instanceof Spannable)
            ? (Spannable)ellipsizedText : new SpannableString(ellipsizedText);
    if (hasBoldStyle) {
        addStyleSpan(spannableText, BOLD_SPAN);
    }
    if (hasUnderlineStyle) {
        addStyleSpan(spannableText, UNDERLINE_SPAN);
    }
    return spannableText;
}
 
Example 4
Project: airgram   File: SimpleTextView.java   View Source Code Vote up 5 votes
private void createLayout(int width) {
    if (text != null) {
        try {
            CharSequence string = TextUtils.ellipsize(text, textPaint, width, TextUtils.TruncateAt.END);
            layout = new StaticLayout(string, 0, string.length(), textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

            /*if (metrics == null) {
                metrics = BoringLayout.isBoring(text, textPaint);
            }
            if (layout == null) {
                layout = BoringLayout.make(text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, metrics, false, TextUtils.TruncateAt.END, width);
            } else {
                layout = ((BoringLayout) layout).replaceOrMake(text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, metrics, false, TextUtils.TruncateAt.END, width);
            }*/

            /*if (spannableStringBuilder == null) {
                spannableStringBuilder = new SpannableStringBuilder(text);
                layout = new DynamicLayout(text, text, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false, TextUtils.TruncateAt.END, width);
            } else {
                spannableStringBuilder.replace(0, text.length(), text);
            }*/

            if (layout.getLineCount() > 0) {
                if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.LEFT) {
                    offsetX = -(int) layout.getLineLeft(0);
                } else if (layout.getLineLeft(0) == 0) {
                    offsetX = (int) (width - layout.getLineWidth(0));
                } else {
                    offsetX = 0;
                }
            }
        } catch (Exception e) {
            //ignore
        }
    }
}
 
Example 5
Project: Cable-Android   File: ViewUtil.java   View Source Code Vote up 5 votes
public static CharSequence ellipsize(@Nullable CharSequence text, @NonNull TextView view) {
  if (TextUtils.isEmpty(text) || view.getWidth() == 0 || view.getEllipsize() != TruncateAt.END) {
    return text;
  } else {
    return TextUtils.ellipsize(text,
                               view.getPaint(),
                               view.getWidth() - view.getPaddingRight() - view.getPaddingLeft(),
                               TruncateAt.END);
  }
}
 
Example 6
Project: PlusGram   File: EditTextCaption.java   View Source Code Vote up 5 votes
@SuppressLint({"DrawAllocation"})
protected void onMeasure(int i, int i2) {
    try {
        super.onMeasure(i, i2);
    } catch (Throwable e) {
        FirebaseCrash.report(e);
        setMeasuredDimension(MeasureSpec.getSize(i), AndroidUtilities.dp(51.0f));
        FileLog.e("tmessages", e);
    }
    this.f399b = null;
    if (this.f398a != null && this.f398a.length() > 0) {
        CharSequence text = getText();
        if (text.length() > 1 && text.charAt(0) == '@') {
            int indexOf = TextUtils.indexOf(text, ' ');
            if (indexOf != -1) {
                TextPaint paint = getPaint();
                int ceil = (int) Math.ceil((double) paint.measureText(text, 0, indexOf + 1));
                int measuredWidth = (getMeasuredWidth() - getPaddingLeft()) - getPaddingRight();
                this.f400c = text.subSequence(0, indexOf + 1).length();
                CharSequence ellipsize = TextUtils.ellipsize(this.f398a, paint, (float) (measuredWidth - ceil), TruncateAt.END);
                this.f401d = ceil;
                try {
                    this.f399b = new StaticLayout(ellipsize, getPaint(), measuredWidth - ceil, Alignment.ALIGN_NORMAL, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT, 0.0f, false);
                    if (this.f399b.getLineCount() > 0) {
                        this.f401d = (int) (((float) this.f401d) + (-this.f399b.getLineLeft(0)));
                    }
                    this.f402e = ((getMeasuredHeight() - this.f399b.getLineBottom(0)) / 2) + AndroidUtilities.dp(0.5f);
                } catch (Throwable e2) {
                    FirebaseCrash.report(e2);
                    FileLog.e("tmessages", e2);
                }
            }
        }
    }
}
 
Example 7
Project: QMUI_Android   File: EmojiconTextView.java   View Source Code Vote up 5 votes
public void setTextWithWidth(CharSequence text, int limitedWidth) {
    if (TextUtils.isEmpty(text)) {
        super.setText(text);
        return;
    }
    if (limitedWidth < 0) {
        limitedWidth = this.getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
    }

    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    EmojiconHandler.addEmojis(getContext(), builder, mEmojiconSize, mEmojiconTextSize, mTextStart, mTextLength, mUseSystemDefault);
    CharSequence trucatedText = TextUtils.ellipsize(builder, getPaint(), limitedWidth, getEllipsize());
    super.setText(trucatedText, BufferType.SPANNABLE);
}
 
Example 8
Project: PlusGram   File: ChatBaseCell.java   View Source Code Vote up 5 votes
protected void measureTime(MessageObject messageObject) {
    boolean hasSign = !messageObject.isOutOwner() && messageObject.messageOwner.from_id > 0 && messageObject.messageOwner.post;
    TLRPC.User signUser = MessagesController.getInstance().getUser(messageObject.messageOwner.from_id);
    if (hasSign && signUser == null) {
        hasSign = false;
    }
    if (hasSign) {
        currentTimeString = ", " + LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    } else {
        currentTimeString = LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    }
    if(showEditedMark && messageObject.messageOwner.edit_date > 0)currentTimeString = currentTimeString + " E";
    timeTextWidth = timeWidth = (int) Math.ceil(timePaint.measureText(currentTimeString));
    if ((messageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
        currentViewsString = String.format("%s", LocaleController.formatShortNumber(Math.max(1, messageObject.messageOwner.views), null));
        viewsTextWidth = (int) Math.ceil(timePaint.measureText(currentViewsString));
        timeWidth += viewsTextWidth + Theme.viewsCountDrawable[0].getIntrinsicWidth() + AndroidUtilities.dp(10);
    }
    if (hasSign) {
        if (availableTimeWidth == 0) {
            availableTimeWidth = AndroidUtilities.dp(1000);
        }
        CharSequence name = ContactsController.formatName(signUser.first_name, signUser.last_name).replace('\n', ' ');
        int widthForSign = availableTimeWidth - timeWidth;
        int width = (int) Math.ceil(timePaint.measureText(name, 0, name.length()));
        if (width > widthForSign) {
            name = TextUtils.ellipsize(name, timePaint, widthForSign, TextUtils.TruncateAt.END);
            width = widthForSign;
        }
        currentTimeString = name + currentTimeString;
        timeTextWidth += width;
        timeWidth += width;
    }
}
 
Example 9
Project: GitHub   File: PickerView.java   View Source Code Vote up 4 votes
private void drawContent(Canvas canvas){
    int index;
    int textColor;
    float textSize;
    float fraction = 0f;// fraction of the item in state between normal and selected, in[0, 1]
    float textSizeCenterYOffset;

    for(int i = 0; i < mShowCount + 1; i++){
        float y = mCurrDrawFirstItemY + mItemHeight * i;
        index = getIndexByRawIndex(mCurrDrawFirstItemIndex + i, getOneRecycleSize(), mWrapSelectorWheel && mWrapSelectorWheelCheck);
        if(i == mShowCount / 2){//this will be picked
            fraction = (float)(mItemHeight + mCurrDrawFirstItemY) / mItemHeight;
            textColor = getEvaluateColor(fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else if(i == mShowCount / 2 + 1){
            textColor = getEvaluateColor(1 - fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(1 - fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(1 - fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else{
            textColor = mTextColorNormal;
            textSize = mTextSizeNormal;
            textSizeCenterYOffset = mTextSizeNormalCenterYOffset;
        }
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);

        if(0 <= index && index < getOneRecycleSize()){
            CharSequence str = mDisplayedValues[index + mMinShowIndex];
            if (mTextEllipsize != null) {
                str = TextUtils.ellipsize(str, mPaintText, getWidth() - 2 * mItemPaddingHorizontal, getEllipsizeType());
            }
            canvas.drawText(str.toString(), mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        } else if(!TextUtils.isEmpty(mEmptyItemHint)){
            canvas.drawText(mEmptyItemHint, mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        }
    }
}
 
Example 10
Project: dhbw-timetable-android   File: WeekdayView.java   View Source Code Vote up 4 votes
@Override
public void onDraw(Canvas canvas) {
    paint.reset();
    textPaint.reset();
    drawGrid(canvas);
    if (!fit) fitRectsToParent();
    for (BackportAppointment a : eventRectangles.keySet()) {
        RectF rect = eventRectangles.get(a);

        textPaint.setColor(getResources().getColor(R.color.colorPrimary));
        // Draw the appointment rectangle
        canvas.drawRoundRect(new RectF(rect), dp(7), dp(7), textPaint);

        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics()));

        // Draw the course time
        canvas.save();
        Typeface currentTypeFace1 = textPaint.getTypeface();
        textPaint.setTypeface(Typeface.create(currentTypeFace1, Typeface.NORMAL));

        canvas.drawText(a.getStartTime() + " - " + a.getEndTime(), rect.left + 16, rect.top + dp(16), textPaint);

        if (a.getTitle().equals("Klausur")) {
            Log.i("1337", "" + (rect.bottom - rect.top));
        }

        // Draw info
        if (rect.bottom - rect.top > 180) {
            canvas.drawText(TextUtils.ellipsize(a.getInfo(), textPaint,
                    dp((int) (X_OFFSET * 1.75) + X_WIDTH) - 32, TextUtils.TruncateAt.END).toString(),
                    rect.left + 16, rect.bottom - 16, textPaint);
        }

        // Draw course title
        if (rect.bottom - rect.top > 60) {
            StaticLayout courseTitleLayout = new StaticLayout(
                    TextUtils.ellipsize(a.getTitle().trim(), textPaint, dp((int) (X_OFFSET * 1.75) + X_WIDTH) - 32, TextUtils.TruncateAt.END),
                    textPaint,
                    dp((int) (X_OFFSET * 1.75) + X_WIDTH) - 32,
                    Layout.Alignment.ALIGN_NORMAL,
                    1.0f,
                    0.0f,
                    false);
            Typeface currentTypeFace = textPaint.getTypeface();
            Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD);
            textPaint.setTypeface(bold);
            canvas.save();
            canvas.translate(rect.left + 16, rect.top + 16 + dp(16));
            courseTitleLayout.draw(canvas);
        }

        // Reset
        canvas.restore();
    }
}
 
Example 11
Project: boohee_v5.6   File: CollapsingTextHelper.java   View Source Code Vote up 4 votes
private void calculateUsingTextSize(float textSize) {
    boolean z = true;
    if (this.mText != null) {
        float availableWidth;
        float newTextSize;
        boolean updateDrawText = false;
        if (isClose(textSize, this.mCollapsedTextSize)) {
            availableWidth = (float) this.mCollapsedBounds.width();
            newTextSize = this.mCollapsedTextSize;
            this.mScale = 1.0f;
            if (this.mCurrentTypeface != this.mCollapsedTypeface) {
                this.mCurrentTypeface = this.mCollapsedTypeface;
                updateDrawText = true;
            }
        } else {
            availableWidth = (float) this.mExpandedBounds.width();
            newTextSize = this.mExpandedTextSize;
            if (this.mCurrentTypeface != this.mExpandedTypeface) {
                this.mCurrentTypeface = this.mExpandedTypeface;
                updateDrawText = true;
            }
            if (isClose(textSize, this.mExpandedTextSize)) {
                this.mScale = 1.0f;
            } else {
                this.mScale = textSize / this.mExpandedTextSize;
            }
        }
        if (availableWidth > 0.0f) {
            if (this.mCurrentTextSize != newTextSize || this.mBoundsChanged || updateDrawText) {
                updateDrawText = true;
            } else {
                updateDrawText = false;
            }
            this.mCurrentTextSize = newTextSize;
            this.mBoundsChanged = false;
        }
        if (this.mTextToDraw == null || updateDrawText) {
            this.mTextPaint.setTextSize(this.mCurrentTextSize);
            this.mTextPaint.setTypeface(this.mCurrentTypeface);
            TextPaint textPaint = this.mTextPaint;
            if (this.mScale == 1.0f) {
                z = false;
            }
            textPaint.setLinearText(z);
            CharSequence title = TextUtils.ellipsize(this.mText, this.mTextPaint, availableWidth, TruncateAt.END);
            if (!TextUtils.equals(title, this.mTextToDraw)) {
                this.mTextToDraw = title;
                this.mIsRtl = calculateIsRtl(this.mTextToDraw);
            }
        }
    }
}
 
Example 12
Project: mesh-core-on-android   File: NumberPickerView.java   View Source Code Vote up 4 votes
private void drawContent(Canvas canvas){
    int index;
    int textColor;
    float textSize;
    float fraction = 0f;// fraction of the item in state between normal and selected, in[0, 1]
    float textSizeCenterYOffset;

    for(int i = 0; i < mShowCount + 1; i++){
        float y = mCurrDrawFirstItemY + mItemHeight * i;
        index = getIndexByRawIndex(mCurrDrawFirstItemIndex + i, getOneRecycleSize(), mWrapSelectorWheel && mWrapSelectorWheelCheck);
        if(i == mShowCount / 2){//this will be picked
            fraction = (float)(mItemHeight + mCurrDrawFirstItemY) / mItemHeight;
            textColor = getEvaluateColor(fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else if(i == mShowCount / 2 + 1){
            textColor = getEvaluateColor(1 - fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(1 - fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(1 - fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else{
            textColor = mTextColorNormal;
            textSize = mTextSizeNormal;
            textSizeCenterYOffset = mTextSizeNormalCenterYOffset;
        }
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);

        if(0 <= index && index < getOneRecycleSize()){
            CharSequence str = mDisplayedValues[index + mMinShowIndex];
            if (mTextEllipsize != null) {
                str = TextUtils.ellipsize(str, mPaintText, getWidth() - 2 * mItemPaddingHorizontal, getEllipsizeType());
            }
            canvas.drawText(str.toString(), mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        } else if(!TextUtils.isEmpty(mEmptyItemHint)){
            canvas.drawText(mEmptyItemHint, mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        }
    }
}
 
Example 13
Project: airgram   File: ChatMessageCell.java   View Source Code Vote up 4 votes
private void measureTime(MessageObject messageObject) {
    boolean hasSign = !messageObject.isOutOwner() && messageObject.messageOwner.from_id > 0 && messageObject.messageOwner.post;
    TLRPC.User signUser = MessagesController.getInstance().getUser(messageObject.messageOwner.from_id);
    if (hasSign && signUser == null) {
        hasSign = false;
    }
    String timeString;
    TLRPC.User author = null;
    if (currentMessageObject.isFromUser()) {
        author = MessagesController.getInstance().getUser(messageObject.messageOwner.from_id);
    }
    if (messageObject.messageOwner.via_bot_id == 0 && messageObject.messageOwner.via_bot_name == null && (author == null || !author.bot) && (messageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_EDITED) != 0) {
        timeString = LocaleController.getString("EditedMessage", R.string.EditedMessage) + " " + LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    } else {
        timeString = LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    }
    if (hasSign) {
        currentTimeString = ", " + timeString;
    } else {
        currentTimeString = timeString;
    }
    timeTextWidth = timeWidth = (int) Math.ceil(timePaint.measureText(currentTimeString));
    if ((messageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
        currentViewsString = String.format("%s", LocaleController.formatShortNumber(Math.max(1, messageObject.messageOwner.views), null));
        viewsTextWidth = (int) Math.ceil(timePaint.measureText(currentViewsString));
        timeWidth += viewsTextWidth + Theme.viewsCountDrawable[0].getIntrinsicWidth() + dp(10);
    }
    if (hasSign) {
        if (availableTimeWidth == 0) {
            availableTimeWidth = dp(1000);
        }
        CharSequence name = ContactsController.formatName(signUser.first_name, signUser.last_name).replace('\n', ' ');
        int widthForSign = availableTimeWidth - timeWidth;
        int width = (int) Math.ceil(timePaint.measureText(name, 0, name.length()));
        if (width > widthForSign) {
            name = TextUtils.ellipsize(name, timePaint, widthForSign, TextUtils.TruncateAt.END);
            width = widthForSign;
        }
        currentTimeString = name + currentTimeString;
        timeTextWidth += width;
        timeWidth += width;
    }
}
 
Example 14
Project: QMUI_Android   File: QMUICollapsingTextHelper.java   View Source Code Vote up 4 votes
private void calculateUsingTextSize(final float textSize) {
    if (mText == null) return;

    final float collapsedWidth = mCollapsedBounds.width();
    final float expandedWidth = mExpandedBounds.width();

    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(textSize, mCollapsedTextSize)) {
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
        availableWidth = collapsedWidth;
    } else {
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }
        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }

        final float textSizeRatio = mCollapsedTextSize / mExpandedTextSize;
        // This is the size of the expanded bounds when it is scaled to match the
        // collapsed text size
        final float scaledDownWidth = expandedWidth * textSizeRatio;

        if (scaledDownWidth > collapsedWidth) {
            // If the scaled down size is larger than the actual collapsed width, we need to
            // cap the available width so that when the expanded text scales down, it matches
            // the collapsed width
            availableWidth = Math.min(collapsedWidth / textSizeRatio, expandedWidth);
        } else {
            // Otherwise we'll just use the expanded width
            availableWidth = expandedWidth;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }

    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTextSize(mCurrentTextSize);
        mTextPaint.setTypeface(mCurrentTypeface);
        // Use linear text scaling if we're scaling the canvas
        mTextPaint.setLinearText(mScale != 1f);

        // If we don't currently have text to draw, or the text size has changed, ellipsize...
        final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
                availableWidth, TextUtils.TruncateAt.END);
        if (!TextUtils.equals(title, mTextToDraw)) {
            mTextToDraw = title;
            mIsRtl = calculateIsRtl(mTextToDraw);
        }
    }
}
 
Example 15
Project: qmui   File: QMUICollapsingTextHelper.java   View Source Code Vote up 4 votes
private void calculateUsingTextSize(final float textSize) {
    if (mText == null) return;

    final float collapsedWidth = mCollapsedBounds.width();
    final float expandedWidth = mExpandedBounds.width();

    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;

    if (isClose(textSize, mCollapsedTextSize)) {
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
        availableWidth = collapsedWidth;
    } else {
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }
        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }

        final float textSizeRatio = mCollapsedTextSize / mExpandedTextSize;
        // This is the size of the expanded bounds when it is scaled to match the
        // collapsed text size
        final float scaledDownWidth = expandedWidth * textSizeRatio;

        if (scaledDownWidth > collapsedWidth) {
            // If the scaled down size is larger than the actual collapsed width, we need to
            // cap the available width so that when the expanded text scales down, it matches
            // the collapsed width
            availableWidth = Math.min(collapsedWidth / textSizeRatio, expandedWidth);
        } else {
            // Otherwise we'll just use the expanded width
            availableWidth = expandedWidth;
        }
    }

    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }

    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTextSize(mCurrentTextSize);
        mTextPaint.setTypeface(mCurrentTypeface);
        // Use linear text scaling if we're scaling the canvas
        mTextPaint.setLinearText(mScale != 1f);

        // If we don't currently have text to draw, or the text size has changed, ellipsize...
        final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
                availableWidth, TextUtils.TruncateAt.END);
        if (!TextUtils.equals(title, mTextToDraw)) {
            mTextToDraw = title;
            mIsRtl = calculateIsRtl(mTextToDraw);
        }
    }
}
 
Example 16
Project: SmartChart   File: NumberPickerView.java   View Source Code Vote up 4 votes
private void drawContent(Canvas canvas){
    int index;
    int textColor;
    float textSize;
    float fraction = 0f;// fraction of the item in state between normal and selected, in[0, 1]
    float textSizeCenterYOffset;

    for(int i = 0; i < mShowCount + 1; i++){
        float y = mCurrDrawFirstItemY + mItemHeight * i;
        index = getIndexByRawIndex(mCurrDrawFirstItemIndex + i, getOneRecycleSize(), mWrapSelectorWheel && mWrapSelectorWheelCheck);
        if(i == mShowCount / 2){//this will be picked
            fraction = (float)(mItemHeight + mCurrDrawFirstItemY) / mItemHeight;
            textColor = getEvaluateColor(fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else if(i == mShowCount / 2 + 1){
            textColor = getEvaluateColor(1 - fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(1 - fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(1 - fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else{
            textColor = mTextColorNormal;
            textSize = mTextSizeNormal;
            textSizeCenterYOffset = mTextSizeNormalCenterYOffset;
        }
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);

        if(0 <= index && index < getOneRecycleSize()){
            CharSequence str = mDisplayedValues[index + mMinShowIndex];
            if (mTextEllipsize != null) {
                str = TextUtils.ellipsize(str, mPaintText, getWidth() - 2 * mItemPaddingHorizontal, getEllipsizeType());
            }
            canvas.drawText(str.toString(), mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        } else if(!TextUtils.isEmpty(mEmptyItemHint)){
            canvas.drawText(mEmptyItemHint, mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        }
    }
}
 
Example 17
Project: cwac-crossport   File: CollapsingTextHelper.java   View Source Code Vote up 4 votes
private void calculateUsingTextSize(final float textSize) {
  if (mText == null) return;

  final float collapsedWidth = mCollapsedBounds.width();
  final float expandedWidth = mExpandedBounds.width();

  final float availableWidth;
  final float newTextSize;
  boolean updateDrawText = false;

  if (isClose(textSize, mCollapsedTextSize)) {
    newTextSize = mCollapsedTextSize;
    mScale = 1f;
          if (areTypefacesDifferent(mCurrentTypeface, mCollapsedTypeface)) {
      mCurrentTypeface = mCollapsedTypeface;
      updateDrawText = true;
    }
    availableWidth = collapsedWidth;
  } else {
    newTextSize = mExpandedTextSize;
          if (areTypefacesDifferent(mCurrentTypeface, mExpandedTypeface)) {
      mCurrentTypeface = mExpandedTypeface;
      updateDrawText = true;
    }
    if (isClose(textSize, mExpandedTextSize)) {
      // If we're close to the expanded text size, snap to it and use a scale of 1
      mScale = 1f;
    } else {
      // Else, we'll scale down from the expanded text size
      mScale = textSize / mExpandedTextSize;
    }

    final float textSizeRatio = mCollapsedTextSize / mExpandedTextSize;
    // This is the size of the expanded bounds when it is scaled to match the
    // collapsed text size
    final float scaledDownWidth = expandedWidth * textSizeRatio;

    if (scaledDownWidth > collapsedWidth) {
      // If the scaled down size is larger than the actual collapsed width, we need to
      // cap the available width so that when the expanded text scales down, it matches
      // the collapsed width
      availableWidth = Math.min(collapsedWidth / textSizeRatio, expandedWidth);
    } else {
      // Otherwise we'll just use the expanded width
      availableWidth = expandedWidth;
    }
  }

  if (availableWidth > 0) {
    updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
    mCurrentTextSize = newTextSize;
    mBoundsChanged = false;
  }

  if (mTextToDraw == null || updateDrawText) {
    mTextPaint.setTextSize(mCurrentTextSize);
    mTextPaint.setTypeface(mCurrentTypeface);
    // Use linear text scaling if we're scaling the canvas
    mTextPaint.setLinearText(mScale != 1f);

    // If we don't currently have text to draw, or the text size has changed, ellipsize...
          final CharSequence title = TextUtils.ellipsize(mText, mTextPaint,
                  availableWidth, TextUtils.TruncateAt.END);
    if (!TextUtils.equals(title, mTextToDraw)) {
      mTextToDraw = title;
      mIsRtl = calculateIsRtl(mTextToDraw);
    }
  }
}
 
Example 18
Project: baselibrary-master   File: NumberPickerView.java   View Source Code Vote up 4 votes
private void drawContent(Canvas canvas){
    int index;
    int textColor;
    float textSize;
    float fraction = 0f;// fraction of the item in state between normal and selected, in[0, 1]
    float textSizeCenterYOffset;

    for(int i = 0; i < mShowCount + 1; i++){
        float y = mCurrDrawFirstItemY + mItemHeight * i;
        index = getIndexByRawIndex(mCurrDrawFirstItemIndex + i, getOneRecycleSize(), mWrapSelectorWheel && mWrapSelectorWheelCheck);
        if(i == mShowCount / 2){//this will be picked
            fraction = (float)(mItemHeight + mCurrDrawFirstItemY) / mItemHeight;
            textColor = getEvaluateColor(fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else if(i == mShowCount / 2 + 1){
            textColor = getEvaluateColor(1 - fraction, mTextColorNormal, mTextColorSelected);
            textSize = getEvaluateSize(1 - fraction, mTextSizeNormal, mTextSizeSelected);
            textSizeCenterYOffset = getEvaluateSize(1 - fraction, mTextSizeNormalCenterYOffset,
                    mTextSizeSelectedCenterYOffset);
        }else{
            textColor = mTextColorNormal;
            textSize = mTextSizeNormal;
            textSizeCenterYOffset = mTextSizeNormalCenterYOffset;
        }
        mPaintText.setColor(textColor);
        mPaintText.setTextSize(textSize);

        if(0 <= index && index < getOneRecycleSize()){
            CharSequence str = mDisplayedValues[index + mMinShowIndex];
            if (mTextEllipsize != null) {
                str = TextUtils.ellipsize(str, mPaintText, getWidth() - 2 * mItemPaddingHorizontal, getEllipsizeType());
            }
            canvas.drawText(str.toString(), mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        } else if(!TextUtils.isEmpty(mEmptyItemHint)){
            canvas.drawText(mEmptyItemHint, mViewCenterX,
                    y + mItemHeight / 2 + textSizeCenterYOffset, mPaintText);
        }
    }
}
 
Example 19
Project: mvvm-template   File: LabelSpan.java   View Source Code Vote up 4 votes
@Override public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end,
                           float x, int top, int y, int bottom, @NonNull Paint paint) {
    final int padding = dims.getPadding();
    final int paddingW = padding + dims.getPaddingExtraWidth();
    final int maxWidth = dims.getMaxWidth();
    setupFontMetrics(text, start, end, fontMetrics, paint);
    final int bgWidth = measureWidth(txtPaint, text, start, end, false);
    fontMetrics.top = Math.min(fontMetrics.top, fontMetrics.ascent - padding);
    fontMetrics.bottom = Math.max(fontMetrics.bottom, padding);
    top = y + fontMetrics.top - fontMetrics.bottom;
    bottom = y;
    y = bottom - fontMetrics.bottom;
    final boolean isRtl = dims.isRtl();
    final int paddingAfter = dims.getPaddingAfter();
    if (txtPaint.bgColor != 0) {
        final int prevColor = txtPaint.getColor();
        final Paint.Style prevStyle = txtPaint.getStyle();
        txtPaint.setColor(txtPaint.bgColor);
        txtPaint.setStyle(Paint.Style.FILL);
        final float left;
        if (isRtl) {
            left = x + paddingAfter;
        } else {
            left = x;
        }
        final float right = left + bgWidth;
        final RectF rect = new RectF(left, top, right, bottom);
        final float radius = dims.getRoundedCornerRadius();
        canvas.drawRoundRect(rect, radius, radius, txtPaint);
        txtPaint.setColor(prevColor);
        txtPaint.setStyle(prevStyle);
    }
    if (bgWidth == maxWidth) {
        text = TextUtils.ellipsize(text.subSequence(start, end).toString(), txtPaint, bgWidth - 2 * paddingW, TextUtils.TruncateAt.MIDDLE);
        start = 0;
        end = text.length();
    }
    float textX = x + paddingW;
    if (isRtl) {
        textX += paddingAfter;
    }
    if (color != Color.TRANSPARENT) txtPaint.setColor(ViewHelper.generateTextColor(color));
    canvas.drawText(text, start, end, textX, y, txtPaint);
}
 
Example 20
Project: PlusGram   File: ChatMessageCell.java   View Source Code Vote up 4 votes
private void measureTime(MessageObject messageObject) {
    boolean hasSign = !messageObject.isOutOwner() && messageObject.messageOwner.from_id > 0 && messageObject.messageOwner.post;
    TLRPC.User signUser = MessagesController.getInstance().getUser(messageObject.messageOwner.from_id);
    if (hasSign && signUser == null) {
        hasSign = false;
    }
    String timeString;
    TLRPC.User author = null;
    if (currentMessageObject.isFromUser()) {
        author = MessagesController.getInstance().getUser(messageObject.messageOwner.from_id);
    }
    if (messageObject.messageOwner.via_bot_id == 0 && messageObject.messageOwner.via_bot_name == null && (author == null || !author.bot) && (messageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_EDITED) != 0) {
        timeString = LocaleController.getString("EditedMessage", R.string.EditedMessage) + " " + LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    } else {
        timeString = LocaleController.getInstance().formatterDay.format((long) (messageObject.messageOwner.date) * 1000);
    }
    if (hasSign) {
        currentTimeString = ", " + timeString;
    } else {
        currentTimeString = timeString;
    }
    timeTextWidth = timeWidth = (int) Math.ceil(timePaint.measureText(currentTimeString));
    if ((messageObject.messageOwner.flags & TLRPC.MESSAGE_FLAG_HAS_VIEWS) != 0) {
        currentViewsString = String.format("%s", LocaleController.formatShortNumber(Math.max(1, messageObject.messageOwner.views), null));
        viewsTextWidth = (int) Math.ceil(timePaint.measureText(currentViewsString));
        timeWidth += viewsTextWidth + Theme.viewsCountDrawable[0].getIntrinsicWidth() + dp(10);
    }
    if (hasSign) {
        if (availableTimeWidth == 0) {
            availableTimeWidth = dp(1000);
        }
        CharSequence name = ContactsController.formatName(signUser.first_name, signUser.last_name).replace('\n', ' ');
        int widthForSign = availableTimeWidth - timeWidth;
        int width = (int) Math.ceil(timePaint.measureText(name, 0, name.length()));
        if (width > widthForSign) {
            name = TextUtils.ellipsize(name, timePaint, widthForSign, TextUtils.TruncateAt.END);
            width = widthForSign;
        }
        currentTimeString = name + currentTimeString;
        timeTextWidth += width;
        timeWidth += width;
    }
}