|
| 1 | +package com.smthbig.shadow.launcher.home; |
| 2 | + |
| 3 | +import android.animation.ValueAnimator; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.graphics.Paint; |
| 8 | +import android.graphics.RectF; |
| 9 | +import android.util.AttributeSet; |
| 10 | +import android.view.View; |
| 11 | +import android.view.animation.DecelerateInterpolator; |
| 12 | + |
| 13 | +import androidx.annotation.Nullable; |
| 14 | + |
| 15 | +public class CircularProgressRingView extends View { |
| 16 | + |
| 17 | + private final Paint trackPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 18 | + private final Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 19 | + private final Paint valuePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 20 | + private final Paint labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 21 | + private final RectF arcRect = new RectF(); |
| 22 | + |
| 23 | + private float progress = 0f; |
| 24 | + private float animatedProgress = 0f; |
| 25 | + private int ringColor = 0xFF58A6FF; |
| 26 | + private int trackColor = 0xFF21262D; |
| 27 | + private int textColor = 0xFFF0F6FC; |
| 28 | + private int mutedColor = 0xFF8B949E; |
| 29 | + private String valueText = "0"; |
| 30 | + private String labelText = ""; |
| 31 | + private float strokeWidth; |
| 32 | + private ValueAnimator animator; |
| 33 | + |
| 34 | + public CircularProgressRingView(Context context, @Nullable AttributeSet attrs) { |
| 35 | + super(context, attrs); |
| 36 | + float density = getResources().getDisplayMetrics().density; |
| 37 | + strokeWidth = density * 4; |
| 38 | + |
| 39 | + trackPaint.setStyle(Paint.Style.STROKE); |
| 40 | + trackPaint.setStrokeWidth(strokeWidth); |
| 41 | + trackPaint.setStrokeCap(Paint.Cap.ROUND); |
| 42 | + trackPaint.setColor(trackColor); |
| 43 | + |
| 44 | + progressPaint.setStyle(Paint.Style.STROKE); |
| 45 | + progressPaint.setStrokeWidth(strokeWidth); |
| 46 | + progressPaint.setStrokeCap(Paint.Cap.ROUND); |
| 47 | + progressPaint.setColor(ringColor); |
| 48 | + |
| 49 | + valuePaint.setAntiAlias(true); |
| 50 | + valuePaint.setColor(textColor); |
| 51 | + valuePaint.setTextAlign(Paint.Align.CENTER); |
| 52 | + valuePaint.setFakeBoldText(true); |
| 53 | + |
| 54 | + labelPaint.setAntiAlias(true); |
| 55 | + labelPaint.setColor(mutedColor); |
| 56 | + labelPaint.setTextAlign(Paint.Align.CENTER); |
| 57 | + } |
| 58 | + |
| 59 | + public void setProgress(float p) { |
| 60 | + progress = Math.max(0f, Math.min(1f, p)); |
| 61 | + animateToProgress(progress); |
| 62 | + } |
| 63 | + |
| 64 | + public void setRingColor(int color) { |
| 65 | + ringColor = color; |
| 66 | + progressPaint.setColor(color); |
| 67 | + invalidate(); |
| 68 | + } |
| 69 | + |
| 70 | + public void setTrackColor(int color) { |
| 71 | + trackColor = color; |
| 72 | + trackPaint.setColor(color); |
| 73 | + invalidate(); |
| 74 | + } |
| 75 | + |
| 76 | + public void setValueText(String text) { |
| 77 | + valueText = text; |
| 78 | + invalidate(); |
| 79 | + } |
| 80 | + |
| 81 | + public void setLabelText(String text) { |
| 82 | + labelText = text; |
| 83 | + invalidate(); |
| 84 | + } |
| 85 | + |
| 86 | + public void setTextColor(int color) { |
| 87 | + textColor = color; |
| 88 | + valuePaint.setColor(color); |
| 89 | + invalidate(); |
| 90 | + } |
| 91 | + |
| 92 | + public void setMutedColor(int color) { |
| 93 | + mutedColor = color; |
| 94 | + labelPaint.setColor(color); |
| 95 | + invalidate(); |
| 96 | + } |
| 97 | + |
| 98 | + private void animateToProgress(float target) { |
| 99 | + if (animator != null) animator.cancel(); |
| 100 | + float start = animatedProgress; |
| 101 | + animator = ValueAnimator.ofFloat(start, target); |
| 102 | + animator.setDuration(800); |
| 103 | + animator.setInterpolator(new DecelerateInterpolator()); |
| 104 | + animator.addUpdateListener(a -> { |
| 105 | + animatedProgress = (float) a.getAnimatedValue(); |
| 106 | + invalidate(); |
| 107 | + }); |
| 108 | + animator.start(); |
| 109 | + } |
| 110 | + |
| 111 | + public void setValues(String value, String label, float progress, int color) { |
| 112 | + valueText = value; |
| 113 | + labelText = label; |
| 114 | + ringColor = color; |
| 115 | + progressPaint.setColor(color); |
| 116 | + this.progress = Math.max(0f, Math.min(1f, progress)); |
| 117 | + animateToProgress(this.progress); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 122 | + int size = Math.min( |
| 123 | + MeasureSpec.getSize(widthMeasureSpec), |
| 124 | + MeasureSpec.getSize(heightMeasureSpec)); |
| 125 | + setMeasuredDimension(size, size); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 130 | + super.onSizeChanged(w, h, oldw, oldh); |
| 131 | + float padding = strokeWidth / 2f + getResources().getDisplayMetrics().density * 4; |
| 132 | + arcRect.set(padding, padding, w - padding, h - padding); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void onDraw(Canvas canvas) { |
| 137 | + super.onDraw(canvas); |
| 138 | + float cx = getWidth() / 2f; |
| 139 | + float cy = getHeight() / 2f; |
| 140 | + |
| 141 | + canvas.drawArc(arcRect, -90f, 360f, false, trackPaint); |
| 142 | + if (animatedProgress > 0) { |
| 143 | + canvas.drawArc(arcRect, -90f, 360f * animatedProgress, false, progressPaint); |
| 144 | + } |
| 145 | + |
| 146 | + float density = getResources().getDisplayMetrics().density; |
| 147 | + valuePaint.setTextSize(density * 18); |
| 148 | + labelPaint.setTextSize(density * 8); |
| 149 | + |
| 150 | + float baseline = cy - (valuePaint.descent() + valuePaint.ascent()) / 2f; |
| 151 | + canvas.drawText(valueText, cx, baseline, valuePaint); |
| 152 | + |
| 153 | + float labelBaseline = cy + density * 10; |
| 154 | + canvas.drawText(labelText, cx, labelBaseline, labelPaint); |
| 155 | + } |
| 156 | +} |
0 commit comments