《疯狂Android讲义》 -- Android 动画系列之逐帧(Frame)动画

来源:互联网 发布:苹果机下载软件 编辑:程序博客网 时间:2024/05/07 08:44

前言

        逐帧(Frame)动画是最容易理解的动画,它要求开发者把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,再利用人眼“视觉暂留”的原理,给用户造成“动画”的错觉。逐帧动画的动画原理与放电影的原理完全一样。

AnimationDrawable与逐帧动画

        前面讲解定义Android资源时已经介绍了动画资源,事实上逐帧动画通常也是采用XML资源文件进行定义的。
        在res/drawable/目录下新建一个动画文件,根节点选择animation-list。这里可能有人很好奇,为什么动画文件不是在anim目录下而是在drawable目录下呢?其实逐帧动画对应的类是:AnimationDrawable其父类就是一个Drawable对象。
        定义逐帧动画非常简单,只要在<animation-list.../>元素中使用<item.../>子元素定义动画的全部帧,并指定隔帧的持续时间即可。定义逐帧动画的语法如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/fat_po_f01"
android:duration="60" />
 
</animation-list>
        在上面的语法格式中,android:oneshot控制该动画是否循环播放,如果该参数指定为true,则动画将不会循环播放,否则该动画将会循环播放。每个<item.../>子元素添加一帧,例如如下代码。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/fat_po_f01"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f02"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f03"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f04"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f05"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f06"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f07"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f08"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f09"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f10"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f11"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f12"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f13"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f14"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f15"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f16"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f17"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f18"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f19"
android:duration="60" />
<item
android:drawable="@drawable/fat_po_f20"
android:duration="60" />
 
</animation-list>
      在Activity的代码如下:
ImageView iv = (ImageView) findViewById(R.id.iv);
AnimationDrawable drawable = (AnimationDrawable) iv.getBackground();
drawable.start();
        Android完全支持在Java代码中创建逐帧动画,如果开发者喜欢,则完全可以先创建AnimationDrawable对象,然后调用addFrame(Drawable frame, int duration)向该动画中添加帧,每调用一次addFrame()方法,就向<animation-list.../>元素中添加一个<item.../>子元素,例如如下代码。
AnimationDrawable animationDrawable = new AnimationDrawable();
// 设置是否需要循环播放 false:循环;true:播放一次
animationDrawable.setOneShot(false);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f01,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f02,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f03,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f04,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f05,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f06,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f07,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f08,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f09,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f10,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f11,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f12,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f13,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f14,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f15,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f16,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f17,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f18,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f19,null),60);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.fat_po_f20,null),60);
 
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setBackground(animationDrawable);
animationDrawable.start();

参考资料

  • 《疯狂Android讲义》
  • http://blog.csdn.net/airsaid/article/details/51559797
        
0 0
原创粉丝点击