如何使ProgressBar setProgress函数顺利移动

时间:2017-05-20 15:38:13

标签: android android-progressbar

我正在尝试根据计时器操作设置进度条的动画。我需要能够在倒计时期间暂停计时器和进度条。由于我需要暂停它,似乎不可能使用以下产生平滑动画:

ObjectAnimator anim = ObjectAnimator.ofInt(mprogressBar, "progress", 0, 100);
anim.setDuration((seconds * 1000));
anim.setInterpolator(new LinearInterpolator());
anim.start();

所以我使用.setProgress();而是每秒更新ProgressBar。但是,当从整数设置进度时,进度条会在我想要平滑的阶段之间跳转,除非有更好的方法来实现这一点。这是我目前的代码:

public class Detail_Activity extends AppCompatActivity {

    ProgressBar mprogressBar;
    Integer seconds = 10;
    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            seconds -= 1;

            mprogressBar.setProgress(seconds);
            timerHandler.postDelayed(this, 1000);

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.detail_activity);

        mprogressBar = (ProgressBar) findViewById(R.id.circular_progress_bar);
        Button b = (Button) findViewById(R.id.button_start);
        b.setText("Start");
        b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Button b = (Button) v;
            if (b.getText().equals("Stop")) {
                timerHandler.removeCallbacks(timerRunnable);
                b.setText("Start");
            } else {
                startTime = System.currentTimeMillis();
                timerHandler.postDelayed(timerRunnable, 0);

                b.setText("Stop");
            }
        }
    });
}

    @Override
    public void onPause() {
        super.onPause();
        timerHandler.removeCallbacks(timerRunnable);
        Button b = (Button)findViewById(R.id.button_start);
        b.setText("Start");
    }
}

0 个答案:

没有答案