如何用预设时间写一个自定义时间的ProgressBar?

How to write a custom time ProgressBar with preset time?

我想写一段代码,预见 ProgressBar 30 秒。

例如,我们有一个计时器,让用户决定是否按下这个按钮 30 秒,同时,我们希望用户显示一个状态栏 30 秒

我是用 Runnable 还是 For loop 做这个?

要显示进度 30 秒,您可以这样做

在你的 activity/fragment

 public class MainActivity extends AppCompatActivity implements TimerTask{

    Timer timer;
    ProgressDialog progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        timer = new Timer();
        progressBar = new ProgressDialog(this);  
        progressBar.setCancelable(true);//you can cancel it by pressing back button  
        progressBar.setMessage("Something downloading ...");  
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
        progressBar.setProgress(0); 
        progressBar.setMax(100);
        progressBar.show(); 
        timer.schedule(this,2000);
     }

 @Override
    public void run() {
        //here dismiss the progress dialog
        progressBar.dismiss();
    }

 }

我用了这个class,我的问题就解决了

public class RequestActivity extends AppCompatActivity {
    private ProgressBar progress_bar;
    private TextView    progress_count;
    private int         REQUEST_TIMER  = 0;
    private Timer       timer          = new Timer(); 
    private Handler     handler        = new Handler();
    private int         counter        = 0; 
    private timerTask   task           = new timerTask();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_request);
        progress_bar   = (ProgressBar) findViewById(R.id.progress_bar);
        progress_count = (TextView)    findViewById(R.id.progress_count);
        REQUEST_TIMER  = getIntent().getIntExtra("timeout", 0);
        progress_bar.setMax(REQUEST_TIMER);
        timer.scheduleAtFixedRate(task, 500, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        task.cancel();
    }

    private class timerTask extends TimerTask {
        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    counter++;
                    progress_count.setText(
                        String.format(Locale.getDefault(), "%d", counter));
                    progress_bar.setProgress(counter);
                    if (counter >= REQUEST_TIMER) {
                        TravelRequestActivity.this.finish();
                        Toast.makeText(
                            getApplicationContext(), "STOP", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}

试试这个:

private void setProgressDialogAnimation(ProgressBar mProgressBar, int process) {
    ObjectAnimator animation = ObjectAnimator.ofInt(mProgressBar, "progress", 0, process);
    animation.setDuration(30000);//30 second
    animation.setInterpolator(new DecelerateInterpolator());
    animation.start();
}