创建android逐帧动画的两种方式 布局和java代码

创建android 逐帧动画可以用下面两种方式来进行,

1:在res下创建文件夹anim,里面创建xml文件

<?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/fat_po_f01" android:duration="60" />
<item android:drawable="@drawable/fat_po_f02" android:duration="60" />
<item android:drawable="@drawable/fat_po_f03" android:duration="60" />
<item android:drawable="@drawable/fat_po_f04" android:duration="60" />
<item android:drawable="@drawable/fat_po_f05" android:duration="60" />
<item android:drawable="@drawable/fat_po_f06" android:duration="60" />
<item android:drawable="@drawable/fat_po_f07" android:duration="60" />
<item android:drawable="@drawable/fat_po_f08" android:duration="60" />
<item android:drawable="@drawable/fat_po_f09" android:duration="60" />
<item android:drawable="@drawable/fat_po_f10" android:duration="60" />
<item android:drawable="@drawable/fat_po_f11" android:duration="60" />
<item android:drawable="@drawable/fat_po_f12" android:duration="60" />
<item android:drawable="@drawable/fat_po_f13" android:duration="60" />
<item android:drawable="@drawable/fat_po_f14" android:duration="60" />
<item android:drawable="@drawable/fat_po_f15" android:duration="60" />
<item android:drawable="@drawable/fat_po_f16" android:duration="60" />
<item android:drawable="@drawable/fat_po_f17" android:duration="60" />
<item android:drawable="@drawable/fat_po_f18" android:duration="60" />
<item android:drawable="@drawable/fat_po_f19" android:duration="60" />
<item android:drawable="@drawable/fat_po_f20" android:duration="60" />
<item android:drawable="@drawable/fat_po_f21" android:duration="60" />
<item android:drawable="@drawable/fat_po_f22" android:duration="60" />
<item android:drawable="@drawable/fat_po_f23" android:duration="60" />
<item android:drawable="@drawable/fat_po_f24" android:duration="60" />
<item android:drawable="@drawable/fat_po_f25" android:duration="60" />
<item android:drawable="@drawable/fat_po_f26" android:duration="60" />
<item android:drawable="@drawable/fat_po_f27" android:duration="60" />
</animation-list>

然后在布局里添加imageview

再在activitiy里面实现

ImageView imageView = (ImageView) findViewById(R.id.anim);

imageView。setBackgroundResource(R.anim.XX);
// 获取AnimationDrawable动画对象
final AnimationDrawable anim = (AnimationDrawable) imageView
.getBackground();

2:创建animationdrawable对象,然后调用addframe(Drawable frame , int duration)向动画里面添加帧。

AnimationDrawable drawable = new AnimationDrawable();
Drawable x1 = getResources().getDrawable(R.drawable.fat_po_f01);
drawable.addFrame(x1, 300);

你可能感兴趣的:(android)