Example usage for android.graphics Paint getTextBounds

List of usage examples for android.graphics Paint getTextBounds

Introduction

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

Prototype

public void getTextBounds(char[] text, int index, int count, Rect bounds) 

Source Link

Document

Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).

Usage

From source file:Main.java

public static int getButtonTextWidth(Button button, String text) {
    Rect bounds = new Rect();
    Paint buttonPaint = button.getPaint();
    buttonPaint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.width();
}

From source file:Main.java

public static void drawCenterText(Canvas canvas, String text, Paint paint, Rect rect) {
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (int) (rect.left + rect.width() / 2 - bounds.centerX());
    int y = (int) (rect.top + rect.height() / 2 - bounds.centerY());
    //Log.v(TAG," x="+x + " y="+y + " "+ text);
    canvas.drawText(text, x, y, paint);//from ww  w.  ja  v  a  2 s.  co  m
}

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

/**
 * Method getTextWith() used to get the TextView's Content
 * //from   w  w  w  . j  av a 2  s. com
 * @param textView
 *            The processed TextView.
 * @param text
 *            The Content of TextView
 * @return
 */
public static int getTextWidth(TextView textView, String text) {
    Rect bounds = new Rect();
    Paint textPaint = textView.getPaint();
    textPaint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.width();
}

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

public static Rect measureText(Paint paint, String str) {
    Rect rect = new Rect();
    paint.getTextBounds(str, 0, str.length(), rect);
    return rect;//from w w  w. j  ava2s  .  c o m
}

From source file:Main.java

public static void drawTextCenter(Canvas canvas, RectF rectf, String s, Paint paint) {
    Rect rect = new Rect();
    paint.getTextBounds(s, 0, s.length(), rect);
    canvas.drawText(s, rectf.left + (rectf.width() - (float) rect.width()) / 2.0F,
            rectf.top + (rectf.height() + (float) rect.height()) / 2.0F, paint);
}

From source file:Main.java

/**
 * calculates the approximate height of a text, depending on a demo text
 * avoid repeated calls (e.g. inside drawing methods)
 *
 * @param paint/*from  w ww  .j ava2 s .  c  o  m*/
 * @param demoText
 * @return
 */
public static int calcTextHeight(Paint paint, String demoText) {

    Rect r = new Rect();
    paint.getTextBounds(demoText, 0, demoText.length(), r);
    return r.height();
}

From source file:Main.java

public static void drawTextCenter(Canvas canvas, RectF rectf, String s, String s1, Paint paint, Paint paint1) {
    Rect rect = new Rect();
    paint.getTextBounds(s, 0, s.length(), rect);
    float f = rectf.left + (rectf.width() - (float) rect.width()) / 2.0F;
    float f1 = rectf.top + (rectf.height() + (float) rect.height()) / 2.0F;
    canvas.drawText(s, f, f1, paint);/*from  w w w  .j  a  va 2s  . c o m*/
    Rect rect1 = new Rect();
    paint1.getTextBounds(s, 0, s.length(), rect1);
    canvas.drawText(s1, 6F + (f + (float) rect.width()), (f1 - (float) rect.height()) + (float) rect1.height(),
            paint1);
}

From source file:Main.java

/**
 * convenience method to calculate the y offset for aligning
 * text vertically in its containing Sprite
 *
 * @param {int} yPos y-coordinate of the Sprite / "container" holding the text
 * @param {int} spriteHeight height of the Sprite / "container" holding the text
 * @param {String} text to render//  w ww  .  jav a 2 s .c  om
 * @param {Paint} textPaint the Paint used to render the text
 *
 * @return {int} the y coordinate for drawing the text
 */
public static int alignTextVertically(int yPos, int spriteHeight, String text, Paint textPaint) {
    final Rect bounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), bounds);

    return yPos + (spriteHeight / 2) + (bounds.height() / 2);
}