From e74e8a9a4561dbe9bf070624e51ee2df4995aea4 Mon Sep 17 00:00:00 2001 From: gansgar Date: Sat, 15 Dec 2018 20:42:20 +0100 Subject: [PATCH 1/2] Add `setProgressInterpolator` --- .../CircularProgressBar.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java index e7e3f7f..c30986d 100644 --- a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java +++ b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java @@ -24,6 +24,7 @@ package com.budiyev.android.circularprogressbar; import android.animation.Animator; +import android.animation.TimeInterpolator; import android.animation.ValueAnimator; import android.content.Context; import android.content.res.TypedArray; @@ -67,6 +68,8 @@ public final class CircularProgressBar extends View { private static final boolean DEFAULT_ANIMATE_PROGRESS = true; private static final boolean DEFAULT_DRAW_BACKGROUND_STROKE = false; private static final boolean DEFAULT_INDETERMINATE = false; + private static final TimeInterpolator DEFAULT_PROGRESS_ANIMATOR = new DecelerateInterpolator(); + private final Runnable mSweepRestartAction = new SweepRestartAction(); private final RectF mDrawRect = new RectF(); private final ValueAnimator mProgressAnimator = new ValueAnimator(); @@ -154,6 +157,18 @@ public void setProgress(final float progress) { } } + /** + * Set the current animation interpolator + */ + public void setProgressInterpolator(final TimeInterpolator interpolator) { + if (mVisible) { + if (mProgressAnimator.isRunning()) { + mProgressAnimator.end(); + } + } + mProgressAnimator.setInterpolator(interpolator == null ? DEFAULT_PROGRESS_ANIMATOR : interpolator); + } + /** * Maximum progress for non-indeterminate mode */ @@ -600,7 +615,7 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut } } } - mProgressAnimator.setInterpolator(new DecelerateInterpolator()); + mProgressAnimator.setInterpolator(DEFAULT_PROGRESS_ANIMATOR); mProgressAnimator.addUpdateListener(new ProgressUpdateListener()); mIndeterminateStartAnimator.setFloatValues(360f); mIndeterminateStartAnimator.setRepeatMode(ValueAnimator.RESTART); From c67ff5fcfafd4568c434ece838794c858036ce2f Mon Sep 17 00:00:00 2001 From: gansgar Date: Sun, 16 Dec 2018 15:34:03 +0100 Subject: [PATCH 2/2] Add custom indetermined sweep and start animations --- .../CircularProgressBar.java | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java index c30986d..feeb328 100644 --- a/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java +++ b/src/main/java/com/budiyev/android/circularprogressbar/CircularProgressBar.java @@ -68,7 +68,9 @@ public final class CircularProgressBar extends View { private static final boolean DEFAULT_ANIMATE_PROGRESS = true; private static final boolean DEFAULT_DRAW_BACKGROUND_STROKE = false; private static final boolean DEFAULT_INDETERMINATE = false; - private static final TimeInterpolator DEFAULT_PROGRESS_ANIMATOR = new DecelerateInterpolator(); + private static final TimeInterpolator DEFAULT_PROGRESS_ANIMATION_INTERPOLATOR = new DecelerateInterpolator(); + private static final TimeInterpolator DEFAULT_SWEEP_ANIMATION_INTERPOLATOR = new LinearInterpolator(); + private static final TimeInterpolator DEFAULT_START_ANIMATION_INTERPOLATOR = new DecelerateInterpolator(); private final Runnable mSweepRestartAction = new SweepRestartAction(); private final RectF mDrawRect = new RectF(); @@ -134,6 +136,46 @@ public void setIndeterminate(final boolean indeterminate) { } } + /** + * Get interpolator used by start animation in + * indeterminate mode + */ + public TimeInterpolator getIndeterminateStartInterpolator() { + return mIndeterminateStartAnimator.getInterpolator(); + } + + /** + * Set interpolation for start animator that + * are used in indeterminate mode. + */ + public void setIndeterminateStartInterpolator(final TimeInterpolator startInterpolator) { + stopIndeterminateAnimations(); + mIndeterminateStartAnimator.setInterpolator( + startInterpolator == null ? DEFAULT_START_ANIMATION_INTERPOLATOR : startInterpolator + ); + setIndeterminate(mIndeterminate); + } + + /** + * Return interpolator used by Sweep Animation in + * indeterminate mode + */ + public TimeInterpolator getIndeterminateSweepInterpolator() { + return mIndeterminateSweepAnimator.getInterpolator(); + } + + /** + * Set interpolation for sweep animator that + * are used in indeterminate mode. + */ + public void setIndeterminateSweepInterpolator(final TimeInterpolator sweepInterpolator) { + stopIndeterminateAnimations(); + mIndeterminateSweepAnimator.setInterpolator( + sweepInterpolator == null ? DEFAULT_SWEEP_ANIMATION_INTERPOLATOR : sweepInterpolator + ); + setIndeterminate(mIndeterminate); + } + /** * Get current progress value for non-indeterminate mode */ @@ -166,7 +208,14 @@ public void setProgressInterpolator(final TimeInterpolator interpolator) { mProgressAnimator.end(); } } - mProgressAnimator.setInterpolator(interpolator == null ? DEFAULT_PROGRESS_ANIMATOR : interpolator); + mProgressAnimator.setInterpolator(interpolator == null ? DEFAULT_PROGRESS_ANIMATION_INTERPOLATOR : interpolator); + } + + /** + * Returns progress animator used to animate setting the progress + */ + public TimeInterpolator getProgressInterpolator() { + return mProgressAnimator.getInterpolator(); } /** @@ -615,15 +664,15 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut } } } - mProgressAnimator.setInterpolator(DEFAULT_PROGRESS_ANIMATOR); + mProgressAnimator.setInterpolator(DEFAULT_PROGRESS_ANIMATION_INTERPOLATOR); mProgressAnimator.addUpdateListener(new ProgressUpdateListener()); mIndeterminateStartAnimator.setFloatValues(360f); mIndeterminateStartAnimator.setRepeatMode(ValueAnimator.RESTART); mIndeterminateStartAnimator.setRepeatCount(ValueAnimator.INFINITE); - mIndeterminateStartAnimator.setInterpolator(new LinearInterpolator()); + mIndeterminateStartAnimator.setInterpolator(DEFAULT_START_ANIMATION_INTERPOLATOR); mIndeterminateStartAnimator.addUpdateListener(new StartUpdateListener()); mIndeterminateSweepAnimator.setFloatValues(360f - mIndeterminateMinimumAngle * 2f); - mIndeterminateSweepAnimator.setInterpolator(new DecelerateInterpolator()); + mIndeterminateSweepAnimator.setInterpolator(DEFAULT_SWEEP_ANIMATION_INTERPOLATOR); mIndeterminateSweepAnimator.addUpdateListener(new SweepUpdateListener()); mIndeterminateSweepAnimator.addListener(new SweepAnimatorListener()); }