Reputation: 77
I am trying to create a smooth progressbar with a countdown timer. I want the timer will countdown upto 20 sec.I am using the below code but I unable to achieve the smooth decending of the progressbar. Countdown tick function is working fine with the progressbar, but not in case of smooth behaviour. I found the below code from stackoverflow which i used in my project, but can you tell me what i am doing wrong.
if(android.os.Build.VERSION.SDK_INT >= 11){
// will update the "progress" propriety of seekbar until it reaches progress
ObjectAnimator animation = ObjectAnimator.ofInt(progressBarTimer, "progress", 0, 500);
animation.setDuration(20000); // 0.5 second
animation.setInterpolator(new LinearInterpolator());
animation.start();
}
else
progressBarTimer.setProgress(100);
Upvotes: 3
Views: 4409
Reputation: 1
Kotlin Code
import android.animation.ObjectAnimator;
import android.view.animation.DecelerateInterpolator;
public void setProgressWithAnimation(float progress) {
ObjectAnimator anim = ObjectAnimator.ofFloat(this, "show progress", progress);
anim.setDuration(6000);
anim.setInterpolator(new DecelerateInterpolator());
anim.start();
}
Upvotes: 0
Reputation: 1439
I achieved this by following code:
val ORDER_ACCEPT_TIME = 5000L //5 seconds
val INTERVAL = 30
progressBar.max = ORDER_ACCEPT_TIMER_TIME
private fun startTimer(){
pauseTimer = object : CountDownTimer(ORDER_ACCEPT_TIMER_TIME, INTERVAL){
override fun onTick(millisUntilFinished: Long) {
val secondsInMilli: Long = 1000
val diff = millisUntilFinished
val elapsedSeconds = diff / secondsInMilli
val progress = INTERVAL + progressBar.progress
progressBar.progress = progress.toInt()
tvProgress.text = elapsedSeconds.toString() //(optional) to show countdown textview
}
override fun onFinish() {
}
}
pauseTimer.start()
}
Upvotes: 0
Reputation: 807
I just Divide progressbar to 1000 parts and call every 100 milisecond so it ll be more smooth
public class MainActivity extends AppCompatActivity {
ProgressBar barTimer;
CountDownTimer countDownTimer;
TextView textTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barTimer = findViewById(R.id.barTimer);
barTimer.setMax(1000);
startTimer(10);
}
private void startTimer(final int minuti) {
countDownTimer = new CountDownTimer(60 * minuti * 1000, 100) {
@Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 600;
barTimer.setProgress((int)seconds);
}
@Override
public void onFinish() {
}
}.start();
}
}
Upvotes: 2