Solution 1 :
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()
Solution 2 :
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
Problem :
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?
Comments
Comment posted by Tenfour04
FYI, that’s not a tutorial. It’s the official documentation of the class.
Comment posted by weera
Thank you! It wasted all of my day finding a proper solution for it!!!
Comment posted by Tenfour04
Sure thing. You can also cast it to AnimatedVectorDrawable, but Animatable is more versatile because all the different types of animated drawables implement it so your code won’t have to change if you switch to a different type of animated drawable.