如何在Kotlin中制作动画列表动画?

Mik*_*yan 2 android background android-animation kotlin

我有一个animation list XMLdrawables:

<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>
Run Code Online (Sandbox Code Playgroud)

我打电话list给我content_main.xml:

<ImageView
    ...
    android:id="@+id/animatedPerson"
    android:src="@drawable/animatedperson"
    ...
/>
Run Code Online (Sandbox Code Playgroud)

现在无论我尝试使用我的代码,我都无法启动动画.

ego*_*ldx 8

在Java中它是这样的:

// 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();
Run Code Online (Sandbox Code Playgroud)

所以Kotlin会非常相似:

// 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()
Run Code Online (Sandbox Code Playgroud)