首页 > 系统 > Android > 正文

Andorid TextView字幕效果实例

2019-10-24 21:11:16
字体:
来源:转载
供稿:网友
本文将介绍Andorid TextView字幕效果的实现方法,需要的朋友可以参考下
 
一、效果图 
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的顺序不要弄反了。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表