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

Android 如何在Kotlin中设置动画列表的动画?,android,background,android-animation,kotlin,Android,Background,Android Animation,Kotlin,我在drawables中有一个动画列表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

我在
drawables
中有一个
动画列表
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>

现在,无论我如何尝试我的代码,我都无法启动动画。

在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();
因此,科特林将非常相似:

// 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
//启动动画(默认情况下为循环播放)

另一种方法是使用convert java代码到kotlin,方法是按ctrl+shift+a,然后在action feild中写下

将java转换为Kotlin


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

没有人问,但我是在ANKO中做的,你不需要背景!?只是有人如何实现可怕的动画

我是一个真正的安科迷

loadinganim是animation list.xml

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

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

请显示一些您尝试过的代码。这不会有帮助。。。我是一个初学者,我不确定这是否会有帮助。例如:animatedPerson.animate()。在Java中,据我所知,我应该使用android:background和getBackground.animate()。但我在科特林找不到类似的东西。
frameAnimation.start()
 loadingAnim = imageView{
   imageResource = R.drawable.loadinganim
  }

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