二、代码
代码如下:
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的顺序不要弄反了。