盒子
盒子
文章目录
  1. 帧动画
    1. 1.简介:一种用XML定义的动画,按照顺序显示一系列图像(像放映电影一样)。
    2. 2.文件位置
    3. 3.资源引用方式
    4. 4.语法
    5. 5.元素
    6. 6.举例

Android动画资源(四)——帧动画资源

Android animations.png
Android动画资源系列文章:Android动画资源文件总结

帧动画

1.简介:一种用XML定义的动画,按照顺序显示一系列图像(像放映电影一样)。

2.文件位置

res/drawable/filename.xml 该文件名将作为资源ID

3.资源引用方式

Java中引用方式: R.drawable.filename
XML中引用方式: @[package:]drawable.filename

4.语法

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>

5.元素

  • <animation-list>
    必须有,根标签,包括一个或者多个标签。
    属性
    • android:oneshot
      Boolean。
      oneshot为true,动画执行一次;oneshot为false,动画循环执行。
  • <item>
    一帧动画,标签的子标签。
    属性
    • android:drawable drawable资源。
    • android:duration Integer. 展示这一帧的时间,单位毫秒。

6.举例

文件位置:res/drawable/rocket.xml:

1
2
3
4
5
6
7
<?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/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

Java代码会将动画设置为视图的背景,然后播放动画:

1
2
3
4
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();