Detailed explanation of automatic line feed in text drawing with staticlayout in Android

preface

Using canvas's DrawText to draw text will not wrap automatically. Even if a very long string is used, DrawText will only display one line, and the excess part is hidden outside the screen. You can calculate the width of each character one by one, divide the string into multiple parts through a certain algorithm, and then call DrawText to display part by part, but this display efficiency will be very low.

Staticlayout is a tool class in Android for processing text line breaks. Staticlayout has implemented text drawing line breaks. The following is an example of how to use staticlayout:

Sample code

This is the same as the effect of textview. In fact, textview also calls staticlayout to realize line feed.

There are three constructors for staticlayout:

Android staticlayout parameter interpretation

StaticLayout(CharSequence source,

TextPaint paint,

Alignment align,

float spacingmult,

boolean includepad,

TextUtils.TruncateAt ellipsize,int ellipsizedWidth)

1. String that requires a branch

2. Where does the string that needs to be branched start

3. Where does the string that needs a branch end

4. Brush object

5. The width of layout. When the string exceeds the width, it will wrap automatically.

6. Align is used for layout_ CENTER, ALIGN_ NORMAL, ALIGN_ Opposite has three types.

7. Relative line spacing, relative font size, 1.5f indicates that the line spacing is 1.5 times the font height.

8. How much is added to the basic line spacing

The actual line spacing is equal to the sum of the two.

9. Unknown parameter

10. Where do you start to omit

11. More than how much to start omitting

It should be noted that this layout is drawn at the (0,0) point of the canvas by default. If you need to adjust the position, you can only move the starting coordinate of the canvas before the draw

canvas.translate(x,y);

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>