18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package com.example.smart.ui.viewer;
 
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.LruCache;
import android.view.View;
 
import com.example.smart.R;
 
public class ProgressPieView extends View {
 
    public interface OnProgressListener {
        public void onProgressChanged(int progress, int max);
        public void onProgressCompleted();
    }
 
    /**
     * Fills the progress radially in a clockwise direction.
     */
    public static final int FILL_TYPE_RADIAL = 0;
    /**
     * Fills the progress expanding from the center of the view.
     */
    public static final int FILL_TYPE_CENTER = 1;
 
    public static final int SLOW_ANIMATION_SPEED = 50;
    public static final int MEDIUM_ANIMATION_SPEED = 25;
    public static final int FAST_ANIMATION_SPEED = 1;
 
    private static final int DEFAULT_MAX = 100;
    private static final int DEFAULT_PROGRESS = 0;
    private static final int DEFAULT_START_ANGLE = -90;
    private static final float DEFAULT_STROKE_WIDTH = 3f;
    private static final float DEFAULT_TEXT_SIZE = 14f;
    private static final int DEFAULT_VIEW_SIZE = 96;
 
    private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(8);
 
    private OnProgressListener mListener;
    private DisplayMetrics mDisplayMetrics;
    private int mMax = DEFAULT_MAX;
    private int mProgress = DEFAULT_PROGRESS;
    private int mStartAngle = DEFAULT_START_ANGLE;
    private boolean mInverted = false;
    private boolean mCounterclockwise = false;
    private boolean mShowStroke = true;
    private float mStrokeWidth = DEFAULT_STROKE_WIDTH;
    private boolean mShowText = true;
    private float mTextSize = DEFAULT_TEXT_SIZE;
    private String mText;
    private String mTypeface;
    private boolean mShowImage = true;
    private Drawable mImage;
    private Rect mImageRect;
    private Paint mStrokePaint;
    private Paint mTextPaint;
    private Paint mProgressPaint;
    private Paint mBackgroundPaint;
    private RectF mInnerRectF;
    private int mProgressFillType = FILL_TYPE_RADIAL;
 
    private int mAnimationSpeed = MEDIUM_ANIMATION_SPEED;
    private AnimationHandler mAnimationHandler = new AnimationHandler();
 
    private int mViewSize;
 
    public ProgressPieView(Context context) {
        this(context, null);
    }
 
    public ProgressPieView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public ProgressPieView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
 
    private void init(Context context, AttributeSet attrs) {
        mDisplayMetrics = context.getResources().getDisplayMetrics();
 
        mStrokeWidth = mStrokeWidth * mDisplayMetrics.density;
        mTextSize = mTextSize * mDisplayMetrics.scaledDensity;
 
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressPieView);
        final Resources res = getResources();
 
        mMax = a.getInteger(R.styleable.ProgressPieView_ppvMax, mMax);
        mProgress = a.getInteger(R.styleable.ProgressPieView_ppvProgress, mProgress);
        mStartAngle = a.getInt(R.styleable.ProgressPieView_ppvStartAngle, mStartAngle);
        mInverted = a.getBoolean(R.styleable.ProgressPieView_ppvInverted, mInverted);
        mCounterclockwise = a.getBoolean(R.styleable.ProgressPieView_ppvCounterclockwise, mCounterclockwise);
        mStrokeWidth = a.getDimension(R.styleable.ProgressPieView_ppvStrokeWidth, mStrokeWidth);
        mTypeface = a.getString(R.styleable.ProgressPieView_ppvTypeface);
        mTextSize = a.getDimension(R.styleable.ProgressPieView_android_textSize, mTextSize);
        mText = a.getString(R.styleable.ProgressPieView_android_text);
 
        mShowStroke = a.getBoolean(R.styleable.ProgressPieView_ppvShowStroke, mShowStroke);
        mShowText = a.getBoolean(R.styleable.ProgressPieView_ppvShowText, mShowText);
        mImage = a.getDrawable(R.styleable.ProgressPieView_ppvImage);
 
