当前位置: 首页>>代码示例>>Java>>正文


Java TextUtils.ellipsize方法代码示例

本文整理汇总了Java中android.text.TextUtils.ellipsize方法的典型用法代码示例。如果您正苦于以下问题:Java TextUtils.ellipsize方法的具体用法?Java TextUtils.ellipsize怎么用?Java TextUtils.ellipsize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.text.TextUtils的用法示例。


在下文中一共展示了TextUtils.ellipsize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getTruncatedText

import android.text.TextUtils; //导入方法依赖的package包/类
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);
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:17,代码来源:TemplatePreservingTextView.java

示例2: updateSecretTimeText

import android.text.TextUtils; //导入方法依赖的package包/类
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();
    }
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:17,代码来源:SecretPhotoViewer.java

示例3: getEllipsizedTextWithSettingScaleX

import android.text.TextUtils; //导入方法依赖的package包/类
@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;
}
 
开发者ID:sergeychilingaryan,项目名称:AOSP-Kayboard-7.1.2,代码行数:34,代码来源:SuggestionStripLayoutHelper.java

示例4: createLayout

import android.text.TextUtils; //导入方法依赖的package包/类
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
        }
    }
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:37,代码来源:SimpleTextView.java

示例5: ellipsize

import android.text.TextUtils; //导入方法依赖的package包/类
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);
  }
}
 
开发者ID:CableIM,项目名称:Cable-Android,代码行数:11,代码来源:ViewUtil.java

示例6: onMeasure

import android.text.TextUtils; //导入方法依赖的package包/类
@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);
                }
            }
        }
    }
}
 
开发者ID:pooyafaroka,项目名称:PlusGram,代码行数:36,代码来源:EditTextCaption.java

示例7: setTextWithWidth

import android.text.TextUtils; //导入方法依赖的package包/类
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);
}
 
开发者ID:QMUI,项目名称:QMUI_Android,代码行数:15,代码来源:EmojiconTextView.java

示例8: measureTime

import android.text.TextUtils; //导入方法依赖的package包/类
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;
    }
}
 
开发者ID:pooyafaroka,项目名称:PlusGram,代码行数:35,代码来源:ChatBaseCell.java

示例9: drawContent

import android.text.TextUtils; //导入方法依赖的package包/类
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);
        }
    }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:43,代码来源:PickerView.java

示例10: onDraw

import android.text.TextUtils; //导入方法依赖的package包/类
@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();
    }
}
 
开发者ID:dhbw-timetable,项目名称:dhbw-timetable-android,代码行数:57,代码来源:WeekdayView.java

示例11: calculateUsingTextSize

import android.text.TextUtils; //导入方法依赖的package包/类
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);
            }
        }
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:53,代码来源:CollapsingTextHelper.java

示例12: measureTime

import android.text.TextUtils; //导入方法依赖的package包/类
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;
    }
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:44,代码来源:ChatMessageCell.java

示例13: calculateUsingTextSize

import android.text.TextUtils; //导入方法依赖的package包/类
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);
        }
    }
}
 
开发者ID:QMUI,项目名称:QMUI_Android,代码行数:70,代码来源:QMUICollapsingTextHelper.java

示例14: calculateUsingTextSize

import android.text.TextUtils; //导入方法依赖的package包/类
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);
    }
  }
}
 
开发者ID:commonsguy,项目名称:cwac-crossport,代码行数:70,代码来源:CollapsingTextHelper.java

示例15: draw

import android.text.TextUtils; //导入方法依赖的package包/类
@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);
}
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:45,代码来源:LabelSpan.java


注:本文中的android.text.TextUtils.ellipsize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。