[英]Smooth Circular Progressbar Animation Android
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.
我希望计时器倒计时到 20 秒。我正在使用下面的代码,但我无法实现进度条的平滑下降。 Countdown tick function is working fine with the progressbar, but not in case of smooth behaviour.
倒计时刻度 function 与进度条一起工作正常,但在平滑行为的情况下则不然。 I found the below code from stackoverflow which i used in my project, but can you tell me what i am doing wrong.
我从我在项目中使用的 stackoverflow 中找到了以下代码,但是你能告诉我我做错了什么吗?
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);
I just Divide progressbar to 1000 parts and call every 100 milisecond so it ll be more smooth我只是将进度条分成 1000 个部分,每 100 毫秒调用一次,这样会更流畅
public class MainActivity extends AppCompatActivity {公共 class MainActivity 扩展 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();
}
}
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()
}
Kotlin Code Kotlin 代码
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.