最近有大佬说,大部分写博客的都是至关网红,想火,吓得咱们这些不想火、不想当网红的人都不敢写博客了java
上一篇,咱们说了绘制路径和简单的文字绘制,这一篇详细说说文字绘制。canvas
绘制文字主要由如下几个方法安全
drawText(String text, float x, float y,Paint paint)
drawText(String text, int start, int end, float x, float y, Paint paint)
drawText(char[] text, int index, int count, float x, float y, Paint paint)
drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
其中,start
、end
表示截取的范围,这里就主要说说第一个方法字体
String text = "abcdefghijk";
paint.setTextSize(144);//px
paint.setColor(Color.BLACK);
canvas.drawLine(300, 300, 1000, 300, paint);
paint.setColor(Color.RED);
canvas.drawText(text, 300, 300, paint);
复制代码
(300,300)
这个点左上角开始,并且还有部分字符超出了这个范围。
开个玩笑,咱们继续分析this
看上去是都是从x位置的右边开始绘制文字的,实际上是由setTextAlign(Align align)
来设置的,Align
有LEFT
、CENTER
、RIGHT
,默认值为LEFT
spa
public enum Align {
/** * The text is drawn to the right of the x,y origin */
LEFT (0),
/** * The text is drawn centered horizontally on the x,y origin */
CENTER (1),
/** * The text is drawn to the left of the x,y origin */
RIGHT (2);
private Align(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
复制代码
RIGHT
code
CENTER
y是基线的位置,上述实例中,文字下的黑线被称为基线。cdn
因此,x坐标、基线位置、文字大小肯定以后,就能够肯定文字的位置。blog
参与肯定位置的其实还有4条安全线:FontMetricsInt
和FontMetrics
,能够经过paint.getFontMetricsInt()
和getFontMetrics
来获取,其本质是同样的,只不过一个返回值是int
,一个是float
get
String text = "abcdefghijk";
paint.setTextSize(144);
paint.setColor(Color.BLACK);
canvas.drawLine(300, 300, 1000, 300, paint);
paint.setColor(Color.RED);
canvas.drawText(text, 300, 300, paint);
int top = paint.getFontMetricsInt().top + 300;
int bottom = paint.getFontMetricsInt().bottom + 300;
int ascent = paint.getFontMetricsInt().ascent + 300;
int descent = paint.getFontMetricsInt().descent + 300;
Log.e("cheng", paint.getFontMetricsInt().toString());
paint.setColor(Color.BLACK);
canvas.drawLine(0, top, 1000, top, paint);
canvas.drawLine(0, bottom, 1000, bottom, paint);
paint.setColor(Color.GREEN);
canvas.drawLine(0, ascent, 1000, ascent, paint);
canvas.drawLine(0, descent, 1000, descent, paint);
复制代码
从上到下,依次是top
、ascent
、descent
、bottom
,其中top
和bottom
分别是可绘制的最高点和最低点,在这个范围内保证能够正确显示;ascent
和descent
分别为本次绘制的最高点和最低点。
关于4条安全线位置,这个取决于使用的字体和字号.。
好比说,一样144px的字号,使用小米兰亭字体时
FontMetricsInt: top=-153 ascent=-134 descent=35 bottom=40 leading=0
使用华康圆体W7字体时,
FontMetricsInt: top=-134 ascent=-150 descent=38 bottom=36 leading=0
本次可绘制线都超出了物理可绘制线,可见,尽可能使用官方字体
可使用如下方法获取
getTextBounds(String text, int start, int end, Rect bounds)
Rect rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);
Log.e("cheng", "onDraw: " + rect.toString());
复制代码
onDraw: Rect(7, -110 - 741, 31)
疑问又来了,怎么是负的?这是由于没有传递基线的位置,因此须要加上基线的位置
Rect rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);
Log.e("cheng", "onDraw: " + rect.toString());
rect.left += 300;
rect.top += 300;
rect.right += 300;
rect.bottom += 300;
paint.setColor(Color.YELLOW);
canvas.drawRect(rect, paint);
复制代码
黄线范围即为最小矩形
以(500,500)位置为中心,绘制文字
String text = "ABCDEFGH";
paint.setTextSize(72);
paint.setTextAlign(Paint.Align.CENTER);
Rect rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);
canvas.drawText(text, 500, (500 + (rect.bottom - rect.top) / 2), paint);
paint.setColor(Color.RED);
canvas.drawCircle(500, 500, 400, paint);
canvas.drawLine(500, 0, 500, 1000, paint);
canvas.drawLine(0, 500, 1000, 500, paint);
复制代码