Android动画之帧动画(一)

定义

在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成动画。

优点:适合表演细腻复杂的动画

缺点:给制作增加了工作量,且文件输出很大。

XML实现方式

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call start() to run the animation.

创建帧动画最简单的方式是在XML文件中定义animation,文件放在res/drawable/文件夹下(放在自定义的anim中也可以)。将它作为view的背景,并通过start()方法来运行animation。

定义aniamtion文件example_animation.xml文件,在<animation-list></animation-list>元素中定义动画的全部帧,并制定各帧的持续时间。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"  >
    <!-- 定义一个动画帧,Drawable为img0,持续时间50毫秒 -->
    <item android:drawable="@drawable/img0" android:duration="50" />
</animation-list>
android:onshot的属性值为true时表示只播放一次,false表示循环播放。

XML中background没有设置动画

<pre name="code" class="html">ImageView view= (ImageView) findViewById(R.id.view);     
// 通过逐帧动画的资源文件获得AnimationDrawable示例
AnimationDrawable=(AnimationDrawable) getResources().getDrawable(R.drawable.example_animation);
// 把AnimationDrawable设置为ImageView的背景view.setBackgroundDrawable(<span style="font-family: Roboto, sans-serif;">AnimationDrawable</span><span style="font-family: Roboto, sans-serif;">);</span>

 

 

XML中background设置了动画

 final AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
 animationDrawable.start();

运行动画
if (AnimationDrawable!= null && !AnimationDrawable.isRunning()) {
  frameAnim.start();
  Toast.makeText(ToXMLActivity.this, "开始播放", 0).show();
}

使用Java代码实现

通过animationDrawable.addFrame()的方式添加帧动画。
imageView view = (ImageView) findViewById(R.id.example_aniamtion);
AnimationDrawable animationDrawable=new AnimationDrawable();
// 为AnimationDrawable添加动画帧
animationDrawable.addFrame(getResources().getDrawable(R.drawable.img0), 50);
// 设置ImageView的背景
AnimationDrawableview.setBackgroundDrawable(animationDrawable);


 
 
 
 
 
 
 

参考链接:http://www.cnblogs.com/plokmju/p/android_AnimationDrawable.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值