draw Text By Center X on Canvas - Android Graphics

Android examples for Graphics:Canvas

Description

draw Text By Center X on Canvas

Demo Code


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

public class Main {

    public static void drawTextByCenterX(Canvas canvas, Paint paint,
            String text, float centerX, float y) {
        canvas.drawText(text,/*from ww  w . ja v  a2  s.co m*/
                (float) (centerX - measureTextWidth(text, paint) * 0.5), y,
                paint);
    }

    public static float measureTextWidth(String text, Paint paint) {
        float width = paint.measureText(text);

        return width;
    }
}

Related Tutorials