        int backgroundColor = res.getColor(R.color.default_background_color);
        backgroundColor = a.getColor(R.styleable.ProgressPieView_ppvBackgroundColor, backgroundColor);
        int progressColor = res.getColor(R.color.default_progress_color);
        progressColor = a.getColor(R.styleable.ProgressPieView_ppvProgressColor, progressColor);
        int strokeColor = res.getColor(R.color.default_stroke_color);
        strokeColor = a.getColor(R.styleable.ProgressPieView_ppvStrokeColor, strokeColor);
        int textColor = res.getColor(R.color.default_text_color);
        textColor = a.getColor(R.styleable.ProgressPieView_android_textColor, textColor);
 
        mProgressFillType = a.getInteger(R.styleable.ProgressPieView_ppvProgressFillType, mProgressFillType);
 
        a.recycle();
 
        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(backgroundColor);
        mBackgroundPaint.setStyle(Paint.Style.FILL);
 
        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(progressColor);
        mProgressPaint.setStyle(Paint.Style.FILL);
 
        mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mStrokePaint.setColor(strokeColor);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(mStrokeWidth);
 
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(textColor);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
 
        mInnerRectF = new RectF();
        mImageRect = new Rect();
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
        int width = resolveSize(DEFAULT_VIEW_SIZE, widthMeasureSpec);
        int height = resolveSize(DEFAULT_VIEW_SIZE, heightMeasureSpec);
        mViewSize = Math.min(width, height);
 
