android staticlayout使用讲解

来源:互联网 发布:什么是注解java 编辑:程序博客网 时间:2024/05/20 19:32

使用Canvas的drawText绘制文本是不会自动换行的,即使一个很长很长的字符串,drawText也只显示一行,超出部分被隐藏在屏幕之外。可以逐个计算每个字符的宽度,通过一定的算法将字符串分割成多个部分,然后分别调用drawText一部分一部分的显示, 但是这种显示效率会很低。


StaticLayout是android中处理文字换行的一个工具类,StaticLayout已经实现了文本绘制换行处理。


protected void onCreate(android.os.Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new MyView(this));};public class MyView extends View {public MyView(Context context) {super(context);}public MyView(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);TextPaint tp = new TextPaint();tp.setColor(Color.BLUE);tp.setStyle(Style.FILL);tp.setTextSize(50);   String message = "1964年,保罗·格雷厄姆(Paul Graham)出生于匹兹堡郊区的一个中产阶级家庭。父亲是设计核反应堆的物理学家,母亲在家照看他和他的妹妹。青少年时代,格雷厄姆就开始编程。但是,他还喜欢许多与计算机无关的东西,这在编程高手之中是很少见的。中学时,他喜欢写小说;进入康奈尔大学以后,他主修哲学。后来发现哲学很难理解,于是研究生阶段他就去了哈佛大学计算机系,主攻人工智能。";   android.text.StaticLayout staticLayout =new android.text.StaticLayout(message, tp, canvas.getWidth(),   Alignment.ALIGN_NORMAL, 1.0f, 2.0f, false);   staticLayout.draw(canvas);   canvas.restore();}}

这跟TextView的效果是一样的,其实TextView也是调用StaticLayout来实现换行的。

StaticLayout的构造函数有三个:

  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);    } public StaticLayout(CharSequence source, int bufstart, int bufend,                        TextPaint paint, int outerwidth,                        Alignment align,                        float spacingmult, float spacingadd,                        boolean includepad) {        this(source, bufstart, bufend, paint, outerwidth, align,             spacingmult, spacingadd, includepad, null, 0);    }  public StaticLayout(CharSequence source, int bufstart, int bufend,            TextPaint paint, int outerwidth,            Alignment align,            float spacingmult, float spacingadd,            boolean includepad,            TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {        this(source, bufstart, bufend, paint, outerwidth, align,                TextDirectionHeuristics.FIRSTSTRONG_LTR,                spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);    }

android StaticLayout参数解释

StaticLayout(CharSequence source, int bufstart, int bufend,
           TextPaint paint, int outerwidth,
           Alignment align,
           float spacingmult, float spacingadd,
           boolean includepad,
           TextUtils.TruncateAt ellipsize, int ellipsizedWidth)


1.需要分行的字符串

2.需要分行的字符串从第几的位置开始

3.需要分行的字符串到哪里结束

4.画笔对象

5.layout的宽度,字符串超出宽度时自动换行。

6.layout的对其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种。

7.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。

8.在基础行距上添加多少

实际行间距等于这两者的和。

9.参数未知

10.从什么位置开始省略

11.超过多少开始省略

需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x,y);


相关文章:

Android使用StaticLayout实现文本绘制自动换行


0 0
原创粉丝点击