使用XML文件的格式来保存每一帧显示的图片和时间
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:oneshot="false"> 4 <!-- 5 将每一帧都保存在 animation-list中 6 oneshot:属性定义是否只运行一次,ture是运行一次 7 每一个帧就是一个item,用drawable来设置图片,duration是持续时间 8 --> 9 <item android:drawable="@drawable/girl_1" android:duration="80" /> 10 <item android:drawable="@drawable/girl_2" android:duration="80" /> 11 <item android:drawable="@drawable/girl_3" android:duration="80" /> 12 <item android:drawable="@drawable/girl_4" android:duration="80" /> 13 <item android:drawable="@drawable/girl_5" android:duration="80" /> 14 <item android:drawable="@drawable/girl_6" android:duration="80" /> 15 <item android:drawable="@drawable/girl_7" android:duration="80" /> 16 <item android:drawable="@drawable/girl_8" android:duration="80" /> 17 <item android:drawable="@drawable/girl_9" android:duration="80" /> 18 <item android:drawable="@drawable/girl_10" android:duration="80" /> 19 <item android:drawable="@drawable/girl_11" android:duration="80" /> 20 </animation-list>
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 tools:context=".MainActivity"> 5 <ImageView 6 android:id ="@+id/frame_img" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" /> 9 </RelativeLayout>
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 ImageView img = (ImageView) findViewById(R.id.frame_img); 8 img.bringToFront();// 置顶:不会被其他组件遮挡 9 //设置图片的背景,是保存了每一帧动画的文件夹 10 //也可以在xml文件中来指定,这两种方法选择一个就可以了 11 img.setBackgroundResource(R.drawable.frame); 12 //通过AnimationDrawable 类来将获取到的资源进行播放 13 AnimationDrawable animationDrawable = (AnimationDrawable) img.getBackground(); 14 animationDrawable.start(); 15 } 16 }
使用java代码来实现逐帧动画
1 public class MainActivity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 ImageView img = (ImageView) findViewById(R.id.frame_img); 8 img.bringToFront(); 9 AnimationDrawable animation = new AnimationDrawable(); 10 //这里有的参数是 Drawable frame,int duration 第一个参数是每一帧的动画文件 11 //第一反应是使用R.drawable.xxx来获取但是不行,原因是直接使用R.drawable.xx返回的是int类型 12 //这里的参数是Drawable类型所以用activity.getResouce()来获取 同意都是对资源文件的操作 13 animation.addFrame(getResources().getDrawable(R.drawable.progress_1),50); 14 animation.addFrame(getResources().getDrawable(R.drawable.progress_2),50); 15 animation.addFrame(getResources().getDrawable(R.drawable.progress_3),50); 16 animation.addFrame(getResources().getDrawable(R.drawable.progress_4),50); 17 animation.addFrame(getResources().getDrawable(R.drawable.progress_5),50); 18 animation.addFrame(getResources().getDrawable(R.drawable.progress_6),50); 19 animation.addFrame(getResources().getDrawable(R.drawable.progress_7),50); 20 animation.addFrame(getResources().getDrawable(R.drawable.progress_8),50); 21 //是否只播放一次 22 animation.setOneShot(false); 23 img.setBackground(animation); 24 //img.setBackgroundDrawable(animation);这个方法已经过去了,可是直接使用setBackground 25 animation.start(); 26 } 27 }
逐帧动画 FrameAnimation
逐帧动画是将事先准备好的一组图像,按顺序播放出来。和补间动画不同的是逐帧动画需要提供每一帧的动画。好处是他改变动画的内容来播放动画而不是改变控件本身物理状态实现动画效果。
操作步骤:
原文:http://www.cnblogs.com/androidLearn/p/5215156.html