Android 中的动画进度条更新 [英] Animate ProgressBar update in Android

查看:34
本文介绍了Android 中的动画进度条更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了 ProgressBar,我在 AsyncTask 的 onProgressUpdate 中更新了它.到现在为止还挺好.

I am using a ProgressBar in my application which I update in onProgressUpdate of an AsyncTask. So far so good.

我想要做的是为进度更新设置动画,使其不仅跳"到该值,而是平滑地移动到该值.

What I want to do is to animate the progress update, so that it does not just "jump" to the value but smoothly moves to it.

我尝试运行以下代码:

this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            while (progressBar.getProgress() < progress) {
                progressBar.incrementProgressBy(1);
                progressBar.invalidate();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    });

问题是进度条在完成其最终值(进度变量)之前不会更新其状态.两者之间的所有状态都不会显示在屏幕上.调用 progressBar.invalidate() 也没有帮助.

The problem is that the progress bar does not update its state until it finished its final value (progress variable). All states in between are not displayed on the screen. Calling progressBar.invalidate() didn't help either.

有什么想法吗?谢谢!

推荐答案

我为此使用了 android Animation:

I used android Animation for this:

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 和 to 值太低而无法产生平滑的动画,只需将它们乘以 100 左右即可.如果这样做,请不要忘记将 setMax(..) 也相乘.

Note: if from and to value are too low to produce smooth animation just multiply them by a 100 or so. If you do so, don't forget to multiply setMax(..) as well.

这篇关于Android 中的动画进度条更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