您的位置:首页 > 移动开发 > Android开发

Android动画之帧动画(一)

2015-11-25 14:36 435 查看

定义

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

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

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: