Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing for custom animations #6

Merged
merged 2 commits into from
Dec 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,10 @@ 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_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();
private final ValueAnimator mProgressAnimator = new ValueAnimator();
Expand Down Expand Up @@ -131,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
*/
Expand All @@ -154,6 +199,25 @@ 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_ANIMATION_INTERPOLATOR : interpolator);
}

/**
* Returns progress animator used to animate setting the progress
*/
public TimeInterpolator getProgressInterpolator() {
return mProgressAnimator.getInterpolator();
}

/**
* Maximum progress for non-indeterminate mode
*/
Expand Down Expand Up @@ -600,15 +664,15 @@ private void initialize(@NonNull final Context context, @Nullable final Attribut
}
}
}
mProgressAnimator.setInterpolator(new DecelerateInterpolator());
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());
}
Expand Down