动画进度更新在安卓进度、动画

2023-09-13 01:08:40 作者:生性凉薄

我使用的是进度在我的应用程序,我在的AsyncTask的onProgressUpdate更新。 到目前为止,一切都很好。我想要做的是动画的最新进展情况,因此,它不只是跳的价值,但顺利地移动到它。

I am using a ProgressBar in my application which I update in onProgressUpdate of a 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.

我试图这样做运行以下code:

I tried doing so running the following code:

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 ProgressBar 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. Any ideas? Thanks!

推荐答案

我用Android的动画这样的:

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

请注意:如果从和值过低,产生平滑的动画仅仅由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.