Android文字绘制Staticlayout

一.Staticlayout的构造函数:

public StaticLayout(CharSequence source, TextPaint paint, int width, Alignment align, float spacingmult, float spacingadd, boolean includepad) {
    super((CharSequence)null, (TextPaint)null, 0, (Alignment)null, 0.0F, 0.0F);
    throw new RuntimeException("Stub!");
}

public StaticLayout(CharSequence source, int bufstart, int bufend, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, boolean includepad) {
    super((CharSequence)null, (TextPaint)null, 0, (Alignment)null, 0.0F, 0.0F);
    throw new RuntimeException("Stub!");
}

public StaticLayout(CharSequence source, int bufstart, int bufend, TextPaint paint, int outerwidth, Alignment align, float spacingmult, float spacingadd, boolean includepad, TruncateAt ellipsize, int ellipsizedWidth) {
    super((CharSequence)null, (TextPaint)null, 0, (Alignment)null, 0.0F, 0.0F);
    throw new RuntimeException("Stub!");
}

source:文本内容

paint:画笔

width:文本内容长度超过该数值则换行

align:对齐方式

spacingmult:行距,表示字体高度的倍数,默认是1,小于1则是减少行距,大于则是增加行距

spacingadd:行距,表示行间距增加的距离(与spacingmult配合使用)

includepad:是否留白



其中paint可以设置字体类型,setTypeface(),不同字体类型有着不同的绘制规则。



重要的属性有:

top,bottom,ascent,descent,baseline

通过Paint.getFontMetricsInt()可以获取到这些值,例如:mPaint.getFontMetricsInt().baseline

  • baseline:基准点,字符在TextView中的基准点,字符的绘制就是通过这个基准点来绘制的,相当于字符的零点,top,bottom,ascent,descent的值就是以这个为零点来得到的,在baseline上面的top和ascent是负数,在baseline下面的bottom和descent是正数

  • top:是指的是最高字符到baseline的值,即ascent的最大值,为负数

  • ascent:是baseline之上至字符最高处的距离,为负数

  • bottom:是指最低字符到baseline的值,即descent的最大值,为正数

    descent:是baseline之下至字符最低处的距离,为正数


二.裁剪

自定义view时,可使用Staticlayout.draw(canvas);

如果需要裁剪,则:

Staticlayout staticLayout = new StaticLayout(...);

canvas.clip(...);

staticLayout.draw(canvas);

达到裁剪效果

猜你喜欢

转载自blog.csdn.net/u012588160/article/details/80999115