一、动画基本类型:
如下表所示,Android的动画由四种类型组成,即可在xml中定义,也可在代码中定义,如下所示:
|
XML |
CODE |
渐变透明度动画效果 |
alpha |
AlphaAnimation |
渐变尺寸伸缩动画效果 |
scale |
ScaleAnimation |
画面转换位置移动动画效果 |
translate |
TranslateAnimation |
画面转移旋转动画效果 |
rotate |
RotateAnimation |
二、如何在XML文件中定义动画
1.alpha
2.scale
3.translate
4.rotate
三、如何使用XML中定义的动画
public static Animation loadAnimation (Contextcontext, int id)
//第一个参数Context为程序的上下文
//第二个参数id为动画XML文件的引用
//例子:
myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
四、如何在java代码中定义动画
1.代码:
2. 分析:
五、如何使用java代码中的动画效果
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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"?> <animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false"> <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代码:
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动画 */ 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(); } } |
代码就那么的几行,运行之后,点击“开始”按钮后,动画一直不停的播放,直到点击“停止”为止。
原文:http://www.cnblogs.com/airry66/p/3972657.html