Animate ProgressBar update in Android

后端 未结 13 1148
悲哀的现实
悲哀的现实 2020-11-28 04:04

I am using a ProgressBar in my application which I update in onProgressUpdate of an AsyncTask. So far so good.

What I want to do is to animate the prog

13条回答
  •  有刺的猬
    2020-11-28 04:33

    I just wanted to add an extra value for those who want to use Data Binding with a progress bar animation.

    First create the following binding adapter:

    @BindingAdapter("animatedProgress")
    fun setCustomProgressBar(progressBar: ProgressBar, progress: Int) {
        ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, progress).apply {
            duration = 500
            interpolator = DecelerateInterpolator()
        }.start()
    }
    

    And then use it in the layout which contains the ViewModel that reports the status updates:

    
    

    The ViewModel itself will report the status with the following LiveData:

    private val _progress = MutableLiveData(null)
    val progress: LiveData
        get() = _
    

提交回复
热议问题