Android的图形与图像处理之三 逐帧动画(Frame)

定义逐帧动画非常简单,只要在<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 >
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"
/>
       
   
</ 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();
}
});
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值