平時(shí)項(xiàng)目里會(huì)涉及到一些文字繪制,這里記錄下自己學(xué)的的。
先來一張網(wǎng)上找的圖,簡單介紹下android是按啥規(guī)則繪制字體的:

FontMetrics
這個(gè)需要結(jié)合Paint.FontMetrics這個(gè)類來看:
/**
* Class that describes the various metrics for a font at a given text size.
* Remember, Y values increase going down, so those values will be positive,
* and values that measure distances going up will be negative. This class
* is returned by getFontMetrics().
*/
public static class FontMetrics {
/**
* The maximum distance above the baseline for the tallest glyph in
* the font at a given text size.
*/
public float top;
/**
* The recommended distance above the baseline for singled spaced text.
*/
public float ascent;
/**
* The recommended distance below the baseline for singled spaced text.
*/
public float descent;
/**
* The maximum distance below the baseline for the lowest glyph in
* the font at a given text size.
*/
public float bottom;
/**
* The recommended additional space to add between lines of text.
*/
public float leading;
}
需要特別關(guān)注的就是BaseLine,像canvas.drawText()里頭的y,就是baseLine的位置,所以,有時(shí)候雖然我們繪制時(shí)參數(shù)y在居中位置,但是實(shí)際體現(xiàn)的效果確不居中,這就是因?yàn)槲淖謺r(shí)基于baseLine來繪制的。這個(gè)時(shí)候的y,就需要根據(jù)文字來特地計(jì)算:
public class SportsView extends View {
private final static float RING_WIDTH = UiUtils.dpToPixel(20);
private final static float RADIUS = UiUtils.dpToPixel(150);
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Rect rect = new Rect();
private Paint.FontMetrics fontMetrics = new Paint.FontMetrics();
public SportsView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
{
paint.setTextSize(UiUtils.dpToPixel(100));
paint.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 繪制環(huán)
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(RING_WIDTH);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, RADIUS, paint);
// 繪制進(jìn)度條
paint.setColor(Color.RED);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS,
getWidth() / 2 + RADIUS, getHeight() / 2 + RADIUS,
-90, 225, false, paint);
paint.setStyle(Paint.Style.FILL);
// // 讀取文字的實(shí)時(shí)繪制范圍,以用于后面如何對baseline進(jìn)行偏移
// paint.getTextBounds("123", 0, "123".length(), rect);
// int offset = (rect.top + rect.bottom) / 2;
// // 會(huì)在baseLine上畫,baseline是一個(gè)穩(wěn)定線,因?yàn)槲淖值膟軸難以確定
// // 文字橫向居中很容易,使用Center即可,但是縱向居中就比較麻煩了
// // 雖然該方法會(huì)讓文字非常居中,但是,當(dāng)文字變化是,可能會(huì)跳動(dòng),因?yàn)槔L制范圍是根據(jù)文字內(nèi)容來計(jì)算的
// canvas.drawText("123", getWidth() / 2, getHeight() / 2 - offset, paint);
// 左對齊情況,如果希望文字完全貼邊,即去除文字訪問的左邊部分
// canvas.drawText("123", - rect.left, 200, paint);
paint.getFontMetrics(fontMetrics);
// 這樣標(biāo)準(zhǔn)比較統(tǒng)一,不會(huì)讓文字跳動(dòng),適用于變動(dòng)場景
float offset = (fontMetrics.ascent + fontMetrics.descent) / 2;
canvas.drawText("123", getWidth() / 2, getHeight() / 2 - offset, paint);
}
}

效果圖