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:文字占用寬度饶套,文字行數(shù)到達這個寬度柜去,自動換行。
align:對齊方式讹躯。
spacingmult:行間距的倍數(shù)治筒。
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(字體微調(diào))
3.1.13setElegantTextHeight(boolean bool) 高度變優(yōu)雅?
3.1.14setSubpixelText(boolean subpixelText) 開啟次像素級的抗鋸齒
3.1.15setLinearText(boolean bool)
3.2.1獲取系統(tǒng)推薦行距爱谁。返回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獲取字符串中每個字符的寬度捐顷,并把結果填入?yún)?shù)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 給出一個位置的像素值徽龟,計算出文字中最進階這個位置的字符偏移量(既第幾個字符最接近這個坐標)。
結合使用唉地,實現(xiàn)(獲取文字點擊處的文字坐標)据悔。
3.3.8hasGlyph(String str) 是否是一個單獨的字形