Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static float getTextWidth(Paint paint, String text, float textSize) {
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        paint.setTextSize(textSize);
        float[] widths = new float[text.length()];
        paint.getTextWidths(text, widths);
        float totalWidth = 0;
        for (int i = 0; i < widths.length; i++) {
            totalWidth += widths[i];
        }
        return totalWidth;
    }
}