顺利进行ProgressBar更新

我正在使用进度条(以条forms)。 我希望使用一个插补器来平滑地增加和减less,但它不工作。 这是我现在所拥有的:

pb.setInterpolator(main.this, android.R.anim.bounce_interpolator); pb.setProgress(pb.getProgress()+10); 

我做错了什么吗?

Interpolator必须附加到一个animation,这将只适用于蜂窝或更高版本:

 if(android.os.Build.VERSION.SDK_INT >= 11){ // will update the "progress" propriety of seekbar until it reaches progress ObjectAnimator animation = ObjectAnimator.ofInt(seekbar, "progress", progress); animation.setDuration(500); // 0.5 second animation.setInterpolator(new DecelerateInterpolator()); animation.start(); } else seekbar.setProgress(progress); // no animation on Gingerbread or lower 

如果您的最低SDK是姜饼或更低,添加

 @TargetApi(Build.VERSION_CODES.HONEYCOMB) // or @SuppressLint("NewApi") 

到你的function/类。

我使用了一个DecelerateInterpolator,但是这是可选的,还有其他的可能性。

这是一个独立的解决scheme:

 import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.content.Context; import android.util.AttributeSet; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Interpolator; import android.widget.ProgressBar; public class AnimatingProgressBar extends ProgressBar { private static final Interpolator DEFAULT_INTERPOLATER = new AccelerateDecelerateInterpolator(); private ValueAnimator animator; private ValueAnimator animatorSecondary; private boolean animate = true; public AnimatingProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public AnimatingProgressBar(Context context, AttributeSet attrs) { super(context, attrs); } public AnimatingProgressBar(Context context) { super(context); } public boolean isAnimate() { return animate; } public void setAnimate(boolean animate) { this.animate = animate; } @Override public synchronized void setProgress(int progress) { if (!animate) { super.setProgress(progress); return; } if (animator != null) animator.cancel(); if (animator == null) { animator = ValueAnimator.ofInt(getProgress(), progress); animator.setInterpolator(DEFAULT_INTERPOLATER); animator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { AnimatingProgressBar.super.setProgress((Integer) animation.getAnimatedValue()); } }); } else animator.setIntValues(getProgress(), progress); animator.start(); } @Override public synchronized void setSecondaryProgress(int secondaryProgress) { if (!animate) { super.setSecondaryProgress(secondaryProgress); return; } if (animatorSecondary != null) animatorSecondary.cancel(); if (animatorSecondary == null) { animatorSecondary = ValueAnimator.ofInt(getProgress(), secondaryProgress); animatorSecondary.setInterpolator(DEFAULT_INTERPOLATER); animatorSecondary.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { AnimatingProgressBar.super.setSecondaryProgress((Integer) animation .getAnimatedValue()); } }); } else animatorSecondary.setIntValues(getProgress(), secondaryProgress); animatorSecondary.start(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (animator != null) animator.cancel(); if (animatorSecondary != null) animatorSecondary.cancel(); } } 

用布局中的AnimatingProgressBar代替ProgressBar

许多人也将types更改为AnimatingProgressBar以利用setAnimate()来禁用animation(在恢复活动状态时可能有用)

如果每次更改进度值1(例如从45到46),则不会看到animation。 最好改变100点的进度,为此,你只需要将你的最大值乘以100,每个进度值乘以100即可。 例如:

 private void setProgressMax(ProgressBar pb, int max) { pb.setMax(max * 100); } private void setProgressAnimate(ProgressBar pb, int progressTo) { ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 100); animation.setDuration(500); animation.setInterpolator(new DecelerateInterpolator()); animation.start(); } 

我想出了如何做到这一点,通过使用一个可运行的,我能够每秒更新一次进度条几次,所以给滑动效果。 代码如下:

 private Runnable SmoothIncrement = new Runnable() { public void run() { final long start = mStartTime; long millis = SystemClock.uptimeMillis() - start; if(track!=increase) { if((pb.getProgress()==100)&&(count<target)) { pb.setProgress(0); } pb.incrementProgressBy(1); track++; incrementor.postAtTime(this, start + millis); } else { incrementor.removeCallbacks(this); } } }; 

在这里,“跟踪”跟踪已经完成了多less增量,增加是应该完成的增量总数。 我可以dynamic增加UI线程的增量数量,以获得平滑的效果。 代码仅适用于不需要减less的进度条。

要运行它,只需使用以下代码:

  mStartTime = System.currentTimeMillis(); incrementor.removeCallbacks(SmoothIncrement); if(track!=0) { track -= increase; } incrementor.postDelayed(SmoothIncrement, 0); 

我的图书馆链接

以上链接是我的图书馆。 只要你想使用它。

根据文件插补器应用于不确定的进展。 既然你设定了进度,我想你打算使用正常的值。 我认为最适合你的就是提高最大进步值,并以较小的增量进行。

我用这个androidanimation:

 public class ProgressBarAnimation extends Animation{ private ProgressBar progressBar; private float from; private float to; public ProgressBarAnimation(ProgressBar progressBar, float from, float to) { super(); this.progressBar = progressBar; this.from = from; this.to = to; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float value = from + (to - from) * interpolatedTime; progressBar.setProgress((int) value); } } 

并像这样调用它:

 ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to); anim.setDuration(1000); progress.startAnimation(anim); 

注意:如果from和value的值太低,不能产生stream畅的animation,只需乘以100左右即可。 如果你这样做的话,不要忘记也要乘以setMax(..)。

我不确定,但请检查:

 pb.setProgress(pb.getProgress() * 100);