1.canvas绘制文字的方式
- drawText
- drawTextOnPath
- drawTextRun
staticlayout
layout的一个子类
public StaticLayout(CharSequence source, TextPaint paint,
int width,
Alignment align, float spacingmult, float spacingadd,
boolean includepad) {
this(source, 0, source.length(), paint, width, align,
spacingmult, spacingadd, includepad);
}
source:你要绘制的文字。
paint:画笔。
width:文字占用宽度,文字行数到达这个宽度,自动换行。
align:对齐方式。
spacingmult:行间距的倍数。
spacingadd:行间距额外增加值。
includepad:是否设置额外空间给哪些过高或过低的字符显示。
小demo
String str = "jfjkakskdjfnklajdkfnlkaksdlnflkjdsfadflkjlfalksdkjf";
String str2 = "a\nab\nabc\nabcd\nabcde\n";
StaticLayout staticLayout = new StaticLayout(str, new TextPaint(), 600, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
StaticLayout staticLayout2 = new StaticLayout(str2, new TextPaint(), 600, Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
canvas.save();
canvas.translate(50,100);
staticLayout.draw(canvas);
canvas.translate(0,300);
staticLayout2.draw(canvas);
canvas.restore();
3.paint对文字绘制的辅助
- 1.设置显示效果
- 2.测量文字尺寸
3.1.1设置文字大小
paint.setTextSize(float x);
3.1.2设置文字粗体(伪粗体)只是把字描粗,不是改变width让字变粗。
paint.setFakeBoldText(Boolean bool);
3.1.3设置文字斜体度(0-1f比较好看)-向右上斜,+向左上斜
paint.setSkewX(float x);
3.1.4设置文字下划线
paint.setUnderlineText(Boolean bool);
3.1.5设置文字字间距(0-1f) 默认值是0
paint.setLetterSapcing(float letterSpacing);
3.1.6设置文字删除线
paint.setStrikeThruText(Boolean bool);
3.1.7设置文字字体
paint.setTypeface(Typeface typeface);
可以从文件中也可以从assets中拿到字体
image.png
3.1.8设置文字胖瘦()(0-1f 比较好看)
setTextScaleX()
3.1.11设置文字使用国家,类似于国际化
setTextLocale(Local local);
3.1.10设置文字对齐,左对齐,居中对齐,右对齐。
setTextAlign(Paint.Align align);
还有些不太常用的哦
3.1.9setFontFeatureSetting(String str) 比如:"smcp"用css的font-feature-setting的方式来设置字体
3.1.12setHinting(int mode)是否启用字体的hinting(字体微调)
3.1.13setElegantTextHeight(boolean bool) 高度变优雅?
3.1.14setSubpixelText(boolean subpixelText) 开启次像素级的抗锯齿
3.1.15setLinearText(boolean bool)
3.2.1获取系统推荐行距。返回float值。好看啊
getFontSpacing()
3.2.2获取FontMetrics 返回float值,有五条线 返回FontMestrics
top,ascent,dscent,baseline,bottom
getFontMestrics()
3.3.3获取文字的显示范围 获取的文字范围加入bounds中。无返回值
getTextBounds(String str,int startX,int startY,Rect bounds);
3.3.4测量文字的宽度并返回
getMeasureText(String str);
比较getTextBounds 一个文字外面紧贴的矩形,一个是文字占用的宽度。
3.3.5获取字符串中每个字符的宽度,并把结果填入参数widths中 包含字间距哦
getTextWidth(String str,float[] widhts);
3.3.6 breakText
int breakText(String text,boolean measureForwards,float maxWidth,float[] measureWidth);
也是测量文字宽度 不过设了最大宽度,超出了就截断。没超过返回文字最大长度 可用于文字折行。
String str = "asldfjlsdflsdajflsdjflasdlksjdljsdafslkdfjlksdjflsdjflksdjflsdaflkasdjlasdj";
float[] a = {0};
int count = 0;
int y = 0;
int length = str.length();
do{
int num = paint.breakText(str, count,length, true, 300, a);
canvas.drawText(str,count,count+=num,200,y+=paint.getFontSpacing(),paint);
}while(length-count >0);

3.3.7光标相关。
1.getRunAdvance(String str,int start,int end,int contextStart,int contextEnd,boolean isRtl,int offset)
isRtl:文字方向
offset:测量哪一个字符
2.getOffsetForAdvance 给出一个位置的像素值,计算出文字中最进阶这个位置的字符偏移量(既第几个字符最接近这个坐标)。
结合使用,实现(获取文字点击处的文字坐标)。
3.3.8hasGlyph(String str) 是否是一个单独的字形
网友评论