您的位置:首页 > 移动开发 > Android开发

Android中的帧动画

2015-12-11 23:37 429 查看
继上一篇补间动画之后,这里主要对帧动画进行简单介绍,以及如何在代码中来使用。

帧动画就是一帧一帧播放的动画,更电影每隔24ms播放一张图片一个道理。对于帧动画同样可以通过代码来创建,也可以通过xml来创建

一、代码创建帧动画

public class MainActivity extends Activity {
private ImageView iv;
private RotateAnimation ra;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

iv = (ImageView) findViewById(R.id.ivGirl);
AnimationDrawable ad=new AnimationDrawable();
//获取id
int id=getResources().getIdentifier("a1", "drawable", getPackageName());
int id2=getResources().getIdentifier("a2", "drawable", getPackageName());
int id3=getResources().getIdentifier("girl", "drawable", getPackageName());

//获取Drawable
Drawable drawable = getResources().getDrawable(id);
Drawable drawable2 = getResources().getDrawable(id2);
Drawable drawable3 = getResources().getDrawable(id3);
//添加到ad
ad.addFrame(drawable, 500);
ad.addFrame(drawable2, 500);
ad.addFrame(drawable3, 500);
ad.setOneShot(false);
iv.setBackground(ad);
ad.start();
}
二、通过在xml文件中把图片先归集在一起,然后通过代码来启动

1、在drawable文件夹下新建一个如下图所示的animation-list文件,把需要的图片都通过这个文件关联起来。

<?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/a1" android:duration="500"/>
<item android:drawable="@drawable/a2" android:duration="500"/>
<item android:drawable="@drawable/girl" android:duration="500"/>
</animation-list>
2、在Activity的onCreate方法中,只需要简单两行代码就可以启动动画了
<span style="white-space:pre"> </span>iv.setBackgroundResource(R.drawable.a);
AnimationDrawable bg = (AnimationDrawable) iv.getBackground();
bg.start();

以上就是创建帧动画的两种方法以及步骤,在后面的文章中还会对属性动画进行介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: