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

时间:2017-08-09 08:50:42

标签: android background android-animation kotlin

我在animation list中有XML 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>

我从我的list

中拨打了content_main.xml
<ImageView
    ...
    android:id="@+id/animatedPerson"
    android:src="@drawable/animatedperson"
    ...
/>

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

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

没有人问,但是我是在ANKO中完成的,您不需要背景!?对于某人来说,如何实现出色的动画……

我是ANKO的忠实粉丝!

loadinganim是.xml动画列表

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

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

答案 2 :(得分:-1)

Kotlin与java相似......

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

Memcheck, a memory error detector
Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
Command: ./test

[... program output ...]

HEAP SUMMARY:
    in use at exit: 140 bytes in 6 blocks
  total heap usage: 19 allocs, 13 frees, 74,348 bytes allocated

140 (40 direct, 100 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
   at 0x4C2C97F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x10883E: createTable(int, int) (in /home/user/code/test/test)
   by 0x108A26: main (in /home/user/code/test/test)

LEAK SUMMARY:
   definitely lost: 40 bytes in 1 blocks
   indirectly lost: 100 bytes in 5 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 0 bytes in 0 blocks
        suppressed: 0 bytes in 0 blocks

For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

//启动动画(默认循环播放)。

val frameAnimation: AnimationDrawable = img.background as AnimationDrawable

另一种方法是通过按ctrl + shift + a然后在操作中使用转换java代码到kotlin,写下来

  

将java转换为Kotlin ..

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