如何在 Kotlin 中为动画列表设置动画?

How to animate animation-list in Kotlin?

我在 drawables 中有一个 animation list XML:

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

我从 content_main.xml:

中调用了 list
<ImageView
    ...
    android:id="@+id/animatedPerson"
    android:src="@drawable/animatedperson"
    ...
/>

现在无论我用我的代码尝试什么,我都无法启动动画。

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

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

Kotlin 类似于 java...

// 获取背景,已编译为AnimationDrawable对象

val frameAnimation: AnimationDrawable = img.background as AnimationDrawable

// 开始播放动画(默认循环播放)

frameAnimation.start()

另一种方法是通过按 ctrl + shift + a 将 java 代码转换为 kotlin,然后在操作字段中,写下

convert java to Kotlin..

它会自动转换kotlin中的java代码。

没人问,但我在安口做了,你不需要后台!?只是为了某人如何实现很棒的动画......

我是真正的安口粉!

loadinganim 是 animation-list .xml

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

loadingAnim = loadingView.drawable as AnimationDrawable
loadingAnim.start()