查看动画以更新进度

时间:2014-03-17 20:32:40

标签: android android-animation

我正在使用一个水平进度条,我正在试图填充条形图(我在条形图出现之前加载了进度)。 这就是我到目前为止所做的事情,但它不起作用:

progressBar = (ProgressBar)v.findViewById(R.id.rewards_progress);
Animation animation = new Animation() {
    @Override
    public void start() {
        progressBar.setInterpolator(new AccelerateDecelerateInterpolator());
        progressBar.setProgress(currentProgress);
    }
};
animation.setDuration(1000);
animation.start();

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您只是想更新ProgressBar,正如我们在之前的评论中所说,您需要在另一个线程中执行此操作,请查看基于官方Android文档的虚拟示例:

package com.example.progressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar mProgress;
    private static int mProgressStatus = 0;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mProgress = (ProgressBar) findViewById(R.id.rewards_progress);

         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     doWork();
                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
    }

    private void doWork(){
        try {
            Thread.sleep(100);
            mProgressStatus+=2;
        } catch (InterruptedException e) {
        }
    }

}

但是,如果要为ProgressBar更新效果设置动画,有几种方法可以实现它。 this post中描述的一种特殊方式看起来非常好,可以通过以下方式轻松完成:

package com.example.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgress = (ProgressBar) findViewById(R.id.rewards_progress);

        ProgressBarAnimation anim = new ProgressBarAnimation(mProgress, 0, 100);
        anim.setDuration(5000);
        mProgress.startAnimation(anim);     
    }   

    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的参数fromto,以便以实际方法应用它。