|
2 | 2 |
|
3 | 3 | import android.animation.ValueAnimator; |
4 | 4 | import android.content.Context; |
| 5 | +import android.content.res.TypedArray; |
5 | 6 | import android.graphics.Canvas; |
6 | 7 | import android.graphics.Color; |
7 | 8 | import android.graphics.Paint; |
8 | 9 | import android.graphics.RectF; |
9 | 10 | import android.support.annotation.Nullable; |
10 | 11 | import android.util.AttributeSet; |
| 12 | +import android.util.TypedValue; |
11 | 13 | import android.view.View; |
12 | 14 | import android.view.animation.DecelerateInterpolator; |
| 15 | +import android.view.animation.LinearInterpolator; |
| 16 | + |
| 17 | +import com.allen.androidcustomview.R; |
13 | 18 |
|
14 | 19 | /** |
15 | 20 | * Created by Allen on 2017/5/14. |
|
19 | 24 |
|
20 | 25 | public class CircleProgressBarView extends View { |
21 | 26 |
|
22 | | - private float width; |
23 | | - private float height; |
| 27 | + private Context mContext; |
| 28 | + |
24 | 29 |
|
25 | 30 | /** |
26 | 31 | * 圆心x坐标 |
@@ -96,6 +101,12 @@ public class CircleProgressBarView extends View { |
96 | 101 | */ |
97 | 102 | private int startDelay = 500; |
98 | 103 |
|
| 104 | + private boolean isDrawCenterProgressText; |
| 105 | + private int centerProgressTextSize = 10; |
| 106 | + private int centerProgressTextColor = Color.BLACK; |
| 107 | + |
| 108 | + private Paint centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 109 | + |
99 | 110 | private ProgressListener progressListener; |
100 | 111 |
|
101 | 112 | public CircleProgressBarView(Context context) { |
@@ -104,9 +115,29 @@ public CircleProgressBarView(Context context) { |
104 | 115 |
|
105 | 116 | public CircleProgressBarView(Context context, @Nullable AttributeSet attrs) { |
106 | 117 | super(context, attrs); |
107 | | - |
| 118 | + mContext = context; |
| 119 | + getAttr(attrs); |
108 | 120 | initPaint(); |
| 121 | + initTextPaint(); |
| 122 | + } |
| 123 | + |
| 124 | + private void getAttr(AttributeSet attrs) { |
| 125 | + TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressBarView); |
| 126 | + |
| 127 | + circleBgStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_circleBgStrokeWidth, defaultStrokeWidth); |
| 128 | + progressStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_progressStrokeWidth, defaultStrokeWidth); |
| 129 | + |
| 130 | + circleBgColor = typedArray.getColor(R.styleable.CircleProgressBarView_circleBgColor, circleBgColor); |
| 131 | + progressColor = typedArray.getColor(R.styleable.CircleProgressBarView_progressColor, progressColor); |
| 132 | + |
| 133 | + duration = typedArray.getColor(R.styleable.CircleProgressBarView_circleAnimationDuration, duration); |
| 134 | + |
| 135 | + isDrawCenterProgressText = typedArray.getBoolean(R.styleable.CircleProgressBarView_isDrawCenterProgressText, false); |
109 | 136 |
|
| 137 | + centerProgressTextColor = typedArray.getColor(R.styleable.CircleProgressBarView_centerProgressTextColor, centerProgressTextColor); |
| 138 | + centerProgressTextSize = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBarView_centerProgressTextSize, sp2px(centerProgressTextSize)); |
| 139 | + |
| 140 | + typedArray.recycle(); |
110 | 141 | } |
111 | 142 |
|
112 | 143 | private void initPaint() { |
@@ -127,16 +158,27 @@ private Paint getPaint(int strokeWidth, int color) { |
127 | 158 | return paint; |
128 | 159 | } |
129 | 160 |
|
| 161 | + /** |
| 162 | + * 初始化文字画笔 |
| 163 | + */ |
| 164 | + private void initTextPaint() { |
| 165 | + centerProgressTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 166 | + centerProgressTextPaint.setTextSize(centerProgressTextSize); |
| 167 | + centerProgressTextPaint.setColor(centerProgressTextColor); |
| 168 | + centerProgressTextPaint.setTextAlign(Paint.Align.CENTER); |
| 169 | + centerProgressTextPaint.setAntiAlias(true); |
| 170 | + } |
| 171 | + |
130 | 172 | private void initAnimation() { |
131 | 173 | progressAnimator = ValueAnimator.ofFloat(0, mProgress); |
132 | 174 | progressAnimator.setDuration(duration); |
133 | 175 | progressAnimator.setStartDelay(startDelay); |
134 | | - progressAnimator.setInterpolator(new DecelerateInterpolator()); |
| 176 | + progressAnimator.setInterpolator(new LinearInterpolator()); |
135 | 177 | progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
136 | 178 | @Override |
137 | 179 | public void onAnimationUpdate(ValueAnimator valueAnimator) { |
138 | 180 | float value = (float) valueAnimator.getAnimatedValue(); |
139 | | - |
| 181 | + mProgress = value; |
140 | 182 | currentProgress = value * 360 / 100; |
141 | 183 |
|
142 | 184 | if (progressListener != null) { |
@@ -151,13 +193,11 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) { |
151 | 193 | @Override |
152 | 194 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
153 | 195 | super.onSizeChanged(w, h, oldw, oldh); |
154 | | - width = w; |
155 | | - height = h; |
156 | 196 |
|
157 | 197 | centerX = w / 2; |
158 | 198 | centerY = h / 2; |
159 | 199 |
|
160 | | - radius = Math.min(w, h) / 2 - circleBgStrokeWidth; |
| 200 | + radius = Math.min(w, h) / 2 - Math.max(circleBgStrokeWidth, progressStrokeWidth); |
161 | 201 |
|
162 | 202 | rectF.set(centerX - radius, |
163 | 203 | centerY - radius, |
@@ -171,8 +211,19 @@ protected void onDraw(Canvas canvas) { |
171 | 211 | super.onDraw(canvas); |
172 | 212 | canvas.drawCircle(centerX, centerY, radius, circleBgPaint); |
173 | 213 | canvas.drawArc(rectF, 90, currentProgress, false, progressPaint); |
| 214 | + if (isDrawCenterProgressText) { |
| 215 | + drawCenterProgressText(canvas, (int) mProgress + "%"); |
| 216 | + } |
174 | 217 | } |
175 | 218 |
|
| 219 | + private void drawCenterProgressText(Canvas canvas, String currentProgress) { |
| 220 | + Paint.FontMetricsInt fontMetrics = centerProgressTextPaint.getFontMetricsInt(); |
| 221 | + int baseline = (int) ((rectF.bottom + rectF.top - fontMetrics.bottom - fontMetrics.top) / 2); |
| 222 | + //文字绘制到整个布局的中心位置 |
| 223 | + canvas.drawText(currentProgress, rectF.centerX(), baseline, centerProgressTextPaint); |
| 224 | + } |
| 225 | + |
| 226 | + |
176 | 227 | public void startProgressAnimation() { |
177 | 228 | progressAnimator.start(); |
178 | 229 | } |
@@ -189,12 +240,33 @@ public void stopProgressAnimation() { |
189 | 240 | progressAnimator.end(); |
190 | 241 | } |
191 | 242 |
|
192 | | - public CircleProgressBarView setProgress(float progress) { |
| 243 | + |
| 244 | + /** |
| 245 | + * 传入一个进度值,从0到progress动画变化 |
| 246 | + * |
| 247 | + * @param progress |
| 248 | + * @return |
| 249 | + */ |
| 250 | + public CircleProgressBarView setProgressWithAnimation(float progress) { |
193 | 251 | mProgress = progress; |
194 | 252 | initAnimation(); |
195 | 253 | return this; |
196 | 254 | } |
197 | 255 |
|
| 256 | + /** |
| 257 | + * 实时进度,适用于下载进度回调时候之类的场景 |
| 258 | + * |
| 259 | + * @param progress |
| 260 | + * @return |
| 261 | + */ |
| 262 | + public CircleProgressBarView setCurrentProgress(float progress) { |
| 263 | + mProgress = progress; |
| 264 | + currentProgress = progress * 360 / 100; |
| 265 | + invalidate(); |
| 266 | + return this; |
| 267 | + } |
| 268 | + |
| 269 | + |
198 | 270 | public interface ProgressListener { |
199 | 271 | void currentProgressListener(float currentProgress); |
200 | 272 | } |
@@ -203,12 +275,36 @@ public CircleProgressBarView setProgressListener(ProgressListener listener) { |
203 | 275 | progressListener = listener; |
204 | 276 | return this; |
205 | 277 | } |
| 278 | + |
206 | 279 | /** |
207 | 280 | * 将一个小数四舍五入,保留两位小数返回 |
| 281 | + * |
208 | 282 | * @param originNum |
209 | 283 | * @return |
210 | 284 | */ |
211 | 285 | public static float roundTwo(float originNum) { |
212 | | - return (float) (Math.round(originNum * 10) / 10.0); |
| 286 | + return (float) (Math.round(originNum * 10) / 10.00); |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * dp 2 px |
| 291 | + * |
| 292 | + * @param dpVal |
| 293 | + */ |
| 294 | + protected int dp2px(int dpVal) { |
| 295 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 296 | + dpVal, getResources().getDisplayMetrics()); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * sp 2 px |
| 301 | + * |
| 302 | + * @param spVal |
| 303 | + * @return |
| 304 | + */ |
| 305 | + protected int sp2px(int spVal) { |
| 306 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, |
| 307 | + spVal, getResources().getDisplayMetrics()); |
| 308 | + |
213 | 309 | } |
214 | 310 | } |