get Paint Text Width - Android Graphics

Android examples for Graphics:Paint

Description

get Paint Text Width

Demo Code


//package com.java2s;

import android.graphics.Paint;

import android.graphics.Rect;

public class Main {
    public static int getTextWidth(String text, Paint paint) {
        Rect rect = getTextBounds(text, paint);
        return rect.width();
    }//  w  ww  .j  a v  a2  s  . c o m

    public static Rect getTextBounds(String text, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect;
    }
}

Related Tutorials