weera
weera

Reputation: 944

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?

Upvotes: 0

Views: 762

Answers (2)

Lena Bru
Lena Bru

Reputation: 13947

Assuming there is an imageview with this vector drawable on it

ImageView imageView = view.findViewById(R.id.animatedImage);

AnimatedVectorDrawable drawable = (AnimatedVectorDrawable)imageView.getDrawable();

drawable.start(); //this starts the animation

drawable.setVisible(true,true); //this also starts the animation from start

Upvotes: 0

Tenfour04
Tenfour04

Reputation: 93789

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()

Upvotes: 1

Related Questions