Example usage for android.graphics Paint measureText

List of usage examples for android.graphics Paint measureText

Introduction

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

Prototype

public float measureText(String text) 

Source Link

Document

Return the width of the text.

Usage

From source file:Main.java

public static float getFontlength(Paint paint, String str) {
    return paint.measureText(str);
}

From source file:Main.java

public static float getTextWidth(Paint paint, String text) {
    return paint.measureText(text);
}

From source file:Main.java

/**
 * calculates the approximate width of a text, depending on a demo text
 * avoid repeated calls (e.g. inside drawing methods)
 *
 * @param paint//from  w w w .  j  a v a  2s  .  c om
 * @param demoText
 * @return
 */
public static int calcTextWidth(Paint paint, String demoText) {
    return (int) paint.measureText(demoText);
}

From source file:Main.java

public static int measureTextWidth(String text, int textsize) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textsize);//from www.  java 2 s . c o m
    return (int) paint.measureText(text);
}

From source file:Main.java

public static void drawText(String text, float x, float y, Canvas canvas, Paint paint) {
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, 1, rect);
    float halfW = paint.measureText(text) / 2;
    canvas.drawText(text, x - halfW, y + halfW, paint);
}

From source file:Main.java

public static float measureTextWidth(Paint paint, String str) {
    if (paint == null || str == null) {
        return 0;
    }/*w w  w  .  j a  va2s. com*/
    return paint.measureText(str);
}

From source file:Main.java

public static float getTextHeight(Paint paint) {
    Rect rectSizeText = new Rect();
    paint.getTextBounds("s", 0, "s".length(), rectSizeText);

    return paint.measureText("s");

}

From source file:Main.java

/**
 * Truncate a string to a given maximum width, relative to its paint size.
 * @param paintObject The object the text will be painted on.
 * @param text The text to truncate./*  ww  w.ja  va 2s  . c  o m*/
 * @param maxWidth The maximum width of the truncated string.
 * @return The truncated string.
 */
public static String getTruncatedString(Paint paintObject, String text, int maxWidth) {

    boolean modified = false;

    while ((paintObject.measureText(text) > maxWidth) && (text.length() > 0)) {
        text = text.substring(0, text.length() - 1);
        modified = true;
    }

    if (modified) {
        text += "...";
    }

    return text;
}

From source file:Main.java

public static float setTextSizeForWidth(Paint paint, float desiredWidth, String text) {

    final float testTextSize = 100;
    paint.setTextSize(testTextSize);//from w w w.  j av a2  s. co m
    final float testWidth = paint.measureText(text);

    // Calculate the desired size as a proportion of our testTextSize.
    float desiredTextSize = testTextSize * desiredWidth / testWidth;

    // Set the paint for that size.
    paint.setTextSize(desiredTextSize);
    return desiredTextSize;
}

From source file:Main.java

private static String wrap(String s, float width, Paint p) {
    String[] str = s.split("\\s"); // regex
    StringBuilder smb = new StringBuilder(); // save memory
    smb.append(SYSTEM_NEWLINE);//from   www .j a v  a  2s .co m
    for (String element : str) {
        float length = p.measureText(element);
        String[] pieces = smb.toString().split(SYSTEM_NEWLINE);
        try {
            if ((p.measureText(pieces[pieces.length - 1]) + length) > width) {
                smb.append(SYSTEM_NEWLINE);
            }
        } catch (Exception e) {
        }
        smb.append(element + " ");
    }
    return smb.toString().replaceFirst(SYSTEM_NEWLINE, "");
}