Frame Animation 表示帧动画,是顺序播放事先做好的图像,跟电影类似,Android SDK提供了另外一个类AnimationDrawable来定义使用Frame Animation。
下面我们就来看看具体怎么使用帧动画吧。
首先在drawable目录下新建一个frame.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/a"
android:duration="2000">
</item>
<item
android:drawable="@drawable/b"
android:duration="2000">
</item>
<item
android:drawable="@drawable/c"
android:duration="2000">
</item>
</animation-list>
上面xml文件中设置了动画变化期间各个图片和持续的时间
下面来看java代码文件怎么实现这个动画:
public void frameAnimation() throws FileNotFoundException {
TextView mTextView = (TextView) findViewById(R.id.tv);
mTextView.setBackgroundResource(R.drawable.frame);//把xml中设置好的帧动画资源设置给控件
AnimationDrawable animDrawable = (AnimationDrawable) mTextView
.getBackground();//得到该控件的背景Drawable对象
Bitmap bitmap = BitmapFactory
.decodeStream(new FileInputStream(Environment
.getExternalStorageDirectory() + "/Download/aaa.jpg"));//在代码中动态添加背景图,也可以从服务器获取到的输入流动态添加
animDrawable.addFrame(new BitmapDrawable(null, bitmap), 2000);//设置持续时间
animDrawable.setAlpha(180);//设置图片的透明度
animDrawable.setOneShot(false);//设置是否只运行一次,设置为true为循环运行
animDrawable.start();
}
这样注释上也说清楚了,可以通过xml文件添加图片也可以通过代码来动态添加。
贴个运行图吧:
最后总结一下使用FrameAnimation的具体步骤:
1、在xml文件中定义好需要显示的图片
2、在java文件中得到该控件的背景AnimationDrawable对象
3、调用AnimationDrawable对象的start()方法来开启这个帧动画