Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。

下面就讲一下Frame Animation。

其实使用起来比较简单,首先需要创建一个AnimationDrawable对象,通过addFrame方法把每一帧要显示的内容添加进去,最后通过Start方法来播放动画。 同时还有设置循环setOneShot等方法可供使用。

下面先看一下官方对AnimationDrawable类的说明:

An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call run() to start the animation.

An AnimationDrawable defined in XML consists of a single <animation-list> element, and a series of nested <item> tags. Each item defines a frame of the animation. See the example below.

spin_animation.xml file in res/drawable/ folder:

<!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder --> <animation-list android:id="selected" android:oneshot="false">    <item android:drawable="@drawable/wheel0" android:duration="50" />    <item android:drawable="@drawable/wheel1" android:duration="50" />    <item android:drawable="@drawable/wheel2" android:duration="50" />    <item android:drawable="@drawable/wheel3" android:duration="50" />    <item android:drawable="@drawable/wheel4" android:duration="50" />    <item android:drawable="@drawable/wheel5" android:duration="50" /> </animation-list>

Here is the code to load and play this animation.

 // Load the ImageView that will host the animation and // set its background to our AnimationDrawable XML resource. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setBackgroundResource(R.drawable.spin_animation); // 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()
   
相信看了这个说明,大家也都知道如何做了吧? 
下面我们使用Frame动画做一个 loading 的效果。
首先准备素材,如下:

然后新建一个AnimationDrawable对象,把这些图片加载进去。 addFrame第一参数表示要加载的内容,第二参数表示持续时间。
代码:
[java] view plain copy print ?
  1. frameAnimation=newAnimationDrawable();
  2. for(inti=0;i<10;i++){
  3. intid=getResources().getIdentifier("load"+(i+1),"drawable",this.getContext().getPackageName());
  4. frameAnimation.addFrame(getResources().getDrawable(id),100);
  5. }
  6. //设置循环播放false表示循环true表示不循环,仅播放一次
  7. frameAnimation.setOneShot(false);
AnimationDrawable 类是Drawable类的子类,因此可以将 AnimationDrawable  设为背景等来查看效果。
最后只要调用  frameAnimation.start();  就可以启动该Frame动画了。
效果图改日补上。

那么上面是使用Java代码的方式来加载的Frame动画, 也可以像Tween动画那样,使用纯XML局部文件来做。 API中已经有说明,这边不再赘述了。
直接看示例:
[xhtml] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <animaltion-listxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <itemandroid:drawable="@drawable/load1"android:duration="50"/>
  5. <itemandroid:drawable="@drawable/load2"android:duration="50"/>
  6. <itemandroid:drawable="@drawable/load3"android:duration="50"/>
  7. <itemandroid:drawable="@drawable/load4"android:duration="50"/>
  8. <itemandroid:drawable="@drawable/load5"android:duration="50"/>
  9. <itemandroid:drawable="@drawable/load6"android:duration="50"/>
  10. <itemandroid:drawable="@drawable/load7"android:duration="50"/>
  11. <itemandroid:drawable="@drawable/load8"android:duration="50"/>
  12. <itemandroid:drawable="@drawable/load9"android:duration="50"/>
  13. <itemandroid:drawable="@drawable/load10"android:duration="50"/>
  14. </animaltion-list>
有不清楚之处,欢迎留言交流。
附上参数详细说明(来自:http://www.moandroid.com/?p=790)

表一

属性[类型] 功能
Duration[long] 属性为动画持续时间 时间以毫秒为单位
fillAfter [boolean] 当设置为true ,该动画转化在动画结束后被应用
fillBefore[boolean] 当设置为true ,该动画转化在动画开始前被应用

interpolator

指定一个动画的插入器 有一些常见的插入器
accelerate_decelerate_interpolator
加速-减速 动画插入器
accelerate_interpolator
加速-动画插入器
decelerate_interpolator
减速- 动画插入器
其他的属于特定的动画效果
repeatCount[int] 动画的重复次数
RepeatMode[int] 定义重复的行为 1:重新开始 2:plays backward
startOffset[long] 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
zAdjustment[int] 定义动画的Z Order的改变 0:保持Z Order不变
1:保持在最上层
-1:保持在最下层

更多相关文章

  1. Android(安卓)Tween 动画 (渐变、缩放、位移、旋转)
  2. Android仿ios年龄、生日、性别滚轮效果
  3. TextView常用属性介绍
  4. android缩放动画的两种实现方法
  5. Android(安卓)TableLayout中的使用说明
  6. Android加载SVG实现交互式地图绘制
  7. Android(安卓)5.0+ 高级动画开发系列 矢量图动画
  8. Android提供的系统服务之--WindowManager(窗口管理服务)
  9. 类加载器

随机推荐

  1. Android(安卓)开发之 ---- 底层驱动开发(
  2. 【可能有点用的记录】Android(安卓)Studi
  3. android完全退出应用程序
  4. android 丛书
  5. GET Android(安卓)摸拟器 IP.
  6. Android(安卓)复习笔记之图解TextView类
  7. android:configChanges="orientation|key
  8. Android各个版本API的区别
  9. [Android(安卓)Pro] 注册 Google Play 开
  10. 2月收藏