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 Rect getTextRect(String text, float size) {
    Paint pFont = new Paint();
    pFont.setTextSize(size);//from ww w  . j  av a 2s .c o m
    Rect rect = new Rect();
    pFont.getTextBounds(text, 0, text.length(), rect);
    return rect;
}

From source file:Main.java

public static Rect getTextBounds(float textSize, String text) {
    Rect bounds = new Rect();
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);//from  ww  w.j av  a2 s  .co m

    paint.getTextBounds(text, 0, text.length(), bounds);

    bounds.height();
    return bounds;
}

From source file:Main.java

public static Rect getTextRect(String text, Paint paint) {
    Rect rect = new Rect();
    if (!TextUtils.isEmpty(text)) {
        paint.getTextBounds(text, 0, text.length(), rect);
    }// w w w.jav  a  2  s .  c om
    return rect;
}

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  www . j  a v  a2  s . co m*/
 * @param demoText
 * @return
 */
public static int calcTextHeight(Paint paint, String demoText) {

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

From source file:Main.java

public static int getTextHeightByBounds(String text, float textSize) {
    Paint paint = new Paint();
    Rect bounds = new Rect();
    paint.setTextSize(textSize);//from w w w  .  ja v  a  2 s  .c om
    paint.getTextBounds(text, 0, text.length(), bounds);
    return bounds.height();
}

From source file:Main.java

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

    final float testTextSize = 48f;
    paint.setTextSize(testTextSize);// w w w.j  a  v  a 2s.c o m
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float desiredTextSize = testTextSize * desiredWidth / bounds.width();
    paint.setTextSize(desiredTextSize);
}

From source file:Main.java

public static float setTextSizeForWidth(Paint paint, float desiredWidth, String text, float max, float min) {
    final float textSize = 12.0f;

    paint.setTextSize(textSize);//from ww  w . j  ava  2 s . co m
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float desiredTextSize = textSize * desiredWidth / bounds.width();
    if (desiredTextSize > max) {
        desiredTextSize = max;
    } else if (desiredTextSize < min) {
        desiredTextSize = min;
    }
    paint.setTextSize(desiredTextSize);
    return desiredTextSize;
}

From source file:Main.java

public static Rect getTextRec(String text, Paint paint, float textSize) {
    Rect rect = new Rect();
    if (paint != null && text != null) {
        if (0 < textSize) {
            paint.setTextSize(textSize);
        }//from   ww  w .  j av  a  2  s .  c o  m
        paint.getTextBounds(text, 0, text.length(), rect);
    }
    return rect;
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w  ww. j  a v a2 s .c o m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    return new BitmapDrawable(res, buffer);
}

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w ww  .j  ava  2s  .co  m*/
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}