        setMeasuredDimension(width, height);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mInnerRectF.set(0, 0, mViewSize, mViewSize);
        mInnerRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);
        if (mShowStroke) {
            final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f);
            mInnerRectF.inset(halfBorder, halfBorder);
        }
        float centerX = mInnerRectF.centerX();
        float centerY = mInnerRectF.centerY();
 
        canvas.drawArc(mInnerRectF, 0, 360, true, mBackgroundPaint);
 
        switch (mProgressFillType) {
            case FILL_TYPE_RADIAL:
                float sweepAngle = 360 * mProgress / mMax;
                if (mInverted) {
                    sweepAngle = sweepAngle - 360;
                }
                if (mCounterclockwise) {
                    sweepAngle = -sweepAngle;
                }
                canvas.drawArc(mInnerRectF, mStartAngle, sweepAngle, true, mProgressPaint);
                break;
            case FILL_TYPE_CENTER:
                float radius = (mViewSize / 2) * ((float) mProgress / mMax);
                if (mShowStroke) {
                    radius = radius + 0.5f - mStrokePaint.getStrokeWidth();
                }
                canvas.drawCircle(centerX, centerY, radius, mProgressPaint);
                break;
            default:
                throw new IllegalArgumentException("Invalid Progress Fill = " + mProgressFillType);
        }
 
        if (!TextUtils.isEmpty(mText) && mShowText) {
            if (!TextUtils.isEmpty(mTypeface)) {
                Typeface typeface = sTypefaceCache.get(mTypeface);
                if (null == typeface && null != getResources()) {
                    AssetManager assets = getResources().getAssets();
                    if (null != assets) {
                        typeface = Typeface.createFromAsset(assets, mTypeface);
                        sTypefaceCache.put(mTypeface, typeface);
                    }
                }
                mTextPaint.setTypeface(typeface);
            }
            int xPos = (int) centerX;
            int yPos = (int) (centerY - (mTextPaint.descent() + mTextPaint.ascent()) / 2);
            canvas.drawText(mText, xPos, yPos, mTextPaint);
        }
 
        if (null != mImage && mShowImage) {
            int drawableSize = mImage.getIntrinsicWidth();
            mImageRect.set(0, 0, drawableSize, drawableSize);
            mImageRect.offset((getWidth() - drawableSize) / 2, (getHeight() - drawableSize) / 2);
            mImage.setBounds(mImageRect);
            mImage.draw(canvas);
        }
 
        if (mShowStroke) {
            canvas.drawOval(mInnerRectF, mStrokePaint);
        }
 
    }
 
    /**
     * Gets the maximum progress value.
     */
    public int getMax() {
        return mMax;
    }
 
    /**
     * Sets the maximum progress value. Defaults to 100.
     */
    public void setMax(int max) {
        if (max <= 0 || max < mProgress) {
            throw new IllegalArgumentException(
                    String.format("Max (%d) must be > 0 and >= %d", max, mProgress));
        }
        mMax = max;
        invalidate();
    }
 
    /**
     * Sets the animation speed used in the animateProgressFill method.
     */
    public void setAnimationSpeed(int animationSpeed){
        this.mAnimationSpeed = animationSpeed;
    }
 
    /**
     * Returns the current animation speed used in animateProgressFill method.
     */
    public int getAnimationSpeed(){
        return this.mAnimationSpeed;
    }
 
    /**
     * Animates a progress fill of the view, using a Handler.
     */
    public void animateProgressFill() {
        mAnimationHandler.removeMessages(0);
        mAnimationHandler.setAnimateTo(mMax);
        mAnimationHandler.sendEmptyMessage(0);
        invalidate();
    }
 
    /**
     * Animates a progress fill of the view, using a Handler.
     * @param animateTo - the progress value the animation should stop at (0 - MAX)
     */
    public void animateProgressFill(int animateTo) {
        mAnimationHandler.removeMessages(0);
        if (animateTo > mMax || animateTo < 0) {
            throw new IllegalArgumentException(
                    String.format("Animation progress (%d) is greater than the max progress (%d) or lower than 0 ", animateTo, mMax));
        }
        mAnimationHandler.setAnimateTo(animateTo);
        mAnimationHandler.sendEmptyMessage(0);
        invalidate();
    }
 
    /**
     * Stops the views animation.
     */
    public void stopAnimating() {
        mAnimationHandler.removeMessages(0);
        mAnimationHandler.setAnimateTo(mProgress);
        invalidate();
    }
 
    /**
     * Gets the current progress from 0 to max.
     */
    public int getProgress() {
        return mProgress;
    }
 
    /**
     * Sets the current progress (must be between 0 and max).
     */
    public void setProgress(int progress) {
        if (progress > mMax || progress < 0) {
            throw new IllegalArgumentException(
                    String.format("Progress (%d) must be between %d and %d", progress, 0, mMax));
        }
        mProgress = progress;
        if (null != mListener) {
            if (mProgress == mMax) {
                mListener.onProgressCompleted();
            } else {
                mListener.onProgressChanged(mProgress, mMax);
            }
        }
        invalidate();
    }
 
    /**
     * Gets the start angle the {@link #FILL_TYPE_RADIAL} uses.
     */
    public int getStartAngle() {
        return mStartAngle;
    }
 
    /**
     * Sets the start angle the {@link #FILL_TYPE_RADIAL} uses.
     * @param startAngle start angle in degrees
     */
    public void setStartAngle(int startAngle) {
        mStartAngle = startAngle;
    }
 
    /**
     * Gets the inverted state.
     */
    public boolean isInverted() {
        return mInverted;
    }
 
    /**
     * Sets the inverted state.
     * @param inverted draw the progress inverted or not
     */
    public void setInverted(boolean inverted) {
        mInverted = inverted;
    }
 
    /**
     * Gets the counterclockwise state.
     */
    public boolean isCounterclockwise() {
        return mCounterclockwise;
    }
 
    /**
     * Sets the counterclockwise state.
     * @param counterclockwise draw the progress counterclockwise or not
     */
    public void setCounterclockwise(boolean counterclockwise) {
        mCounterclockwise = counterclockwise;
    }
 
    /**
     * Gets the color used to display the progress of the view.
     */
    public int getProgressColor() {
        return mProgressPaint.getColor();
    }
 
    /**
     *  Sets the color used to display the progress of the view.
     *  @param color - color of the progress part of the view
     */
    public void setProgressColor(int color) {
        mProgressPaint.setColor(color);
        invalidate();
    }
 
    /**
     *  Gets the color used to display the background of the view.
     */
    public int getBackgroundColor() {
        return mBackgroundPaint.getColor();
    }
 
    /**
     * Sets the color used to display the background of the view.
     * @param color - color of the background part of the view
     */
    public void setBackgroundColor(int color) {
        mBackgroundPaint.setColor(color);
        invalidate();
    }
 
    /**
     *  Gets the color used to display the text of the view.
     */
    public int getTextColor() {
        return mTextPaint.getColor();
    }
 
    /**
     * Sets the color used to display the text of the view.
     * @param color - color of the text part of the view
     */
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }
 
    /**
     * Gets the text size in sp.
     */
    public float getTextSize() {
        return mTextSize;
    }
 
    /**
     * Sets the text size.
     * @param sizeSp in sp for the text
     */
    public void setTextSize(int sizeSp) {
        mTextSize = sizeSp * mDisplayMetrics.scaledDensity;
        mTextPaint.setTextSize(mTextSize);
        invalidate();
    }
 
    /**
     * Gets the text of the view.
     */
    public String getText() {
        return mText;
    }
 
    /**
     * Sets the text of the view.
     * @param text to be displayed in the view
     */
    public void setText(String text) {
        mText = text;
        invalidate();
    }
 
    /**
     * Gets the typeface of the text.
     */
    public String getTypeface() {
        return mTypeface;
    }
 
    /**
     * Sets the text typeface.
     *  - i.e. fonts/Roboto/Roboto-Regular.ttf
     * @param typeface that the text is displayed in
     */
    public void setTypeface(String typeface) {
        mTypeface = typeface;
        invalidate();
    }
 
    /**
     * Gets the show text state.
     */
    public boolean isTextShowing() {
        return mShowText;
    }
 
    /**
     * Sets the show text state.
     * @param showText show or hide text
     */
    public void setShowText(boolean showText) {
        mShowText = showText;
        invalidate();
    }
 
    /**
     *  Get the color used to display the stroke of the view.
     */
    public int getStrokeColor() {
        return mStrokePaint.getColor();
    }
 
    /**
     * Sets the color used to display the stroke of the view.
     * @param color - color of the stroke part of the view
     */
    public void setStrokeColor(int color) {
        mStrokePaint.setColor(color);
        invalidate();
    }
 
    /**
     * Gets the stroke width in dp.
     */
    public float getStrokeWidth() {
        return mStrokeWidth;
    }
 
    /**
     * Set the stroke width.
     * @param widthDp in dp for the pie border
     */
    public void setStrokeWidth(int widthDp) {
        mStrokeWidth = widthDp * mDisplayMetrics.density;
        mStrokePaint.setStrokeWidth(mStrokeWidth);
        invalidate();
    }
 
    /**
     * Gets the show stroke state.
     */
    public boolean isStrokeShowing() {
        return mShowStroke;
    }
 
    /**
     * Sets the show stroke state.
     * @param showStroke show or hide stroke
     */
    public void setShowStroke(boolean showStroke) {
        mShowStroke = showStroke;
        invalidate();
    }
 
    /**
     * Gets the drawable of the view.
     */
    public Drawable getImageDrawable() {
        return mImage;
    }
 
    /**
     * Sets the drawable of the view.
     * @param image drawable of the view
     */
    public void setImageDrawable(Drawable image) {
        mImage = image;
        invalidate();
    }
 
    /**
     * Sets the drawable of the view.
     * @param resId resource id of the view's drawable
     */
    public void setImageResource(int resId) {
        if (null != getResources()) {
            mImage = getResources().getDrawable(resId);
            invalidate();
        }
    }
 
    /**
     * Gets the show image state.
     */
    public boolean isImageShowing() {
        return mShowImage;
    }
 
    /**
     * Sets the show image state.
     * @param showImage show or hide image
     */
    public void setShowImage(boolean showImage) {
        mShowImage = showImage;
        invalidate();
    }
 
    /**
     * Gets the progress fill type.
     */
    public int getProgressFillType() {
        return mProgressFillType;
    }
 
    /**
     * Sets the progress fill type.
     * @param fillType one of {@link #FILL_TYPE_CENTER}, {@link #FILL_TYPE_RADIAL}
     */
    public void setProgressFillType(int fillType) {
        mProgressFillType = fillType;
    }
 
    /**
     * Sets the progress listner.
     * @param listener progress listener
     *
     * @see com.filippudak.ProgressPieView.ProgressPieView.OnProgressListener
     */
    public void setOnProgressListener(OnProgressListener listener) {
        mListener = listener;
    }
 
    /**
     * Handler used to perform the fill animation.
     */
    private class AnimationHandler extends Handler {
 
        private int mAnimateTo;
 
        public void setAnimateTo(int animateTo) {
            mAnimateTo = animateTo;
        }
 
        @Override
        public void handleMessage(Message msg) {
            if (mProgress > mAnimateTo) {
                setProgress(mProgress - 1);
                sendEmptyMessageDelayed(0, mAnimationSpeed);
            } else if (mProgress < mAnimateTo) {
                setProgress(mProgress + 1);
                sendEmptyMessageDelayed(0, mAnimationSpeed);
            } else {
                removeMessages(0);
            }
        }
    }
 
}