draw Paint Text Align Center - Android Graphics

Android examples for Graphics:Paint

Description

draw Paint Text Align Center

Demo Code


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

public class Main {

    public static void drawTextAlignCenter(Context context, Canvas canvas,
            Paint textPaint, String text, int centerY, int centerX) {
        textPaint.setTextAlign(Paint.Align.CENTER);

        canvas.drawText(text, centerX, getDrawTextY(centerY, textPaint),
                textPaint);//  www .j a v a2 s  .  co  m
    }

    public static int getDrawTextY(int textCenterY, Paint paint) {
        return (int) (textCenterY + 0.3 * getDrawTextHeight(paint));
    }

    public static int getDrawTextHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }
}

Related Tutorials