今天小编跟大家分享一下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的顺序不要弄反了。
以上所述就是Andorid TextView字幕效果以及代码介绍,希望大家能够喜欢,更多内容请继续关注武林技术频道网站!