android中,如何创建Frame Animation?

来源:互联网 发布:mysql官方文档有问题 编辑:程序博客网 时间:2024/04/30 14:10

Frame Animation是在一系列的图片之间产生切换的动画效果。在xml文件描述不同的图片,文件放在res/anim目录下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="
http://schemas.android.com/apk/res/android" android:oneshot="true">
  <item android:drawable="@drawable/icon1" android:duration="1000"></item>
  <item android:drawable="@drawable/icon2" android:duration="1000"></item>
  <item android:drawable="@drawable/icon3" android:duration="1000"></item>
</animation-list>

android:oneshot用于指定动画是播放一次还是循环播放。

创建一个java类文件:

    ImageButton image=(ImageButton)findViewById(R.id.image_button);
    image.setBackgroundResource(R.anim.frame_anim);
    AnimationDrawable anim=(AnimationDrawable)image.getBackground();
    anim.start();

值得注意的是,Frame Animation不能在Activity中的onCreate()方法启动,因为此时对应的动画资源没有准备好,可以在onWindowFocusChanged()方法内使用。

原创粉丝点击