Java Code Examples for android.text.TextPaint.getTextWidths()

The following are Jave code examples for showing how to use getTextWidths() of the android.text.TextPaint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: AOSP-Kayboard-7.1.2   File: SuggestionStripLayoutHelper.java   Source Code and License Vote up 8 votes
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;
}
 
Example 2
Project: simple-keyboard   File: KeyPreviewView.java   Source Code and License Vote up 5 votes
private static float getTextWidth(final String text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0.0f;
    }
    final int len = text.length();
    final float[] widths = new float[len];
    final int count = paint.getTextWidths(text, 0, len, widths);
    float width = 0;
    for (int i = 0; i < count; i++) {
        width += widths[i];
    }
    return width;
}
 
Example 3
Project: AOSP-Kayboard-7.1.2   File: KeyPreviewView.java   Source Code and License Vote up 5 votes
private static float getTextWidth(final String text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0.0f;
    }
    final int len = text.length();
    final float[] widths = new float[len];
    final int count = paint.getTextWidths(text, 0, len, widths);
    float width = 0;
    for (int i = 0; i < count; i++) {
        width += widths[i];
    }
    return width;
}