get the width for a string with specified paint - Android android.graphics

Android examples for android.graphics:Paint

Description

get the width for a string with specified paint

Demo Code

import android.graphics.Rect;
import android.text.TextPaint;

public class Main{

    /**//from  w w  w  . j  a  v a  2 s. co m
     * get the width for a string with specified paint.
     * @Title: getStringsWidth 
     * @Description: TODO
     * @param paint
     * @param text
     * @return string width
     * @return: int
     */
    public static int getStringsWidth(TextPaint paint, String text) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.width();
    }

}

Related Tutorials