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

添加了动画过渡效果 #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -32,11 +32,11 @@ public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bnp.incrementProgressBy(1);
bnp.incrementProgressBy(5);
}
});
}
}, 1000, 100);
}, 1000, 1000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import static com.daimajia.numberprogressbar.NumberProgressBar.ProgressTextVisibility.Invisible;
import static com.daimajia.numberprogressbar.NumberProgressBar.ProgressTextVisibility.Visible;
Expand All @@ -19,6 +20,7 @@
*/
public class NumberProgressBar extends View {

private static final long ANIMATION_DURATION = 300;
private int mMaxProgress = 100;

/**
Expand Down Expand Up @@ -150,10 +152,15 @@ public class NumberProgressBar extends View {

private boolean mIfDrawText = true;

private DecelerateInterpolator mInterpolator;


/**
* Listener
*/
private OnProgressBarListener mListener;
private int mAnimationFrom;
private long mAnimationStartTime;

public enum ProgressTextVisibility {
Visible, Invisible
Expand Down Expand Up @@ -193,6 +200,8 @@ public NumberProgressBar(Context context, AttributeSet attrs, int defStyleAttr)
mIfDrawText = false;
}

mInterpolator = new DecelerateInterpolator();

setProgress(attributes.getInt(R.styleable.NumberProgressBar_progress_current, 0));
setMax(attributes.getInt(R.styleable.NumberProgressBar_progress_max, 100));

Expand Down Expand Up @@ -254,6 +263,12 @@ protected void onDraw(Canvas canvas) {

if (mIfDrawText)
canvas.drawText(mCurrentDrawText, mDrawTextStart, mDrawTextEnd, mTextPaint);

if (needAnimation()) invalidate();
}

private boolean needAnimation() {
return System.currentTimeMillis() - mAnimationStartTime <= ANIMATION_DURATION;
}

private void initializePainters() {
Expand All @@ -272,7 +287,7 @@ private void initializePainters() {
private void calculateDrawRectFWithoutProgressText() {
mReachedRectF.left = getPaddingLeft();
mReachedRectF.top = getHeight() / 2.0f - mReachedBarHeight / 2.0f;
mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getProgress() + getPaddingLeft();
mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getAnimateProgress() + getPaddingLeft();
mReachedRectF.bottom = getHeight() / 2.0f + mReachedBarHeight / 2.0f;

mUnreachedRectF.left = mReachedRectF.right;
Expand All @@ -294,7 +309,7 @@ private void calculateDrawRectF() {
mDrawReachedBar = true;
mReachedRectF.left = getPaddingLeft();
mReachedRectF.top = getHeight() / 2.0f - mReachedBarHeight / 2.0f;
mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getProgress() - mOffset + getPaddingLeft();
mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getAnimateProgress() - mOffset + getPaddingLeft();
mReachedRectF.bottom = getHeight() / 2.0f + mReachedBarHeight / 2.0f;
mDrawTextStart = (mReachedRectF.right + mOffset);
}
Expand Down Expand Up @@ -348,6 +363,12 @@ public int getProgress() {
return mCurrentProgress;
}

private float getAnimateProgress() {
final long time = System.currentTimeMillis() - mAnimationStartTime;
if (time >= ANIMATION_DURATION) return getProgress();
return mAnimationFrom + mInterpolator.getInterpolation((float) time / ANIMATION_DURATION) * (getProgress() - mAnimationFrom);
}

public int getMax() {
return mMaxProgress;
}
Expand Down Expand Up @@ -428,18 +449,25 @@ public void incrementProgressBy(int by) {
setProgress(getProgress() + by);
}

if(mListener != null){
if (mListener != null) {
mListener.onProgressChange(getProgress(), getMax());
}
}

public void setProgress(int progress) {
if (progress <= getMax() && progress >= 0) {
startAnimation(this.mCurrentProgress, progress);
this.mCurrentProgress = progress;
invalidate();
}
}

private void startAnimation(int from, int to) {
this.mAnimationFrom = from;
this.mAnimationStartTime = System.currentTimeMillis();
}


@Override
protected Parcelable onSaveInstanceState() {
final Bundle bundle = new Bundle();
Expand Down Expand Up @@ -499,7 +527,7 @@ public boolean getProgressTextVisibility() {
return mIfDrawText;
}

public void setOnProgressBarListener(OnProgressBarListener listener){
public void setOnProgressBarListener(OnProgressBarListener listener) {
mListener = listener;
}
}