Example usage for android.text TextPaint setTextScaleX

List of usage examples for android.text TextPaint setTextScaleX

Introduction

In this page you can find the example usage for android.text TextPaint setTextScaleX.

Prototype

public void setTextScaleX(float scaleX) 

Source Link

Document

Set the paint's horizontal scale factor for text.

Usage

From source file:Main.java

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;
    }//from   www.j  av a  2s  . c  o m
    return maxWidth / (float) width;
}

From source file:Main.java

private static float getTextScaleX(final CharSequence text, final int maxWidth, final TextPaint paint) {
    paint.setTextScaleX(1.0f);
    final int width = getTextWidth(text, paint);
    if (width <= maxWidth) {
        return 1.0f;
    }//from  w  w  w . j a  v  a 2s .  c o  m
    return maxWidth / (float) width;
}

From source file:Main.java

private static float getTextScaleX(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;
    }/*from w w  w  .j  a v a2 s  .c o  m*/
    return maxWidth / (float) width;
}

From source file:Main.java

private static CharSequence getEllipsizedText(final CharSequence text, final int maxWidth,
        final TextPaint paint) {
    if (text == null) {
        return null;
    }/*from   ww  w.j a va 2 s  . c  o m*/
    final float scaleX = getTextScaleX(text, maxWidth, paint);
    if (scaleX >= MIN_TEXT_XSCALE) {
        paint.setTextScaleX(scaleX);
        return text;
    }

    // Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To
    // get squeezed and ellipsized text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE).
    final float upscaledWidth = maxWidth / MIN_TEXT_XSCALE;
    CharSequence ellipsized = TextUtils.ellipsize(text, paint, upscaledWidth, TextUtils.TruncateAt.MIDDLE);
    // For an unknown reason, ellipsized seems to return a text that does indeed fit inside the
    // passed width according to paint.measureText, but not according to paint.getTextWidths.
    // But when rendered, the text seems to actually take up as many pixels as returned by
    // paint.getTextWidths, hence problem.
    // To save this case, we compare the measured size of the new text, and if it's too much,
    // try it again removing the difference. This may still give a text too long by one or
    // two pixels so we take an additional 2 pixels cushion and call it a day.
    // TODO: figure out why getTextWidths and measureText don't agree with each other, and
    // remove the following code.
    final float ellipsizedTextWidth = getTextWidth(ellipsized, paint);
    if (upscaledWidth <= ellipsizedTextWidth) {
        ellipsized = TextUtils.ellipsize(text, paint, upscaledWidth - (ellipsizedTextWidth - upscaledWidth) - 2,
                TextUtils.TruncateAt.MIDDLE);
    }
    paint.setTextScaleX(MIN_TEXT_XSCALE);
    return ellipsized;
}

From source file:Main.java

@Nullable
private static CharSequence getEllipsizedTextWithSettingScaleX(@Nullable final CharSequence text,
        final int maxWidth, @Nonnull final TextPaint paint) {
    if (text == null) {
        return null;
    }// w  w w  . j  av  a2 s.  c o  m
    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;
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawWordmarkFromText(@NonNull Context context, @NonNull Canvas canvas,
        boolean isArticleRTL) {
    final int maxWidth = WIDTH - DESCRIPTION_WIDTH - 2 * HORIZONTAL_PADDING;
    final float fontSize = 20.0f;
    final float scaleX = 1.06f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);// ww  w. j av a 2 s.c  o  m
    textPaint.setColor(Color.LTGRAY);
    textPaint.setTextSize(fontSize);
    textPaint.setTypeface(SERIF);
    textPaint.setTextScaleX(scaleX);

    Spanned wikipedia = StringUtil.fromHtml(context.getString(R.string.wp_stylized));
    Layout.Alignment align = L10nUtil.isDeviceRTL() ? ALIGN_OPPOSITE : ALIGN_NORMAL;
    StaticLayout wordmarkLayout = buildLayout(
            new TextLayoutParams(wikipedia, textPaint, maxWidth, 1.0f, align));
    final int width = (int) wordmarkLayout.getLineWidth(0);
    final int height = wordmarkLayout.getHeight();

    final int bottom = HEIGHT - BOTTOM_PADDING;
    final int top = bottom - height;

    int left = WIDTH - HORIZONTAL_PADDING - width;
    if (isArticleRTL) {
        left = HORIZONTAL_PADDING;
    }

    canvas.save(); // --
    canvas.translate(left, top);
    wordmarkLayout.draw(canvas);
    canvas.restore(); // --
}