一、在xml文件中设置帧动画
1、首先得在drawable资源文件夹下创建一个animation_list文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/p1"
android:duration="500" />
<item
android:drawable="@drawable/p2"
android:duration="500" />
<item
android:drawable="@drawable/p3"
android:duration="500" />
<item
android:drawable="@drawable/p4"
android:duration="500" />
<item
android:drawable="@drawable/p5"
android:duration="500" />
</animation-list>
2、通过java代码启动将动画加载到空间中,并启动
ImageView imageView=(ImageView)findViewById(R.id.id_iv); imageView.setImageResource(R.drawable.anim_list); AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable(); animationDrawable.setOneShot(false);//是否只执行一次 animationDrawable.start();//开启动画 //animationDrawable.stop();//停止动画
二、在java代码中设置帧动画
ImageView imageView= (ImageView)findViewById(R.id.id_iv); animationDrawable= new AnimationDrawable(); animationDrawable.addFrame(getResources().getDrawable(R.drawable.p1), 500); animationDrawable.addFrame(getResources().getDrawable(R.drawable.p2), 500); imageView.setImageDrawable(animationDrawalbe); animationDrawable.setOneShot(false);//是否只执行一次 animationDrawable.start();//开启动画 //animationDrawable.stop();//停止动画
原文:http://www.cnblogs.com/zhisuoyu/p/5240546.html