1.帧布局
1.1 在布局文件中进行动画的设置
1.1.1 将图片资源放入mipmap 文件夹中
1.1.2 在drawable文件夹下新建一个frame.xml的文件
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/a2" android:duration="100"/>
<item android:drawable="@mipmap/a3" android:duration="100"/>
<item android:drawable="@mipmap/a4" android:duration="100"/>
</animation-list>
1.1.3 在layout布局中,添加一个ImageView控件
<ImageView
android:id="@+id/frame_iv1"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame"/>
1.1.4 把imageView控件的背景资源获取出来,转换成AnimationDrawable类型
AnimationDrawable anim = (AnimationDrawable) iv1.getBackground();
让动画资源开始的方法
anim.start();
1.2在代码中定义
1.2.1在layout布局中,添加一个ImageView控件
<ImageView
android:id="@+id/frame_iv2"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
/>
1.2.2. 在代码中的实现
AnimationDrawable anim = new AnimationDrawable();
通过resource方法把图片加载到动画对象中
for (int i = 2;i<21;i++){
参数1.图片名,参数2.图片所在的文件夹,3.当前的包名
int id = getResources().getIdentifier("a" + i,"mipmap",getPackageName());
根据id把图片转化成Drawable对象
Drawable drawable = getResources().getDrawable(id);
把当前的图片添加到AnimationDrawable对象中,并设置图片显示时间
anim.addFrame(drawable,100);
}
设置播放模式是否循环播放,false表示循环,true表示不循环
anim.setOneShot(false);
设置将要显示的这个动画
iv2.setBackgroundDrawable(anim);
启动动画
anim.start();