a
554325746@qq.com
2020-01-15 956063ff14bc75e3a2a97c7bcaa06b9edc84ad24
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.basic.security.widget;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
 
public class ClusterRoundView extends View implements Animation.AnimationListener {
    private float mSweepAngle = 1;
    private float mOldAngle = 0;
    private float mNewAngle = 0;
    private float mRingWidth = 0;
    private int mRingColor = 0;
    private float mProgressRingWidth = 0;
    private int mProgressRingStartColor = 0;
    private int mProgressRingEndColor = 0;
    private float mRadius = 0;
    private Paint mProgressRingPaint;
    private Shader mProgressRingShader;
    private Paint mRingPaint;
    private Paint mTextPaint;
    private int mTextColor;
    private float mTextSize;
    private int[] arcColors = {};
    private BarAnimation barAnimation;
    private ScaleAnimation scaleAnimation;
    private boolean isAnimation = false;
 
    public ClusterRoundView(Context context) {
        this(context, null);
    }
 
    public ClusterRoundView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public ClusterRoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
 
    public ClusterRoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, com.basic.security.utils.RUtils.R_styleable_ClusterRoundView);
        mRingWidth = typedArray.getDimension(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_ring_width, 0);
        mRingColor = typedArray.getColor(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_ring_color, Color.parseColor("#CBCBCB"));
        mProgressRingWidth = typedArray.getDimension(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_progress_ring_width, 0);
        mProgressRingStartColor = typedArray.getColor(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_progress_ring_start_color, Color.parseColor("#086ab5"));
        mProgressRingEndColor = typedArray.getColor(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_progress_ring_end_color, Color.parseColor("#21cbe2"));
        mRadius = typedArray.getDimension(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_android_radius, 0);
        mTextColor = typedArray.getColor(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_text_color, Color.parseColor("#086ab5"));
        mTextSize = typedArray.getDimension(com.basic.security.utils.RUtils.R_styleable_ClusterRoundView_text_size, dp2px(context, 40));
        typedArray.recycle();
        init();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
 
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }
 
    private void init() {
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setColor(mRingColor);
        arcColors = new int[]{mProgressRingStartColor, mProgressRingEndColor};
        mProgressRingPaint = new Paint();
        mProgressRingPaint.setAntiAlias(true);
        mProgressRingPaint.setStyle(Paint.Style.STROKE);
        mProgressRingPaint.setStrokeWidth(mProgressRingWidth);
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);
        barAnimation = new BarAnimation();
        barAnimation.setAnimationListener(this);
        scaleAnimation = new ScaleAnimation();
        scaleAnimation.setDuration(100);
    }
 
    public void setAngle(final float newAngle) {
        setAngle(newAngle, true);
    }
 
    public void setAngle(final float newAngle, boolean isScale) {
        if (!isAnimation) {
            if (isScale) {
                scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    public void onAnimationStart(Animation animation) {
                    }
 
                    public void onAnimationEnd(Animation animation) {
                        changeAngle(newAngle);
                    }
 
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                startAnimation(scaleAnimation);
            } else {
                changeAngle(newAngle);
            }
        }
    }
 
    private void changeAngle(float newAngle) {
        mOldAngle = mNewAngle;
        mNewAngle = newAngle;
        int duration = (int) (Math.abs(mNewAngle - mOldAngle) * 15);
        barAnimation.setDuration(duration);
        barAnimation.setInterpolator(new DecelerateInterpolator());
        startAnimation(barAnimation);
    }
 
    @SuppressLint("DrawAllocation")
    public void onDraw(Canvas canvas) {
        float x = getWidth() / 2;
        float y = getHeight() / 2;
        float radius = mRadius == 0 ? Math.min(getWidth() / 2, getHeight() / 2) : mRadius;
        float ringWidth = mRingWidth == 0 ? radius / 5 : mRingWidth;
        radius = radius - ringWidth / 2;
        mRingPaint.setStrokeWidth(ringWidth);
        canvas.drawCircle(x, y, radius, mRingPaint);
        if (mProgressRingShader == null) {
            mProgressRingShader = new SweepGradient(x, y, arcColors, null);
            mProgressRingPaint.setShader(mProgressRingShader);
        }
        float progressRingWidth = mProgressRingWidth == 0 ? ringWidth : mProgressRingWidth;
        mProgressRingPaint.setStrokeWidth(progressRingWidth);
        float left = x - radius;
        float top = y - radius;
        float right = x + radius;
        float bottom = y + radius;
        double radian = radianToAngle(radius);
        double degrees = Math.toDegrees(-2 * Math.PI / 360 * (90 + radian));
        canvas.save();
        canvas.rotate((float) degrees, x, y);
        canvas.drawArc(new RectF(left, top, right, bottom), (float) radian, mOldAngle + mSweepAngle, false, mProgressRingPaint);
        canvas.restore();
        int percentage = (int) ((mOldAngle + mSweepAngle) / 360 * 100);
        float sizeHeight = getFontHeight();
        canvas.drawText(percentage + "%", x, y + sizeHeight / 2, mTextPaint);
        super.onDraw(canvas);
    }
 
    public float getFontHeight() {
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        return fm.descent - fm.ascent;
    }
 
    private double radianToAngle(float radios) {
        double aa = mProgressRingWidth / 2 / radios;
        double asin = Math.asin(aa);
        return Math.toDegrees(asin);
    }
 
    public void onAnimationStart(Animation animation) {
        isAnimation = true;
    }
 
    public void onAnimationEnd(Animation animation) {
        isAnimation = false;
    }
 
    public void onAnimationRepeat(Animation animation) {
    }
 
    private int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }
 
    public class ScaleAnimation extends Animation {
        private int centerX;
        private int centerY;
 
        public ScaleAnimation() {
        }
 
        public void initialize(int width, int height, int parentWidth,
                               int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            centerX = width / 2;
            centerY = height / 2;
        }
 
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 0.25f) {
                float ss = interpolatedTime * 0.4f + 1f;
                Matrix matrix = t.getMatrix();
                matrix.setScale(ss, ss, centerX, centerY);
            } else if (interpolatedTime >= 0.25f && interpolatedTime < 0.5f) {
                float ss = (0.5f - interpolatedTime) * 0.4f + 1f;
                Matrix matrix = t.getMatrix();
                matrix.setScale(ss, ss, centerX, centerY);
            } else if (interpolatedTime >= 0.5f && interpolatedTime < 0.75f) {
                float ss = (0.75f - interpolatedTime) * 0.4f + 0.9f;
                Matrix matrix = t.getMatrix();
                matrix.setScale(ss, ss, centerX, centerY);
            } else if (interpolatedTime >= 0.75f && interpolatedTime <= 1f) {
                float ss = interpolatedTime * 0.4f + 0.6f;
                Matrix matrix = t.getMatrix();
                matrix.setScale(ss, ss, centerX, centerY);
            }
        }
    }
 
    public class BarAnimation extends Animation {
        public BarAnimation() {
        }
 
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (mNewAngle - mOldAngle >= 0) {
                mSweepAngle = interpolatedTime * (mNewAngle - mOldAngle);
            } else {
                mSweepAngle = interpolatedTime * (mNewAngle - mOldAngle);
            }
            postInvalidate();
        }
    }
}