From 4c8f62d3b298506bfe340deefeafeb15b7ba2d86 Mon Sep 17 00:00:00 2001 From: Yuriy Budiyev Date: Thu, 11 Jan 2018 19:42:01 +0300 Subject: [PATCH] Dev --- .../CircularProgressBar.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java index d8617a1..63fe61b 100644 --- a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java +++ b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java @@ -50,6 +50,7 @@ * Circular progress bar */ public class CircularProgressBar extends View { + private static final float DEFAULT_SIZE_DP = 48f; private static final float DEFAULT_MAXIMUM = 100f; private static final float DEFAULT_PROGRESS = 0f; private static final float DEFAULT_FOREGROUND_STROKE_WIDTH_DP = 3f; @@ -71,6 +72,7 @@ public class CircularProgressBar extends View { private final ValueAnimator mIndeterminateSweepAnimator = new ValueAnimator(); private final Paint mForegroundStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint mBackgroundStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private int mDefaultSize; private float mMaximum; private float mProgress; private float mStartAngle; @@ -324,8 +326,44 @@ protected void onDraw(Canvas canvas) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); - int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int widthSize = MeasureSpec.getSize(widthMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + int heightSize = MeasureSpec.getSize(heightMeasureSpec); + int defaultWidth = Math.max(getSuggestedMinimumWidth(), mDefaultSize); + int defaultHeight = Math.max(getSuggestedMinimumHeight(), mDefaultSize); + int width; + int height; + switch (widthMode) { + case MeasureSpec.EXACTLY: { + width = widthSize; + break; + } + case MeasureSpec.AT_MOST: { + width = Math.min(defaultWidth, widthSize); + break; + } + case MeasureSpec.UNSPECIFIED: + default: { + width = defaultWidth; + break; + } + } + switch (heightMode) { + case MeasureSpec.EXACTLY: { + height = heightSize; + break; + } + case MeasureSpec.AT_MOST: { + height = Math.min(defaultHeight, heightSize); + break; + } + case MeasureSpec.UNSPECIFIED: + default: { + height = defaultHeight; + break; + } + } setMeasuredDimension(width, height); invalidateDrawRect(width, height); } @@ -357,6 +395,7 @@ private void initialize(@NonNull Context context, @Nullable AttributeSet attribu mForegroundStrokePaint.setStyle(Paint.Style.STROKE); mBackgroundStrokePaint.setStyle(Paint.Style.STROKE); DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); + mDefaultSize = Math.round(DEFAULT_SIZE_DP * displayMetrics.density); if (attributeSet == null) { mMaximum = DEFAULT_MAXIMUM; mProgress = DEFAULT_PROGRESS;