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);

达到裁剪效果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值