Example usage for android.text TextPaint setTypeface

List of usage examples for android.text TextPaint setTypeface

Introduction

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

Prototype

public Typeface setTypeface(Typeface typeface) 

Source Link

Document

Set or clear the typeface object.

Usage

From source file:Main.java

private static int getTextWidth(final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }//from w  w w.  j av  a 2 s.  c  o m
    final Typeface savedTypeface = paint.getTypeface();
    paint.setTypeface(getTextTypeface(text));
    final int len = text.length();
    final float[] widths = new float[len];
    final int count = paint.getTextWidths(text, 0, len, widths);
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    paint.setTypeface(savedTypeface);
    return width;
}

From source file:com.irccloud.android.data.model.Avatar.java

public static Bitmap generateBitmap(String text, int textColor, int bgColor, boolean isDarkTheme, int size,
        boolean round) {
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);

        if (isDarkTheme || !round) {
            p.setColor(bgColor);/*from w w w  . ja v  a 2 s. c  om*/
            if (round)
                c.drawCircle(size / 2, size / 2, size / 2, p);
            else
                c.drawColor(bgColor);
        } else {
            float[] hsv = new float[3];
            Color.colorToHSV(bgColor, hsv);
            hsv[2] *= 0.8f;
            p.setColor(Color.HSVToColor(hsv));
            c.drawCircle(size / 2, size / 2, (size / 2) - 2, p);
            p.setColor(bgColor);
            c.drawCircle(size / 2, (size / 2) - 2, (size / 2) - 2, p);
        }
        TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        tp.setTextAlign(Paint.Align.CENTER);
        tp.setTypeface(font);
        tp.setTextSize((int) (size * 0.65));
        tp.setColor(textColor);
        if (isDarkTheme || !round) {
            c.drawText(text, size / 2, (size / 2) - ((tp.descent() + tp.ascent()) / 2), tp);
        } else {
            c.drawText(text, size / 2, (size / 2) - 4 - ((tp.descent() + tp.ascent()) / 2), tp);
        }

        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }// w w  w .j  av  a 2  s  .c  o  m
    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;
}

From source file:enterprayz.megatools.Tools.java

public static int getTextHeight(String text, int maxWidth, float textSize, Typeface typeface) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
    paint.setTextSize(textSize);//from  ww  w. ja  v  a  2s.  c  o m
    paint.setTypeface(typeface);

    int lineCount = 0;

    int index = 0;
    int length = text.length();

    while (index < length - 1) {
        index += paint.breakText(text, index, length, true, maxWidth, null);
        lineCount++;
    }

    Rect bounds = new Rect();
    paint.getTextBounds("Py", 0, 2, bounds);
    return (int) Math.floor(lineCount * bounds.height());
}

From source file:org.cocos2dx.lib.Cocos2dxBitmap.java

private static String getStringWithEllipsis(String originalText, float width, float fontSize) {
    if (TextUtils.isEmpty(originalText)) {
        return "";
    }/*from   w ww  .j a  va  2 s  .co m*/

    TextPaint paint = new TextPaint();
    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(fontSize);

    return TextUtils.ellipsize(originalText, paint, width, TextUtils.TruncateAt.valueOf("END")).toString();
}

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);/*  w ww .  j a va  2 s .co 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(); // --
}

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

@NonNull
private static Layout drawTextSnippet(@NonNull Canvas canvas, @NonNull CharSequence textSnippet) {
    final int top = TOP_PADDING;
    final int maxHeight = 225;
    final int maxLines = 5;
    final float maxFontSize = 195.0f;
    final float minFontSize = 32.0f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);/*from   www  .  jav a  2s.c o m*/
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);
    textPaint.setShadowLayer(1.0f, 1.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(textSnippet, textPaint, TEXT_WIDTH, SPACING_MULTIPLIER), maxHeight, maxLines,
            maxFontSize, minFontSize);

    canvas.save();
    int horizontalCenterOffset = top + (maxHeight - textLayout.getHeight()) / QUARTER;
    canvas.translate(HORIZONTAL_PADDING, horizontalCenterOffset);
    textLayout.draw(canvas);
    canvas.restore();

    return textLayout;
}

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

private static void drawTitle(@NonNull Canvas canvas, @NonNull String title, int top, boolean isArticleRTL) {
    final int marginBottom = 0;
    final int maxHeight = 70;
    final int maxLines = 2;
    final float maxFontSize = 30.0f;
    final float spacingMultiplier = 0.7f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);//from  w  ww.  j  ava2  s . c o m
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(SERIF);
    textPaint.setShadowLayer(1.0f, 0.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(title, textPaint, DESCRIPTION_WIDTH, spacingMultiplier), maxHeight, maxLines,
            maxFontSize, maxFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }
    int marginBottomTotal = marginBottom;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // versions < 5.0 don't compensate for bottom margin correctly when line
        // spacing is less than 1.0, so we'll compensate ourselves
        final int marginBoost = 10;
        marginBottomTotal += marginBoost;
    }

    top = top - marginBottomTotal - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();
}

From source file:com.gm.goldencity.util.Utils.java

/**
 * Apply typeface to a plane text and return spannableString
 *
 * @param text     Text that you want to apply typeface
 * @param typeface Typeface that you want to apply to your text
 * @return spannableString/*from   ww  w .j a va  2s. co m*/
 */
public static SpannableString applyTypefaceToString(String text, final Typeface typeface) {
    SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new MetricAffectingSpan() {
        @Override
        public void updateMeasureState(TextPaint p) {
            p.setTypeface(typeface);

            // Note: This flag is required for proper typeface rendering
            p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setTypeface(typeface);

            // Note: This flag is required for proper typeface rendering
            tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
    }, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

From source file:io.vit.vitio.Extras.TypeFaceSpan.java

@Override
public void updateMeasureState(TextPaint p) {
    p.setTypeface(mTypeface);
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}