定义逐帧动画非常简单,只要在<animation-list …/>元素中使用<item.../>子元素定义动画的全部帧,并指定各帧的持续时间即可。
android:onshot控制该动画是否循环播放,设为false则会循环播放
也支持在Java代码中创建逐帧动画,调用addFrame(Drawable frame, int duration)向该动画中添加帧
程序获取AnimationDrawable后,接下来就可用ImageView把AnimationDrawable显示出来
示例代码:
res/anim/fat_po.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 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
>
main_activity.xml
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
android:gravity = "center" >
< Button android:id = "@+id/play"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "start"
/>
< Button android:id = "@+id/stop"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "stop"
/>
</ LinearLayout >
< ImageView
android:id = "@+id/anim"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@anim/fat_po"
android:scaleType = "center"
android:layout_gravity = "center_horizontal"
/>
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
android:gravity = "center" >
< Button android:id = "@+id/play"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "start"
/>
< Button android:id = "@+id/stop"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "stop"
/>
</ LinearLayout >
< ImageView
android:id = "@+id/anim"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@anim/fat_po"
android:scaleType = "center"
android:layout_gravity = "center_horizontal"
/>
</
LinearLayout
>
main.java
public
class
MainActivity
extends
Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
// 获取两个按钮
Button play = (Button) findViewById(R.id. play );
Button stop = (Button) findViewById(R.id. stop );
ImageView imageView = (ImageView) findViewById(R.id. anim );
// 获取AnimationDrawable动画对象
final AnimationDrawable anim = (AnimationDrawable) imageView
.getBackground();
play.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
// 开始播放动画
anim.start();
}
});
stop.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
// 停止播放动画
anim.stop();
}
});
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
// 获取两个按钮
Button play = (Button) findViewById(R.id. play );
Button stop = (Button) findViewById(R.id. stop );
ImageView imageView = (ImageView) findViewById(R.id. anim );
// 获取AnimationDrawable动画对象
final AnimationDrawable anim = (AnimationDrawable) imageView
.getBackground();
play.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
// 开始播放动画
anim.start();
}
});
stop.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
// 停止播放动画
anim.stop();
}
});
}
}