Example usage for android.text TextPaint getTypeface

List of usage examples for android.text TextPaint getTypeface

Introduction

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

Prototype

public Typeface getTypeface() 

Source Link

Document

Get the paint's 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  a va 2s .  c  om
    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:Main.java

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