draw Paint Text Align Right - Android Graphics

Android examples for Graphics:Paint

Description

draw Paint Text Align Right

Demo Code


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

public class Main {

    public static void drawTextAlignRight(Context context, Canvas canvas,
            Paint textPaint, String text, int centerY, int right) {
        textPaint.setTextAlign(Paint.Align.RIGHT);

        canvas.drawText(text, right, getDrawTextY(centerY, textPaint),
                textPaint);/*from  ww w .  j  a  va 2  s  . c o  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