我创建了一个倒计时活动,进度条移动了几秒钟,但我想让进度条更顺畅。请帮帮我。 这是我的代码,如果你想我可以上传我的应用程序。 当一秒钟减小进度条增加时,进度条会以秒为单位移动我希望进度条以毫秒为单位增加并顺利移动。
package com.cinegoes.www.daily10exercise;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import com.cinegoes.www.daily10exercise.R;
import com.cinegoes.www.daily10exercise.SaveRestTime;
import com.cinegoes.www.daily10exercise.button3;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
import static com.cinegoes.www.daily10exercise.SaveRestTime.mypreference;
/**
* Created by ever on 7/25/2017.
*/
public class t1 extends Activity implements View.OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerStarted = false;
private Button buttonStart;
public TextView textView;
private ProgressBar progressBarCircle;
private final long startTime = 30 * 1000;
private final long interval = 1 * 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
buttonStart = (Button) this.findViewById(R.id.button);
progressBarCircle = (ProgressBar) findViewById(R.id.progressBarCircle);
buttonStart.setOnClickListener(this);
textView = (TextView) this.findViewById(R.id.textView);
countDownTimer = new CountDownTimerActivity(startTime, interval);
textView.setText(textView.getText() + String.valueOf(startTime / 1000));
}
@Override
protected void onStart() {
super.onStart();
countDownTimer.start();
timerStarted = true;
setProgressBarValues();
}
@Override
public void onClick(View v) {
Intent i = new Intent(this, pushupe1.class);
startActivity(i);
finish();
}
public class CountDownTimerActivity extends CountDownTimer {
public CountDownTimerActivity(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
textView.setText(msTimeFormatter(startTime));
textView.setText("Time's up!");
setProgressBarValues();
Intent i = new Intent(getBaseContext(), pushupe1.class);
startActivity(i);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
textView.setText(msTimeFormatter(millisUntilFinished));
progressBarCircle.setProgress((int) (millisUntilFinished / 1000));
}
}
private void setProgressBarValues() {
progressBarCircle.setMax((int) startTime / 1000);
progressBarCircle.setProgress((int) startTime / 1000);
}
private String msTimeFormatter(long milliSeconds) {
String ms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(milliSeconds) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
TimeUnit.MILLISECONDS.toSeconds(milliSeconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));
return ms;
}
}
答案 0 :(得分:1)
考虑让您的时间间隔更小,并为进度条的进度提供更大范围的值。
这里有一些我用于更新进度条进度的kotlin代码。
progressBar.max = INIT_TIME * 1000
countDown = object : CountDownTimer((INIT_TIME * 1000).toLong(), 10) {
override fun onTick(millisUntilFinished: Long) {
progressBar.progress = millisUntilFinished.toInt()
}
override fun onFinish() {}
}.start()
正如您所看到的,我的间隔时间更短,进度条的进度值范围为数千,从而可以实现更顺畅的进度条动画。