前言
【Andorid X 项目笔记】
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com农民伯伯: http://over140.cnblogs.com
系列
1、
2、
正文
一、效果图
![](https://images.cnblogs.com/cnblogs_com/over140/2012/11/2012-11-21_1.png)
二、代码
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的顺序不要弄反了。