Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate animation-list in Kotlin?

I have an animation list XML in drawables:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:duration="40" android:drawable="@drawable/animated_person_0 />
    <item android:duration="40" android:drawable="@drawable/animated_person_1 />
    <item android:duration="40" android:drawable="@drawable/animated_person_2 />
</animated-list>

I called the list from my content_main.xml:

<ImageView
    ...
    android:id="@+id/animatedPerson"
    android:src="@drawable/animatedperson"
    ...
/>

And now whatever I try with my code, I can't start the animation.

like image 305
Mikayel Saghyan Avatar asked Dec 11 '25 17:12

Mikayel Saghyan


2 Answers

in Java it's like this:

// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();

So Kotlin will be really similar:

// Get the background, which has been compiled to an AnimationDrawable object.
val frameAnimation: AnimationDrawable = img.background as AnimationDrawable

// Start the animation (looped playback by default).
frameAnimation.start()
like image 63
egoldx Avatar answered Dec 13 '25 08:12

egoldx


Nobody asked, but i did it in ANKO, you don't need the background!? Just for somebody how whats to implement awesome animations…

I am a real ANKO fan!

loadinganim is the animation-list .xml

 loadingAnim = imageView{
   imageResource = R.drawable.loadinganim
  }

loadingAnim = loadingView.drawable as AnimationDrawable
loadingAnim.start()
like image 36
Tom Peak Avatar answered Dec 13 '25 07:12

Tom Peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!