Andorid TextView字幕效果实例

时间:2021-08-29 07:00:13

一、效果图
Andorid TextView字幕效果实例
二、代码

复制代码 代码如下:


public class textsubview extends textview {

private textpaint mpaint;

public textsubview(context context, attributeset attrs) {
super(context, attrs);

mpaint = new textpaint(getpaint());
mpaint.setstyle(textpaint.style.stroke);
mpaint.setshadowlayer(2.0f, 2.0f, 2.0f, color.red);
}

@override
protected void ondraw(canvas canvas) {
super.ondraw(canvas);

canvas.save();
canvas.cliprect(0, 0, 55, getbottom());
canvas.drawtext(gettext().tostring(), 0, getbaseline(), mpaint);
canvas.restore();
}
}


代码说明

关键是setshadowlayer设置阴影效果以及ondraw的四行代码,大家可以搜一下"android cliprect"了解一下这个函数的作用,注意cliprect与drawtext的顺序不要弄反了。