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();
以上就是创建帧动画的两种方法以及步骤,在后面的文章中还会对属性动画进行介绍。
帧动画就是一帧一帧播放的动画,更电影每隔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();
以上就是创建帧动画的两种方法以及步骤,在后面的文章中还会对属性动画进行介绍。
相关文章推荐
- Android调用.NET webservice实现用户登录验证
- android HandlerThread源码解析
- Android
- android 图片缓存
- android 内存优化方式整理
- android emulator使用
- 【Android】《第一行代码—Android》第一章总结
- Android中的补间动画
- android获取网卡的物理地址
- android studio启动快速调试
- 下载android 源码
- Android 多种方式正确的加载图像,有效避免oom
- Android优化(一)——布局优化
- Android设计中的.9.png
- android怎样调用@hide和internal API
- 喝酒不骑马的Android自学日记(3)
- Android系统定制开机动画
- android studio在真机调试时offline解决办法
- android stdio安装记录
- android 解码出来的视频frame数据,是如何一步步的传递到显示端的(使用 GPU offline 合成)