-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgressView.java
More file actions
136 lines (111 loc) · 3.04 KB
/
Copy pathProgressView.java
File metadata and controls
136 lines (111 loc) · 3.04 KB
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
package cn.luliangdev.devprogressview;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
/**
* ProgressView
* Created by LuLiang on 2018/8/4.
*/
public class ProgressView extends View {
float progress = 0;
float radius = 0;
long duration = 1000;
int color = 0xff000000;
private Paint paint;
Context context;
private ObjectAnimator animator;
public ProgressView(Context context) {
super(context);
this.context = context;
initView();
}
public ProgressView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
initView();
}
public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
initView();
}
private void initView() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
animator = new ObjectAnimator();
//设置动画属性
animator.setPropertyName("progress");
//设置执行动画的View
animator.setTarget(this);
}
@SuppressLint({"NewApi", "DrawAllocation"})
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setAntiAlias(true); //设置画笔为无锯齿
paint.setColor(color); //设置画笔颜色
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(1);
RectF rectF = new RectF(0, 0, progress, getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
}
/**
* set view background color
*
* @param color
*/
public void setColor(int color) {
this.color = color;
}
/**
* set view round radius
*
* @param radius
*/
public void setRadius(float radius) {
this.radius = radius;
}
/**
* set anim duration
*
* @param duration
*/
public void setDuration(long duration) {
this.duration = duration;
}
public float getProgress() {
return progress;
}
/**
* set progress
*
* @param progress
*/
public void setProgress(float progress) {
this.progress = progress;
invalidate();
}
/**
* 返回属性动画实例,可用于动画进度监听做后续操作
* @return
*/
public ObjectAnimator getAnimator() {
return animator;
}
//
public void startAnim() {
if (animator.isRunning()) animator.end();
//设置进度数组, 0 - max
animator.setFloatValues(0, progress);
//设置动画时间
animator.setDuration(duration);
//动画开启
animator.start();
}
}