启动页广告倒计时View

上一篇我们了解了自定义view用户圆弧实现计步view,今天我们继续巩固,实现App的启动页广告倒计view
1. 显示倒计时的圆弧(效果图中的红色圆弧)
2. 按钮圆
3. 倒计时数字
4. 设置倒计时
5. 倒计时圆弧动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Created by zhangfeng on 2016/10/27.
* 广告倒计时 view
*/
public class AVDCountZeroView extends View {
private Paint paint;
private float paintWidth = 5f;
private float centerX;
private float outRadius = dipToPx(20);
private float insideRadius = dipToPx(19);
private float textSize = dipToPx(15);
private int countZeroNumber;
private String describeStr = "跳过";
private float currentCountZeroArc = 360;
public AVDCountZeroView(Context context) {
super(context);
init();
}
public AVDCountZeroView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public AVDCountZeroView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
centerX = getWidth() / 2;
drawCountZeroCircle(canvas);
drawShowContent(canvas);
drawCountZeroText(canvas);
drawCountZeroNumber(canvas);
}
/**
* 绘制倒计时圆弧
*
* @param canvas
*/
private void drawCountZeroCircle(Canvas canvas) {
paint.setStrokeWidth(paintWidth);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
RectF rectF = new RectF(centerX - outRadius, centerX - outRadius, centerX + outRadius, centerX + outRadius);
canvas.drawArc(rectF, 0, currentCountZeroArc, false, paint);
}
/**
* 绘制显示内容圆
*
* @param canvas
*/
private void drawShowContent(Canvas canvas) {
paint.setStrokeWidth(paintWidth);
paint.setColor(Color.LTGRAY);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(centerX, centerX, insideRadius, paint);
}
/**
* 绘制描述文字
*
* @param canvas
*/
private void drawCountZeroText(Canvas canvas) {
paint.setTextSize(textSize);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
Rect rect = new Rect();
paint.getTextBounds(describeStr + "", 0, String.valueOf(describeStr).length(), rect);
canvas.drawText(describeStr + "", centerX - rect.width() / 2, centerX + rect.height() / 2, paint);
}
/**
* 绘制倒计时数字
*
* @param canvas
*/
private void drawCountZeroNumber(Canvas canvas) {
paint.setTextSize(textSize);
paint.setColor(Color.GRAY);
paint.setStyle(Paint.Style.FILL);
Rect rect = new Rect();
String tempStr = String.valueOf(countZeroNumber) + "秒";
paint.getTextBounds(tempStr, 0, tempStr.length(), rect);
canvas.drawText(tempStr, centerX - rect.width() / 2, centerX + rect.height() / 2 + dipToPx(35), paint);
}
/**
* dp 转换成px
*
* @param dip
* @return
*/
private int dipToPx(float dip) {
float density = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * density + 0.5f * (dip >= 0 ? 1 : -1));
}
/**
* 数字倒计时
*
* @param number
*/
public void setCountZeroNumber(final int number) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = number; i >= 0; i--) {
countZeroNumber = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
setAinmation(360, 0, number * 1000);
}
/**
* 倒计时外圆动画
*
* @param last
* @param current
* @param length
*/
private void setAinmation(float last, float current, int length) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(last, current);
valueAnimator.setDuration(length);
valueAnimator.setTarget(currentCountZeroArc);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentCountZeroArc = (float) animation.getAnimatedValue();
postInvalidate();
}
});
valueAnimator.start();
}
}


广告倒计时