
Android动画资源系列文章:Android动画资源文件总结
帧动画
- 一种用XML定义的动画,按照顺序显示一系列图像(像放映电影一样)。
-
文件位置
res/drawable/filename.xml 该文件名将作为资源ID -
资源引用方式
Java中引用方式: R.drawable.filename
XML中引用方式: @[package:]drawable.filename -
语法
<?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>
-
元素
-
<animation-list>
必须有,根标签,包括一个或者多个<item>标签。
属性- android:oneshot
Boolean。
oneshot为true,动画执行一次;oneshot为false,动画循环执行。
- android:oneshot
-
<item>
一帧动画,<animation-list>标签的子标签。
属性- android:drawable drawable资源。
- android:duration Integer. 展示这一帧的时间,单位毫秒。
-
<animation-list>
-
举例
文件位置:res/drawable/rocket.xml:
<?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代码会将动画设置为视图的背景,然后播放动画:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
网友评论