这篇文章将为大家详细讲解有关Android使用view实现倒计时控件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。



直接上代码
自定义TextView
文字展示
public class StrokeTextView extends TextView {
private TextView borderText = null;///用于描边的TextView
private Context mContext;
public StrokeTextView(Context context) {
super(context);
mContext = context;
borderText = new TextView(context);
init();
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
borderText = new TextView(context, attrs);
init();
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
borderText = new TextView(context, attrs, defStyle);
init();
}
public void init() {
TextPaint tp1 = borderText.getPaint();
tp1.setStrokeWidth(12); //设置描边宽度
tp1.setStyle(Paint.Style.STROKE); //对文字只描边
//设置自定义字体
Typeface fromAsset = Typeface.createFromAsset(mContext.getAssets(), "fonts/Alibaba-PuHuiTi-Heavy.ttf");
borderText.setTypeface(fromAsset, Typeface.ITALIC); //自定义字体 ITALIC斜体
borderText.setTextColor(Color.parseColor("#F46059")); //设置描边颜色
borderText.setShadowLayer(3.0F, 2F, 2F, Color.parseColor("#ffd44042")); //设置阴影效果(投影)
borderText.setGravity(getGravity());
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
borderText.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CharSequence tt = borderText.getText();
//两个TextView上的文字必须一致
if (tt == null || !tt.equals(this.getText())) {
borderText.setText(getText());
this.postInvalidate();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
borderText.measure(widthMeasureSpec, heightMeasureSpec);
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
borderText.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
borderText.draw(canvas);
super.onDraw(canvas);
}
}