如何动画动画列表在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然后在行动feild,写下来
将Java转换成Kotlin ..
它会自动转换kotlin中的java代码。