Java tutorial
//package com.java2s; /* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.graphics.Typeface; import android.text.Spannable; import android.text.SpannableString; import android.text.Spanned; import android.text.TextPaint; import android.text.TextUtils; import android.text.style.CharacterStyle; import android.text.style.StyleSpan; import android.text.style.UnderlineSpan; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class Main { private static final float MIN_TEXT_XSCALE = 0.70f; private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); @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; } private static float getTextScaleX(@Nullable final CharSequence text, final int maxWidth, final TextPaint paint) { paint.setTextScaleX(1.0f); final int width = getTextWidth(text, paint); if (width <= maxWidth || maxWidth <= 0) { return 1.0f; } return maxWidth / (float) width; } private static boolean hasStyleSpan(@Nullable final CharSequence text, final CharacterStyle style) { if (text instanceof Spanned) { return ((Spanned) text).getSpanStart(style) >= 0; } return false; } private static void addStyleSpan(@Nonnull final Spannable text, final CharacterStyle style) { text.removeSpan(style); text.setSpan(style, 0, text.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); } private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; } final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; } private static Typeface getTextTypeface(@Nullable final CharSequence text) { return hasStyleSpan(text, BOLD_SPAN) ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT; } }