Animation-list实现逐帧动画

jopen 11年前

我们经常有些需求需要用到轮播的逐帧动画,可以用线程实现,当然android系统为我们提供了更简约简单的实现手段——Animation-list。

首先下面是一段XML,你可以新建一个XML存放在项目的res/drawable目录下面。

<?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/charging_01" android:duration="500"></item>      <item android:drawable="@drawable/charging_02" android:duration="500"></item>      <item android:drawable="@drawable/charging_03" android:duration="500"></item>          </animation-list>
其中android:oneshot="false" 这个是表示是否只展示一次,设为false就表示会循环播放,android:duration="500"这个是表示当前图片停留的时间,单位为毫秒。

第二步:写一个布局文件,在这里我就不写了,在一个控件里面,比如ImageView,设置它的背景图片为上面新建好的那个动画xml即可。

第三步:开启动画,下面的代码中mChargingLogo是我写的ImageView控件,通过ImageView.getBackgroud获得ImageView的背景动画,然后通过AnimationDrawable 启动动画即可,停止动画可使用mAnimationDrawable.stop();

AnimationDrawable mAnimationDrawable = (AnimationDrawable) mChargingLogo.getBackground();
mAnimationDrawable.start();