虽然介绍的是有关draw text的相关方法,但是一些属性设置还是通过Paint 来设定,毕竟text还是paint 画出来的。
一、paint的方法、canvas.drawText方法以及Typeface的使用
1. paint的方法介绍
Paint.Style.FILL_AND_STROKE :填充内部和描边
Paint.Style.STROKE :描边*/
//drawText 以及样式
mPaint.setStyle(Paint.Style.STROKE); //字体中间不填充
canvas.drawText("一篇诗,一斗酒,一曲长歌,一剑天涯", 100, 100, mPaint);
mPaint.setStyle(Paint.Style.FILL); //字体填充
canvas.drawText("一篇诗,一斗酒,一曲长歌,一剑天涯", 100, 250, mPaint);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE); //字体填充
canvas.drawText("一篇诗,一斗酒,一曲长歌,一剑天涯", 100, 400, mPaint);
mPaint.setUnderlineText(true); //设置下划线
mPaint.setStrikeThruText(true); //设置删除线
mPaint.setTextSkewX((float) -0.25); //倾斜度 - 左, + 右
canvas.drawText("一篇诗,一斗酒,一曲长歌,一剑天涯", 100, 550, mPaint);
mPaint.setTextScaleX(2); //水平拉伸原来的2倍(上一次样式的2倍)
mPaint.setColor(Color.BLUE);
canvas.drawText("一篇诗,一斗酒,一曲长歌,一剑天涯", 1, 5, 100, 700, mPaint); // 0开始不包含5 篇诗,一
效果图如下2. canvas drawText
public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
float vOffset : 与路径中心的垂直偏移量
public void drawPosText(String text, float[] pos, Paint paint)
float[] pos = {80, 80, 200, 1050, 250, 80, 234, 456, 240, 300, 400, 500, 900, 600};
canvas.drawPosText("但愿长醉不复醒", pos, mPaint); //根据位置来写每个字
//canvas.drawPosText("一篇诗,一斗酒,一曲长歌,一剑天涯".toCharArray(), 2, 4, pos, mPaint);
mPaint.setColor(Color.GREEN);
//drawOnPath
RectF mRectF = new RectF(100, 100, 600, 500);
Path mPath = new Path();
mPath.addOval(mRectF, Path.Direction.CW); //顺时针
canvas.drawTextOnPath("一篇诗,一斗酒,一曲长歌,一剑天涯", mPath, 0, 0, mPaint);
效果图如下:
3. Typeface 的使用
Typeface create(String familyName, int style) //直接通过指定字体名来加载系统中自带的文字样式
Typeface create(Typeface family, int style) //通过其它Typeface变量来构建文字样式
Typeface createFromAsset(AssetManager mgr, String path) //通过从Asset中获取外部字体来显示字体样式
Typeface createFromFile(String path)//直接从路径创建
Typeface createFromFile(File path)//从外部路径来创建字体样式
Typeface defaultFromStyle(int style)//创建默认字体
上面的几个个参数会用到Style变量,Style的枚举值如下:
Typeface.NORMAL //正常体
Typeface.BOLD //粗体
Typeface.ITALIC //斜体
Typeface.BOLD_ITALIC //粗斜体
Typeface font = Typeface.create("宋体", Typeface.NORMAL);
mPaint.setTypeface(font);
mPaint.setTextSize(100);
canvas.drawText("Aa", 80, 200, mPaint);
Typeface font1 = Typeface.createFromAsset(getContext().getAssets(), "DancingScript-Bold.ttf");
mPaint.setTypeface(font1);
canvas.drawText("Aa", 80, 400, mPaint);
效果图如下:
二、drawText中的须知特点
ascent: 系统建议的,绘制单个字符时,字符应当的最高高度所在线
descent: 系统建议的,绘制单个字符时,字符应当的最低高度所在线
top: 可绘制的最高高度所在线
bottom: 可绘制的最低高度所在线
1. 画一条基线
Paint paint_line = createPaint(Color.RED, Paint.Style.STROKE, 5);
canvas.drawColor(Color.BLACK);
int x = 300;
int y = 500;
canvas.drawLine(x, y, 1800, 500, paint_line);
// paint_text.setTextAlign(Paint.Align.CENTER); //文字的中间位置在指定点
paint_text.setTextAlign(Paint.Align.LEFT); //默认情况,文字的左边在指定点
// paint_text.setTextAlign(Paint.Align.RIGHT); //以文字的右边在指定点
canvas.drawText("HaaaAgfj", 300, 500, paint_text);
运行结果如下: 指定点在文字的左侧,并且红色就是基线2. 如何判断其他线的位置
public float ascent;
public float bottom;
public float descent;
public float leading;
public float top;
public FontMetrics() {
throw new RuntimeException("Stub!");
}
}
descent = descent线的y座标 - baseline线的y座标;
top = top线的y座标 - baseline线的y座标;
bottom = bottom线的y座标 - baseline线的y座标;
Paint.FontMetrics text_meterics = paint_text.getFontMetrics();
int top = (int) (y + text_meterics.top);
int ascent = (int) (y + text_meterics.ascent);
int descent = (int) (y + text_meterics.descent);
int bottom = (int) (y + text_meterics.bottom);
canvas.drawLine(x, top, 1800, top, paint_line);
canvas.drawLine(x, ascent, 1800, ascent, paint_line);
canvas.drawLine(x, bottom, 1800, bottom, paint_line);
paint_line.setColor(Color.BLUE);
canvas.drawLine(x, descent, 1800, descent, paint_line);
其中bottom和descent的距离比较近,效果图如下:3. 绘制文字矩形
int textWidth = (int) paint_text.measureText("HaaaAgfj");
Rect mRect = new Rect(x, top, x + textWidth, bottom);
canvas.drawRect(mRect, paint_line);
2. 绘制文字的当前区域
Paint paint_rect = createPaint(Color.BLUE, Paint.Style.STROKE, 5);
Rect minRect = new Rect();
paint_rect.getTextBounds("HaaaAgfj",0,"HaaaAgfj".length(),minRect); //paint.getTextBounds()得到基线为y=0的最小矩形的各点座标
minRect.top = y + minRect.top;
minRect.bottom = y + minRect.bottom;
minRect.left = x;
minRect.right = x + textWidth;
canvas.drawRect(minRect,paint_rect);
效果如下: