Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android custom indeterminate progress bar using ImageView and Animated Vector Drawable
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
/**
* Custom loading indicator using Animated vector drawable.
*
* ## External Resources
* - https://medium.com/androiddevelopers/animation-jump-through-861f4f5b3de4
* - https://gist.github.com/nickbutcher/97143b3240682e5c5851fe45b49fde93
* - https://medium.com/androiddevelopers/re-animation-7869722af206
* - https://gist.github.com/nickbutcher/b1806905c6bc0ef29f545fd580935bd3
*/
class AvdLoadingProgressBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {
/*
* NOTE: It can only return null if parsing error is found.
* So, using `!!` operator should expose any issue with the AVD XML file in API 23 or lower.
*/
private val avd = AnimatedVectorDrawableCompat.create(context, R.drawable.avd_anim_kijiji_loading)!!
init {
setImageDrawable(avd)
avd.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
post { avd.start() }
}
})
avd.start()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.