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:com.zoffcc.applications.zanavi.Navit.java

synchronized static int find_max_font_size_for_height(String sample_text, int height, int max_font_size,
        int padding_in_dp) {
    String s = sample_text;/*ww  w. ja v a2 s.  c o  m*/
    int bh = 0;
    Paint p = new Paint();
    Rect bounds = new Rect();
    p.setTextSize(max_font_size);
    // p.measureText(s);
    p.getTextBounds(s, 0, s.length(), bounds);

    int ret_font_size = max_font_size;

    int loop_counter_max = 400;
    int loop_counter = 0;
    int padding_in_px = 0;
    if (padding_in_dp > 0) {
        padding_in_px = NavitGraphics.dp_to_px(padding_in_dp);
    }

    bh = bounds.height();
    //System.out.println("bh(1)=" + bh);
    while ((bh + padding_in_px) > height) {
        loop_counter++;
        if (loop_counter > loop_counter_max) {
            break;
        }

        ret_font_size--;
        p.setTextSize(ret_font_size);
        // p.measureText(s);
        p.getTextBounds(s, 0, s.length(), bounds);
        bh = bounds.height();
    }

    return ret_font_size;
}