Example usage for android.graphics Paint getTextWidths

List of usage examples for android.graphics Paint getTextWidths

Introduction

In this page you can find the example usage for android.graphics Paint getTextWidths.

Prototype

public int getTextWidths(String text, float[] widths) 

Source Link

Document

Return the advance widths for the characters in the string.

Usage

From source file:Main.java

/**
 * Gets the width of a text given the specified paint.
 *///from   www  .java 2 s  .co m
public static float getTextWidth(String text, Paint paint) {
    float[] widths = new float[text.length() * 2];
    paint.getTextWidths(text, widths);

    float sum = 0;
    for (float width : widths)
        sum += width;

    return sum;
}

From source file:Main.java

public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }/*from   ww w. ja  v a 2 s.co  m*/
    }
    return iRet;
}

From source file:Main.java

public static int getStringWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
        int len = str.length();
        float[] widths = new float[len];
        paint.getTextWidths(str, widths);
        for (int j = 0; j < len; j++) {
            iRet += (int) Math.ceil(widths[j]);
        }/* w w w  .  ja v a2s  . c o m*/
    }
    return iRet;
}

From source file:Main.java

public static float getTextWidth(Paint paint, String text, float textSize) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }/*from   www . ja  v a  2s  .  c  om*/
    paint.setTextSize(textSize);
    float[] widths = new float[text.length()];
    paint.getTextWidths(text, widths);
    float totalWidth = 0;
    for (int i = 0; i < widths.length; i++) {
        totalWidth += widths[i];
    }
    return totalWidth;
}

From source file:com.android.deskclock.timer.TimerView.java

/**
 * Measure the text and add a start padding to the view
 * @param textView view to measure and onb to which add start padding
 *///from w  w  w .j a  v  a  2  s  .  co m
private void addStartPadding(TextView textView) {
    final float gapPadding = 0.45f;
    // allDigits will contain ten digits: "0123456789" in the default locale
    String allDigits = String.format(Locale.getDefault(), "%010d", 123456789);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textView.getTextSize());
    paint.setTypeface(textView.getTypeface());

    float widths[] = new float[allDigits.length()];
    int ll = paint.getTextWidths(allDigits, widths);
    int largest = 0;
    for (int ii = 1; ii < ll; ii++) {
        if (widths[ii] > widths[largest]) {
            largest = ii;
        }
    }
    // Add left padding to the view - Note: layout inherits LTR
    textView.setPadding((int) (gapPadding * widths[largest]), 0, 0, 0);
}

From source file:com.slushpupie.deskclock.DeskClock.java

private Rect getBoundingBox(String text, Typeface font, float size) {
    Rect r = new Rect(0, 0, 0, 0);
    float widths[] = new float[text.length()];
    float width = 0;
    Paint paint = new Paint(0);
    paint.setTypeface(font);/*from w w  w. j a v a2 s  .co m*/
    paint.setTextSize(size);
    paint.getTextBounds(text, 0, text.length(), r);
    paint.getTextWidths(text, widths);
    for (float w : widths)
        width += w;
    r.right = (int) width;
    return r;
}