|
@ -0,0 +1,144 @@
|
|
1
|
package com.electric.chargingpile.view;
|
|
2
|
|
|
3
|
import android.content.Context;
|
|
4
|
import android.content.res.TypedArray;
|
|
5
|
import android.graphics.Canvas;
|
|
6
|
import android.graphics.Color;
|
|
7
|
import android.graphics.Paint;
|
|
8
|
import android.graphics.RectF;
|
|
9
|
import android.graphics.Shader;
|
|
10
|
import android.graphics.SweepGradient;
|
|
11
|
import android.util.AttributeSet;
|
|
12
|
import android.view.View;
|
|
13
|
import android.view.animation.Animation;
|
|
14
|
import android.view.animation.Transformation;
|
|
15
|
|
|
16
|
import com.electric.chargingpile.R;
|
|
17
|
|
|
18
|
public class CircleProgressBar extends View {
|
|
19
|
private Context context;
|
|
20
|
private Paint paint;
|
|
21
|
private Paint bgPaint;
|
|
22
|
private float mStrokeWidth;
|
|
23
|
|
|
24
|
private int contentWidth = 100;
|
|
25
|
private int contentHeight = 100;
|
|
26
|
private int defaultBgColor = 0xffefefef;
|
|
27
|
private int sweepAngle;
|
|
28
|
private int animAngle;
|
|
29
|
private ProgressBarAnimation anim;
|
|
30
|
|
|
31
|
|
|
32
|
private int
|
|
33
|
startColor = Color.parseColor("#3CBFF8"),
|
|
34
|
endColor = Color.parseColor("#64E1B5"),
|
|
35
|
intermediateOne = Color.parseColor("#41C6E8"),
|
|
36
|
intermediateTwo = Color.parseColor("#48D0D0"),
|
|
37
|
intermediateThree = Color.parseColor("#54DABD");
|
|
38
|
|
|
39
|
|
|
40
|
public CircleProgressBar(Context context) {
|
|
41
|
super(context);
|
|
42
|
this.context = context;
|
|
43
|
init(null, 0);
|
|
44
|
}
|
|
45
|
|
|
46
|
public CircleProgressBar(Context context, AttributeSet attrs) {
|
|
47
|
super(context, attrs);
|
|
48
|
this.context = context;
|
|
49
|
init(attrs, 0);
|
|
50
|
}
|
|
51
|
|
|
52
|
public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
53
|
super(context, attrs, defStyleAttr);
|
|
54
|
this.context = context;
|
|
55
|
init(attrs, defStyleAttr);
|
|
56
|
}
|
|
57
|
|
|
58
|
private void init(AttributeSet attrs, int defStyle) {
|
|
59
|
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
|
|
60
|
mStrokeWidth = typedArray.getDimension(R.styleable.CircleProgressBar_circleStrokeWidth, 5);
|
|
61
|
defaultBgColor = typedArray.getColor(R.styleable.CircleProgressBar_bgProgressBarColor, 0xffefefef);
|
|
62
|
typedArray.recycle();
|
|
63
|
anim = new ProgressBarAnimation();
|
|
64
|
}
|
|
65
|
|
|
66
|
|
|
67
|
private void initPaint() {
|
|
68
|
paint = new Paint();
|
|
69
|
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
|
|
70
|
paint.setStrokeWidth(mStrokeWidth);
|
|
71
|
paint.setStyle(Paint.Style.STROKE);
|
|
72
|
paint.setStrokeCap(Paint.Cap.ROUND);
|
|
73
|
|
|
74
|
int[] mColors = new int[]{intermediateOne, intermediateTwo, intermediateThree, endColor, startColor, intermediateOne};
|
|
75
|
float[] positions = new float[]{0f, 0.25f, 0.5f, 0.75f, 0.75f, 1f};
|
|
76
|
Shader s = new SweepGradient(contentWidth / 2, contentHeight / 2, mColors, positions);
|
|
77
|
paint.setShader(s);
|
|
78
|
|
|
79
|
bgPaint = new Paint();
|
|
80
|
bgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
|
|
81
|
bgPaint.setStrokeWidth(mStrokeWidth);
|
|
82
|
bgPaint.setStyle(Paint.Style.STROKE);
|
|
83
|
bgPaint.setColor(defaultBgColor);
|
|
84
|
}
|
|
85
|
|
|
86
|
|
|
87
|
@Override
|
|
88
|
protected void onDraw(Canvas canvas) {
|
|
89
|
super.onDraw(canvas);
|
|
90
|
contentWidth = getWidth();
|
|
91
|
contentHeight = getHeight();
|
|
92
|
initPaint();
|
|
93
|
/**
|
|
94
|
* left - The X coordinate of the left side of the rectagle
|
|
95
|
* top - The Y coordinate of the top of the rectangle
|
|
96
|
* right - The X coordinate of the right side of the rectagle
|
|
97
|
* bottom - The Y coordinate of the bottom of the rectangle
|
|
98
|
*/
|
|
99
|
RectF oval = new RectF(0 + mStrokeWidth / 2, 0 + mStrokeWidth / 2, 0 + contentWidth - mStrokeWidth / 2, 0 + contentHeight - mStrokeWidth / 2);
|
|
100
|
canvas.drawArc(oval, 0, 360, false, bgPaint);
|
|
101
|
canvas.drawArc(oval, -90, sweepAngle, false, paint);
|
|
102
|
}
|
|
103
|
|
|
104
|
@Override
|
|
105
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
106
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
107
|
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
|
|
108
|
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
|
|
109
|
|
|
110
|
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
|
|
111
|
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
|
|
112
|
|
|
113
|
if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
|
|
114
|
setMeasuredDimension(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
|
|
115
|
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
|
|
116
|
setMeasuredDimension(getMeasuredWidth() / 2, heightSpecSize);
|
|
117
|
} else if (heightSpecMode == MeasureSpec.AT_MOST) {
|
|
118
|
setMeasuredDimension(widthSpecSize, getMeasuredHeight() / 2);
|
|
119
|
}
|
|
120
|
}
|
|
121
|
|
|
122
|
/**
|
|
123
|
* @author loonggg
|
|
124
|
*/
|
|
125
|
public class ProgressBarAnimation extends Animation {
|
|
126
|
public ProgressBarAnimation() {
|
|
127
|
|
|
128
|
}
|
|
129
|
|
|
130
|
@Override
|
|
131
|
protected void applyTransformation(float interpolatedTime,
|
|
132
|
Transformation t) {
|
|
133
|
super.applyTransformation(interpolatedTime, t);
|
|
134
|
sweepAngle = (int) (interpolatedTime * animAngle);
|
|
135
|
postInvalidate();
|
|
136
|
}
|
|
137
|
}
|
|
138
|
|
|
139
|
public void setProgress(float progress, int time) {
|
|
140
|
animAngle = (int) (progress * 360f);
|
|
141
|
anim.setDuration(time);
|
|
142
|
this.startAnimation(anim);
|
|
143
|
}
|
|
144
|
}
|