逐帧动画是在时间帧上逐帧绘制帧内容,由于是一帧一帧的画,所以逐帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容。因为使用简便,所以开发中也经常使用
逐帧动画和其它动画一样有两种设置方式
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/refresh_1" android:duration="100" />
<item android:drawable="@drawable/refresh_2" android:duration="100" />
<item android:drawable="@drawable/refresh_3" android:duration="100" />
<item android:drawable="@drawable/refresh_4" android:duration="100" />
<item android:drawable="@drawable/refresh_5" android:duration="100" />
<item android:drawable="@drawable/refresh_6" android:duration="100" />
<item android:drawable="@drawable/refresh_7" android:duration="100" />
<item android:drawable="@drawable/refresh_8" android:duration="100" />
<item android:drawable="@drawable/refresh_9" android:duration="100" />
<item android:drawable="@drawable/refresh_10" android:duration="100" />
<item android:drawable="@drawable/refresh_11" android:duration="100" />
<item android:drawable="@drawable/refresh_12" android:duration="100" />
<item android:drawable="@drawable/refresh_13" android:duration="100" />
<item android:drawable="@drawable/refresh_14" android:duration="100" />
<item android:drawable="@drawable/refresh_15" android:duration="100" />
<item android:drawable="@drawable/refresh_16" android:duration="100" />
<item android:drawable="@drawable/refresh_17" android:duration="100" />
<item android:drawable="@drawable/refresh_18" android:duration="100" />
</animation-list>
首先xml中写明每帧动画的播放顺序和持续时间,oneshot属性设置为true为播放一次,false为循环播放
或者在java中设置
animationDrawable = new AnimationDrawable();
for (int i = 1; i <= 18; i++) {
int drawableID = getResources().getIdentifier("refresh_" + i, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(drawableID);
animationDrawable.addFrame(drawable, 100);
}
从xml加载并设置到ImageView上
imageView.setImageResource(R.drawable.frame_animation);
animationDrawable=(AnimationDrawable)imageView.getDrawable();
设置animationDrawable对象到ImageView上
imageView.setImageDrawable(animationDrawable);
开始动画
animationDrawable.start();
停止动画
animationDrawable.stop();