[转载]Android动画之Frame动画实战 – 老牛啊 – 博客园.
Android动画分为Tween动画和Frame动画,Tween动画主要包括图片的放大缩小、旋转、透明度变化、移动等等操作;Frame动画则简单得多了,就是把一张张的图片连续播放产生动画效果。
本节主要介绍一下Frame动画,Tween动画会在后面的文章中介绍,敬请关注。
Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame 动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根 元素和若干个<item>子元素。
实现一个人跳舞的Frame动画,6张图片如下所示:
1、把这6张图片放到res/drawable目录下,分别取名为:p01.png,p02.png,p03.png,p04.png,p05.png,p06.png。
2、在res/anim目录下创建一个XML配置文件,文件名为:dance.xml,文件内容:
<? xml version = "1.0" encoding = "utf-8" ?> < item apk:drawable = "@drawable/p01" apk:duration = "500" /> < item apk:drawable = "@drawable/p02" apk:duration = "500" /> < item apk:drawable = "@drawable/p03" apk:duration = "500" /> < item apk:drawable = "@drawable/p04" apk:duration = "500" /> < item apk:drawable = "@drawable/p05" apk:duration = "500" /> < item apk:drawable = "@drawable/p06" apk:duration = "500" /> </ animation-list > |
apk:oneshot指示是否只运行一次,设置为false则意味着循环播放。
3、在res/layout目录下创建layout配置文件dance.xml,文件内容:
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:apk = "http://schemas.android.com/apk/res/android" apk:orientation = "vertical" apk:layout_width = "fill_parent" apk:layout_height = "fill_parent" > <!-- Frame动画图片 --> < ImageView apk:id = "@+id/ImgDance" apk:layout_width = "wrap_content" apk:layout_height = "wrap_content" apk:background = "@anim/dance" /> <!-- 动画控制按钮 --> < LinearLayout apk:layout_width = "fill_parent" apk:layout_height = "wrap_content" apk:orientation = "horizontal" > < Button apk:text = "开始" apk:layout_width = "wrap_content" apk:layout_height = "wrap_content" apk:onClick = "onStartDance" /> < Button apk:text = "结束" apk:layout_width = "wrap_content" apk:layout_height = "wrap_content" apk:onClick = "onStopDance" /> </ LinearLayout > </ LinearLayout > |
apk:background使用上面的动画作为背景,意味着要取得动画,只要取得该View的背景即可,当然可以在代码中通过设置背景的方式指定;
apk:onClick指示按钮的动作,当然可以在代码中通过实现OnClickListener的方式实现。
4、Activity代码:
/** * Copyright (c) 2004-2011 All Rights Reserved. */ package com.aboy.android.study.animation; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import com.aboy.android.study.R; /** * Frame动画 * * @author obullxl@gmail.com * @version $Id: FrameActivity.java, v 0.1 2011-6-10 下午12:49:46 oldbulla Exp $ */ public class FrameActivity extends Activity { public static final String TAG = "FrameActivity" ; // 显示动画的组件 private ImageView imgDance; // Frame动画 private AnimationDrawable animDance; /** * @see android.app.Activity#onCreate(android.os.Bundle) */ public void onCreate(Bundle cycle) { super .onCreate(cycle); super .setContentView(R.layout.dance); // 实例化组件 this .imgDance = (ImageView) super .findViewById(R.id.ImgDance); // 获得背景(6个图片形成的动画) this .animDance = (AnimationDrawable) this .imgDance.getBackground(); } /** * 按钮:开始‘跳舞’动画 */ public void onStartDance(View view) { this .animDance.start(); } /** * 按钮:停止‘跳舞’动画 */ public void onStopDance(View view) { this .animDance.stop(); } } |
代码就那么的几行,运行之后,点击“开始”按钮后,动画一直不停的播放,直到点击“停止”为止。