canvas.drawText淺析

❀卜卜ღ?Bruce發表於2019-03-12

public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)

繪製文字的時候,引數X,Y並不是繪製的起點,而是文字的底部
比如繪製A,這個x,y 是A的左下角的座標,下圖 baseline起點,而不是右上角的座標。
獲取text的長度 使用paint.measureText(text);方法測量文字的長度
獲取text的高度 使用float height = paint.ascent()+paint.descent(); 也可以使用Paint類的getTextBounds方法測量字的邊框
關於字型引數可以檢視下面圖片。

FontMetrics.png