Use animatedVectorDrawable in custom view

856 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 answers

0
Patrick Jackson 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)
    }
  }
}