From 8c9fd30c05a7de2a32a6fd8d18c696822293e6f4 Mon Sep 17 00:00:00 2001 From: Yuriy Budiyev Date: Sun, 17 Dec 2017 02:26:12 +0300 Subject: [PATCH] Dev --- .../CircularProgressBar.java | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java index 5ab6165..41ecc9d 100644 --- a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java +++ b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java @@ -25,7 +25,6 @@ import android.animation.Animator; import android.animation.ValueAnimator; -import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; @@ -39,6 +38,7 @@ import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; import android.support.annotation.StyleRes; import android.util.AttributeSet; import android.util.DisplayMetrics; @@ -76,6 +76,7 @@ public class CircularProgressBar extends View { private boolean mAnimateProgress; private boolean mDrawBackgroundStroke; private boolean mIndeterminateGrowMode; + private boolean mVisible; private Paint mForegroundStrokePaint; private Paint mBackgroundStrokePaint; private RectF mDrawRect; @@ -98,7 +99,7 @@ public CircularProgressBar(@NonNull Context context, @Nullable AttributeSet attr initialize(context, attrs, defStyleAttr, 0); } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) + @RequiresApi(Build.VERSION_CODES.LOLLIPOP) public CircularProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); @@ -116,12 +117,11 @@ public boolean isIndeterminate() { * Indeterminate mode, disabled by default */ public void setIndeterminate(boolean indeterminate) { + stopIndeterminateAnimations(); mIndeterminate = indeterminate; invalidate(); - if (indeterminate) { + if (mVisible && indeterminate) { startIndeterminateAnimations(); - } else { - stopIndeterminateAnimations(); } } @@ -140,7 +140,7 @@ public void setProgress(float progress) { mProgress = progress; } else { stopProgressAnimation(); - if (mAnimateProgress && isLaidOutCompat()) { + if (mVisible && mAnimateProgress) { setProgressAnimated(progress); } else { setProgressInternal(progress); @@ -183,14 +183,11 @@ public void setAnimateProgress(boolean animate) { * Minimum angle for indeterminate mode */ public void setIndeterminateMinimumAngle(@FloatRange(from = 0f, to = 360f) float angle) { - boolean animating = isIndeterminateAnimating(); - if (animating) { - stopIndeterminateAnimations(); - } + stopIndeterminateAnimations(); mIndeterminateMinimumAngle = angle; mIndeterminateSweepAnimator.setFloatValues(360f - angle * 2f); invalidate(); - if (animating) { + if (mVisible && mIndeterminate) { startIndeterminateAnimations(); } } @@ -199,13 +196,10 @@ public void setIndeterminateMinimumAngle(@FloatRange(from = 0f, to = 360f) float * Rotation animation duration for indeterminate mode */ public void setIndeterminateRotationAnimationDuration(@IntRange(from = 0) long duration) { - boolean animating = isIndeterminateAnimating(); - if (animating) { - stopIndeterminateAnimations(); - } + stopIndeterminateAnimations(); mIndeterminateStartAnimator.setDuration(duration); invalidate(); - if (animating) { + if (mVisible && mIndeterminate) { startIndeterminateAnimations(); } } @@ -214,13 +208,10 @@ public void setIndeterminateRotationAnimationDuration(@IntRange(from = 0) long d * Sweep animation duration for indeterminate mode */ public void setIndeterminateSweepAnimationDuration(@IntRange(from = 0) long duration) { - boolean animating = isIndeterminateAnimating(); - if (animating) { - stopIndeterminateAnimations(); - } + stopIndeterminateAnimations(); mIndeterminateSweepAnimator.setDuration(duration); invalidate(); - if (animating) { + if (mVisible && mIndeterminate) { startIndeterminateAnimations(); } } @@ -271,6 +262,7 @@ public void setDrawBackgroundStroke(boolean draw) { @Override public void onVisibilityAggregated(boolean visible) { super.onVisibilityAggregated(visible); + mVisible = visible; if (mIndeterminate) { if (visible) { startIndeterminateAnimations(); @@ -328,6 +320,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); + mVisible = true; if (mIndeterminate) { startIndeterminateAnimations(); } @@ -336,6 +329,7 @@ protected void onAttachedToWindow() { @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); + mVisible = false; stopIndeterminateAnimations(); stopProgressAnimation(); } @@ -426,10 +420,6 @@ private void initialize(@NonNull Context context, @Nullable AttributeSet attribu mIndeterminateSweepAnimator.addListener(new SweepAnimatorListener()); } - private boolean isLaidOutCompat() { - return getWidth() > 0 && getHeight() > 0; - } - private void invalidateDrawRect() { int width = getWidth(); int height = getHeight(); @@ -493,10 +483,6 @@ private void startIndeterminateAnimations() { } } - private boolean isIndeterminateAnimating() { - return mIndeterminate && (mIndeterminateStartAnimator.isRunning() || mIndeterminateSweepAnimator.isRunning()); - } - private final class ProgressUpdateListener implements ValueAnimator.AnimatorUpdateListener { @Override public void onAnimationUpdate(ValueAnimator animation) {