calculate the width of a string with a specific TextPaint - Android Graphics

Android examples for Graphics:Paint

Description

calculate the width of a string with a specific TextPaint

Demo Code


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

public class Main {
    /**// w w w.  java2 s.  c om
     * calculate the width of a string with a specific TextPaint
     * @param paint
     * @param s
     * @return
     */
    public static int calculateStringWidth(Paint paint, String s) {
        int iRet = 0;
        if (s != null && s.length() > 0) {
            int len = s.length();
            float[] widths = new float[len];
            paint.getTextWidths(s, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }
}

Related Tutorials