Use animatedVectorDrawable in custom view

896 Views Asked by At

I know we can draw a vector drawable like this on canvas:

vectorDrawable.setBounds(left, top, right, bottom);
vectorDrawable.draw(canvas);

but Is it possible to use animatedVectorDrawables in custom view?

1

There are 1 best solutions below

0
On

Yes, it is possible, and looks like the snippet you have would work. Be sure to set bounds and call the start() method on the drawable to start the animation.

class MyCustomView : View {

  lateinit var animVectDrawable: AnimatedVectorDrawable

  fun startAnim() {
    animVectDrawable.setBounds(left, top, right, bottom)
    animVectDrawable.start()
    shouldDrawAnim = true
  }

  override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    if (shouldDrawAnim) {
      animVectDrawable.draw(canvas)
    }
  }
}