draw Text By Center Y on Canvas - Android Graphics

Android examples for Graphics:Canvas

Description

draw Text By Center Y on Canvas

Demo Code


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

public class Main {

    public static void drawTextByCenterY(Canvas canvas, Paint paint,
            String text, float x, float centerY) {
        float bottom = paint.getFontMetrics().bottom;
        canvas.drawText(text, x, (float) (0.5 * measureTextHeight(paint)
                - bottom + centerY), paint);
    }//from  ww w . jav  a 2 s  .co  m

    public static float measureTextHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        float height = fm.bottom - fm.top;

        return height;
    }
}

Related Tutorials