【Andorid X 项目笔记】TextView字幕效果(3)
前言
【Andorid X 项目笔记】
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
系列
1、【Andorid X 项目笔记】禁用ListView的Fling功能(1)
2、【Andorid X 项目笔记】动态设置ViewPager的Adapter问题(2)
正文
一、效果图
二、代码
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();
}
}
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的顺序不要弄反了。