1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public class CenterVerticalTextSpan extends ReplacementSpan { private int fontSizePx;
public CenterVerticalTextSpan(int fontSizePx) { this.fontSizePx = fontSizePx; }
@Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { text = text.subSequence(start, end); Paint p = getCustomTextPaint(paint); return (int) p.measureText(text.toString()); }
@Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { text = text.subSequence(start, end); Paint p = getCustomTextPaint(paint); Paint.FontMetricsInt fm = p.getFontMetricsInt(); canvas.drawText(text.toString(), x, y - ((y + fm.descent + y + fm.ascent) / 2 - (bottom + top) / 2), p); }
private TextPaint getCustomTextPaint(Paint srcPaint) { TextPaint paint = new TextPaint(srcPaint); paint.setTextSize(fontSizePx); return paint; } }
|