Here you can find the source of drawTextAtCanvasCenter(Canvas canvas, Paint paint, String text)
public static void drawTextAtCanvasCenter(Canvas canvas, Paint paint, String text)
//package com.java2s; import android.graphics.Canvas; import android.graphics.Paint; public class Main { public static void drawTextAtCanvasCenter(Canvas canvas, Paint paint, String text) {// w w w . jav a2s.com if (text == null) { return; } final int width = canvas.getWidth(); final int height = canvas.getHeight(); int offsetX, offsetY; offsetX = calcXWhenAlignCenter(width, paint, text); offsetY = calcYWhenAlignCenter(height, paint); canvas.drawText(text, offsetX, offsetY, paint); } public static int calcXWhenAlignCenter(int width, Paint paint, String string) { if (paint == null) { return 0; } return (int) ((width - paint.measureText(string)) / 2); } public static int calcYWhenAlignCenter(int height, Paint paint) { if (paint == null) { return 0; } final float fontHeight = paint.getFontMetrics().bottom - paint.getFontMetrics().top; return (int) ((height - fontHeight) / 2 - paint.getFontMetrics().top); } }