顺利进行ProgressBar更新

Tod*_*ies 52 java android android-layout

我正在使用进度条(条形图).我希望使用插补器使条形平滑增加和减少,但它不起作用.这就是我现在所拥有的:

pb.setInterpolator(main.this, android.R.anim.bounce_interpolator);             
pb.setProgress(pb.getProgress()+10);
Run Code Online (Sandbox Code Playgroud)

我做错了吗?

now*_*now 129

Interpolator必须附加到动画上,这只适用于Honeycomb或更高版本:

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
Run Code Online (Sandbox Code Playgroud)

如果您的最低SDK是Gingerbread或更低,请添加

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
// or 
@SuppressLint("NewApi") 
Run Code Online (Sandbox Code Playgroud)

到你的功能/类.

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

  • 它不光滑:( (4认同)
  • @HamidrezaHosseinkhani - 尝试`animation.setInterpolator(new LinearInterpolator());` (4认同)
  • 想要添加一些东西,如果你想让它从点x移动到点y,只需添加:ObjectAnimator animation = ObjectAnimator.ofInt(seekbar,"progress",now,end); 此外,如果它不平滑,请尝试新的LinearInterpolator() (2认同)

tom*_*136 18

这是一个独立的解决方案:

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

更换ProgressBarAnimatingProgressBar在布局

您还可以将类型更改为AnimatingProgressBar以setAnimate()用于禁用动画(在恢复活动状态时可能很有用)

  • 我希望<sdk 11的任何东西都会消失; 支持旧版本的Android就像支持IE6一样 (9认同)

Pav*_*kin 13

如果每次更改进度值1(例如从45到46),您将看不到动画.最好将进度改为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();
}
Run Code Online (Sandbox Code Playgroud)