get Text Width via Paint - Android Graphics

Android examples for Graphics:Paint

Description

get Text Width via Paint

Demo Code


//package com.java2s;
import android.graphics.Paint;
import android.graphics.Rect;

public class Main {
    public static Rect textBounds = null;

    public static int getTextWidth(String str, Paint paint) {
        int width = 0;
        if (str != null && str.length() != 0) {
            if (textBounds == null) {
                textBounds = new Rect();
            }//from  w  w w. j  a v  a  2  s .c om
            paint.getTextBounds(str, 0, str.length(), textBounds);
            width = textBounds.width();
        }
        return width;
    }
}

Related Tutorials