Android动画之帧动画(一)

来源:互联网 发布:百合 红楼梦 知乎 编辑:程序博客网 时间:2024/05/09 08:47

定义

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

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

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

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