第一种,应用动画文件到背景:
文件可以放到drawable文件夹,也可以放到anim文件夹
<?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/images_3_0001"
android:duration="500" />
<item
android:drawable="@drawable/images_3_0002"
android:duration="500" />
<item
android:drawable="@drawable/images_3_0003"
android:duration="500" />
<item
android:drawable="@drawable/images_3_0004"
android:duration="500" />
<item
android:drawable="@drawable/images_3_0005"
android:duration="500" />
<item
android:drawable="@drawable/images_3_0006"
android:duration="500" />
</animation-list>
其中用到了六幅图片。
布局就是一个ImageView。
代码:
/**
* 方案1:xml文件,background
*/
private void scheme1() {
imgAnimationContainer.setBackgroundResource(R.drawable.frame_animation_1);
AnimationDrawable anim = (AnimationDrawable) imgAnimationContainer.getBackground();
anim.start();
}
第二种,应用动画文件到drawable
代码:
/**
* 方案2:xml文件,drawable
*/
private void scheme2() {
imgAnimationContainer.setImageResource(R.drawable.frame_animation_1);
AnimationDrawable anim = (AnimationDrawable) imgAnimationContainer.getDrawable();
anim.start();
}
第三种,纯代码实现
/**
* 方案3:纯代码
*/
private void scheme3() {
int[] imagesRes = {R.drawable.images_3_0001,
R.drawable.images_3_0002,
R.drawable.images_3_0003,
R.drawable.images_3_0004,
R.drawable.images_3_0005,
R.drawable.images_3_0006
};
AnimationDrawable animation = new AnimationDrawable();
for (int i = 0; i < 6; i++) {
Drawable drawable = getResources().getDrawable(imagesRes[i]);
animation.addFrame(drawable, 1000);
}
animation.setOneShot(false);//不断循环
imgAnimationContainer.setImageDrawable(animation);
animation.start();
}