androidkotlinanimatedvectordrawable

how to init animated vector in kotlin


I followed this tutorial and created my first animated vector in android studio.

my vector is:

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:aapt="http://schemas.android.com/aapt"
  android:width="491.4dp"
  android:height="297.83dp"
  android:viewportWidth="491.4"
  android:viewportHeight="297.83"
  android:drawable="@drawable/ic_logo">
  <target android:name="name1">
    <aapt:attr name="android:animation">
      <objectAnimator
        android:duration="2000"
        android:repeatCount="-1"
        android:repeatMode="reverse">
        <propertyValuesHolder android:propertyName="alpha" >
          <keyframe
            android:fraction="0"
            android:value="1f" />
          <keyframe
            android:fraction=".5"
            android:value="0f" />
          <keyframe
            android:fraction="1"
            android:value="1f" />
        </propertyValuesHolder>
      </objectAnimator>
    </aapt:attr>
  </target>
  <target android:name="name2">
    <aapt:attr name="android:animation">
      <objectAnimator
        android:duration="2000"
        android:repeatCount="-1"
        android:repeatMode="reverse">
        <propertyValuesHolder android:propertyName="alpha" >
          <keyframe
            android:fraction="0"
            android:value="1f" />
          <keyframe
            android:fraction=".5"
            android:value="0f" />
          <keyframe
            android:fraction="1"
            android:value="1f" />
        </propertyValuesHolder>
      </objectAnimator>
    </aapt:attr>
  </target>
</animated-vector>

It seems I need to init it in the activity to set animation to work. But I did not find any tutorial about how to init it in Kotlin. Could anyone help me please?


Solution

  • It depends how you apply this Drawable. If it's set on an ImageView, you can use

    (imageView.drawable as? Animatable)?.start()
    

    If it's the background of a view you can use

    (view.background as? Animatable)?.start()