I have an animated vector drawable asset in my drawables folder. I use the following code to run it on button click
val myVectorDrawable = ResourcesCompat.getDrawable(
resources,
R.drawable.animation,
theme
)
button.setOnClickListener {
image.setImageDrawable(null)
image.setImageDrawable(myVectorDrawable)
val drawable = image.drawable
if (drawable is AnimatedVectorDrawableCompat) {
drawable.start()
} else if (drawable is AnimatedVectorDrawable)
drawable.start()
}
This runs perfectly if the device runs an android version > 24 and crashes otherwise. I need to support android devices with minimum SDK 21.
My questions are
- How to make my code support devices with
21
up to24
. - is there a better way to run
AnimatedVectorDrawable
animation