標籤:字型 restore 過程 static int raw size imp 寬度
StaticLayout。這個也是使用 Canvas 來進行文字的繪製,不過並不是使用 Canvas 的方法。
Canvas.drawText() 只能繪製單行的文字,而不能換行。它:
不能在 View 的邊緣自動折行
taticLayout 的構造方法是 StaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad),其中參數裡:
width 是文字地區的寬度,文字到達這個寬度後就會自動換行;
align 是文字的對齊方向;
spacingmult 是行間距的倍數,通常情況下填 1 就好;
spacingadd 是行間距的額外增加值,通常情況下填 0 就好;
includeadd 是指是否在文字上下添加額外的空間,來避免某些過高的字元的繪製出現越界。
如果你需要進行多行文字的繪製,並且對文字的排列和樣式沒有太複雜的花式要求,那麼使用 StaticLayout 就好。
TextPaint textPaint =new TextPaint(); textPaint.setTextSize(40); //是否使用偽粗體 之所以叫偽粗體( fake bold ),因為它並不是通過選用更高 weight 的字型讓文字變粗,而是通過程式在運行時把文字給「描粗」了。 textPaint.setFakeBoldText(true); //是否加底線 textPaint.setUnderlineText(true); //是否加刪除線 textPaint.setStrikeThruText(true); String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."; StaticLayout staticLayout = new StaticLayout(text,textPaint,600, Layout.Alignment.ALIGN_NORMAL, 1,0,true); String text2 = "a\nbc\ndefghi\njklm\nnopqrst\nuvwx\nyz"; StaticLayout staticLayout2 = new StaticLayout(text2, textPaint, 600, Layout.Alignment.ALIGN_NORMAL, 1, 0, true); canvas.save(); canvas.translate(50,100); staticLayout.draw(canvas); canvas.translate(0,200); staticLayout2.draw(canvas); canvas.restore();
android staticlayout文字繪製