Skip to content

Commit 60152b3

Browse files
Allow setting different stroke width for progress and background rings
1 parent 8a4c97f commit 60152b3

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import android.view.View;
2727
import android.view.animation.AccelerateDecelerateInterpolator;
2828

29+
import java.lang.annotation.Retention;
30+
import java.lang.annotation.RetentionPolicy;
31+
2932
/**
3033
* Created by Anton on 03.03.2018.
3134
*/
@@ -115,6 +118,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
115118
int progressColor = Color.parseColor(DEFAULT_PROGRESS_COLOR);
116119
int progressBackgroundColor = Color.parseColor(DEFAULT_PROGRESS_BACKGROUND_COLOR);
117120
int progressStrokeWidth = dp2px(DEFAULT_STROKE_WIDTH_DP);
121+
int progressBackgroundStrokeWidth = progressStrokeWidth;
118122
int textColor = progressColor;
119123
int textSize = sp2px(DEFAULT_TEXT_SIZE_SP);
120124

@@ -130,6 +134,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
130134
progressColor = a.getColor(R.styleable.CircularProgressIndicator_progressColor, progressColor);
131135
progressBackgroundColor = a.getColor(R.styleable.CircularProgressIndicator_progressBackgroundColor, progressBackgroundColor);
132136
progressStrokeWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_progressStrokeWidth, progressStrokeWidth);
137+
progressBackgroundStrokeWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_progressBackgroundStrokeWidth, progressStrokeWidth);
133138
textColor = a.getColor(R.styleable.CircularProgressIndicator_textColor, progressColor);
134139
textSize = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_textSize, textSize);
135140

@@ -170,7 +175,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
170175

171176
progressBackgroundPaint = new Paint();
172177
progressBackgroundPaint.setStyle(Paint.Style.STROKE);
173-
progressBackgroundPaint.setStrokeWidth(progressStrokeWidth);
178+
progressBackgroundPaint.setStrokeWidth(progressBackgroundStrokeWidth);
174179
progressBackgroundPaint.setColor(progressBackgroundColor);
175180
progressBackgroundPaint.setAntiAlias(true);
176181

@@ -208,7 +213,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
208213
Rect textBoundsRect = new Rect();
209214
textPaint.getTextBounds(progressText, 0, progressText.length(), textBoundsRect);
210215

211-
float strokeSizeOffset = (shouldDrawDot) ? Math.max(dotPaint.getStrokeWidth(), progressPaint.getStrokeWidth()) : progressPaint.getStrokeWidth(); // to prevent progress or dot from drawing over the bounds
216+
217+
float dotWidth = dotPaint.getStrokeWidth();
218+
float progressWidth = progressPaint.getStrokeWidth();
219+
float progressBackgroundWidth = progressBackgroundPaint.getStrokeWidth();
220+
float strokeSizeOffset = (shouldDrawDot) ? Math.max(dotWidth, Math.max(progressWidth, progressBackgroundWidth)) : Math.max(progressWidth, progressBackgroundWidth);
221+
212222
int desiredSize = ((int) strokeSizeOffset) + dp2px(DESIRED_WIDTH_DP) +
213223
Math.max(paddingBottom + paddingTop, paddingLeft + paddingRight);
214224

@@ -256,7 +266,10 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
256266
private void calculateBounds(int w, int h) {
257267
radius = w / 2f;
258268

259-
float strokeSizeOffset = (shouldDrawDot) ? Math.max(dotPaint.getStrokeWidth(), progressPaint.getStrokeWidth()) : progressPaint.getStrokeWidth(); // to prevent progress or dot from drawing over the bounds
269+
float dotWidth = dotPaint.getStrokeWidth();
270+
float progressWidth = progressPaint.getStrokeWidth();
271+
float progressBackgroundWidth = progressBackgroundPaint.getStrokeWidth();
272+
float strokeSizeOffset = (shouldDrawDot) ? Math.max(dotWidth, Math.max(progressWidth, progressBackgroundWidth)) : Math.max(progressWidth, progressBackgroundWidth); // to prevent progress or dot from drawing over the bounds
260273
float halfOffset = strokeSizeOffset / 2f;
261274

262275
circleBounds.left = halfOffset;
@@ -436,8 +449,17 @@ public void setProgressStrokeWidthDp(@Dimension int strokeWidth) {
436449
setProgressStrokeWidthPx(dp2px(strokeWidth));
437450
}
438451

439-
public void setProgressStrokeWidthPx(@Dimension final int strokeWidth) {
452+
public void setProgressStrokeWidthPx(@Dimension int strokeWidth) {
440453
progressPaint.setStrokeWidth(strokeWidth);
454+
455+
invalidateEverything();
456+
}
457+
458+
public void setProgressBackgroundStrokeWidthDp(@Dimension int strokeWidth) {
459+
setProgressBackgroundStrokeWidthPx(dp2px(strokeWidth));
460+
}
461+
462+
public void setProgressBackgroundStrokeWidthPx(@Dimension int strokeWidth) {
441463
progressBackgroundPaint.setStrokeWidth(strokeWidth);
442464

443465
invalidateEverything();
@@ -495,7 +517,7 @@ public void setDotWidthDp(@Dimension int width) {
495517
setDotWidthPx(dp2px(width));
496518
}
497519

498-
public void setDotWidthPx(@Dimension final int width) {
520+
public void setDotWidthPx(@Dimension int width) {
499521
dotPaint.setStrokeWidth(width);
500522

501523
invalidateEverything();
@@ -533,6 +555,9 @@ public float getProgressStrokeWidth() {
533555
return progressPaint.getStrokeWidth();
534556
}
535557

558+
public float getProgressBackgroundStrokeWidth() {
559+
return progressBackgroundPaint.getStrokeWidth();
560+
}
536561

537562
@ColorInt
538563
public int getTextColor() {
@@ -617,10 +642,12 @@ public boolean isAnimationEnabled() {
617642
return isAnimationEnabled;
618643
}
619644

645+
@Retention(RetentionPolicy.SOURCE)
620646
@IntDef({DIRECTION_CLOCKWISE, DIRECTION_COUNTERCLOCKWISE})
621647
public @interface Direction {
622648
}
623649

650+
@Retention(RetentionPolicy.SOURCE)
624651
@IntDef({CAP_ROUND, CAP_BUTT})
625652
public @interface Cap {
626653
}

circularprogressindicator/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<attr name="progressColor" format="color" />
66
<attr name="progressBackgroundColor" format="color" />
77
<attr name="progressStrokeWidth" format="dimension" />
8+
<attr name="progressBackgroundStrokeWidth" format="dimension" />
89

910
<attr name="textColor" format="color" />
1011
<attr name="textSize" format="dimension" />

0 commit comments

Comments
 (0)