---
title: "Android开发之动画实现"
author: "mmdev"
date: "2011-12-09T18:09:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2012-07-24T13:06:58Z"
id: 1605285
url: "https://mmdev.iteye.com/blog/1605285"
---

# Android开发之动画实现

/*

\* Android开发之动画实现

\* 北京Android俱乐部群：167839253

\* Created on: 2011-12-09

\* Author: blueeagle

\* Email: liujiaxiang@gmail.com

\*/

在《Android开发之PopupWindow》这篇文章中，已经初步涉及到了动画的相关内容。对于一个弹出对话框。其动画效果可以利用xml文件进行设置，复习一下，就是对于对话框的飞进飞出，定义两个XML文件。例如：

飞入动画xml:

```
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       android:fromYDelta="-100"
       android:toYDelta="0"
       android:duration="1000"
       android:fillEnabled="true"
       android:fillAfter="true"
       />
        <scale android:fromXScale="0.6" android:toXScale="1.0"
                android:fromYScale="0.6" android:toYScale="1.4" android:pivotX="50%"
                android:pivotY="50%" android:duration="2000" 
                android:fillAfter="false"/>
        <alpha android:interpolator="@android:anim/decelerate_interpolator"
                android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />            
        <rotate
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0"
        android:toDegrees="+359"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000">
        </rotate>
</set>

```

anin.xml

飞出动画:

anout.xml

```
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true"
    >
    <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       android:fromYDelta="0"
        android:toYDelta="-100"
       android:duration="1000"
       android:fillEnabled="true"
       android:fillAfter="true"
       />
               <scale android:fromXScale="1.0" android:toXScale="0.4"
                android:fromYScale="1.0" android:toYScale="0.4" android:pivotX="50%"
                android:pivotY="50%" android:duration="2000" />
    <alpha android:interpolator="@android:anim/decelerate_interpolator"
       android:fromAlpha="1.0" 
       android:toAlpha="0.0" 
       android:duration="2000" 
       />
</set>

```

总结一下上面动画的特点：

1\. 透明度渐变

2\. 尺寸伸缩

3\. 画面旋转

4\. 位置移动

那么这类动画，可以在xml中体现，当然也可以在代码中的体现。在代码中对于上述四种动画形式可以总结成：

1\. 透明度渐变

AlphaAnimation(float fromAlpha,float toAlpha);

这里的参数跟xml文件中的参数为对应的，（后面的也均为对应）功能跟xml文件中的保持一致。

fromAlpha:表示动画起始时的透明度；

toAlpha:表示动画结束时的透明度；

0.0表示完全透明，1.0表示完全不透明。

2\. 尺寸伸缩

ScaleAnimation(float fromXScale,float toXScale,float fromYScale,float toYScale,int pivotXType,float pivotX,int pivotYType,float pivotY)

fromXScale,toXScale:起始和结束时的X坐标的伸缩；

fromYScale,toYScale:起始和结束时的Y坐标的伸缩；

pivotX:表示伸缩动画相对于X坐标的开始位置；

pivotY:表示伸缩动画相对于Y坐标的开始位置；

pivotXType:X坐标上的伸缩模式；

pivotYType:Y坐标上的伸缩模式。

3\. 画面旋转

RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotX,int pivotYType,float pivotY)

fromDegrees,toDegrees:起始和结束时的角度；

fromYScale,toYScale:起始和结束时的Y坐标的伸缩；

pivotX:表示伸缩动画相对于X坐标的开始位置；

pivotY:表示伸缩动画相对于Y坐标的开始位置；

pivotXType:X坐标上的伸缩模式；

pivotYType:Y坐标上的伸缩模式。

4\. 位置移动

TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float toYDelte)

fromXDelta, fromYDelta:起始时候的坐标；

toXDelta, toYDelte:结束时候的坐标；

当然，在代码中做了这些设置后，是需要将动画播放出来的，那么播放动画的函数就为：

startAnimation(Animation animation)

animation为要播放的动画。

同时，需要设定一个播放动画的时间，利用：

setDuration(long duration)

duration为动画显示的时间，以毫秒为单位。

以上介绍的是Android平台下的**Tween动画**，Android平台下一共提供了两种动画，另外一种就是Frame动画。

**Frame动画**播放，需要有很多帧的图片。首先还是可以在XML文件中来说明。

framean.xml:

```
<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false">
    <item android:drawable="@drawable/pic1" android:duration="100" />
    <item android:drawable="@drawable/pic2" android:duration="100" />
    <item android:drawable="@drawable/pic3" android:duration="100" />
…
…
…
</animation-list>

```

JAVA代码：

```
public class GameView extends View
{
    // 定义AnimationDrawable动画对象
    private AnimationDrawable   frameAnimation    = null;
    Context                     mContext      = null;
    public GameView(Context context)
    {
       super(context);
       mContext = context;
       //定义一个ImageView用来显示动画
       ImageView myImageView = new ImageView(mContext);
       // 装载动画布局文件 
       myImageView.setBackgroundResource(R.anim.framean);   
       //构建动画
       frameAnimation = (AnimationDrawable)myImageView.getBackground();
       //设置是否循环 
       frameAnimation.setOneShot( false );  
       //设置该类显示的动画
       this.setBackgroundDrawable(frameAnimation);
    }
    public void onDraw(Canvas canvas)
    {
       super.onDraw(canvas);
    }
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {
       switch ( keyCode )
       {
       case KeyEvent.KEYCODE_DPAD_UP:  
           /* 开始播放动画 */
           frameAnimation.start();
           break;
       }
       return true;
    }
}

```

直接用代码，而不用xml描述的方法为：

```
public class GameView extends View {
	// 定义AnimationDrawable动画对象
	private AnimationDrawable	frameAnimation	= null;
	Context						mContext		= null;
	//定义一个Drawable对象
	Drawable myBitAnimation = null;
	public GameView(Context context) {
		super(context);
		//实例化AnimationDrawable对象
		mContext = context;
		frameAnimation = new AnimationDrawable();
		//装载资源，循环装载需要作为动画的图片
		for(int i=1;i<=3;i++)
		{
			int id = getResources().getIdentifier("pic"+i, "drawable", mContext.getPackageName());
			myBitAnimation = getResources().getDrawable(id);
			//为动画添加一帧，参数myBitAnimation是该帧的图片，参数100是显示时间，毫秒为单位
			frameAnimation.addFrame(myBitAnimation, 100);
		}
		//设置是否循环，false表示循环
		frameAnimation.setOneShot(false);
		//设置本类将要显示这个动画
		this.setBackgroundDrawable(frameAnimation);
    public void onDraw(Canvas canvas){
    	super.onDraw(canvas);
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		switch ( keyCode )
		{
		case KeyEvent.KEYCODE_DPAD_UP:	
			//开始播放动画 
			frameAnimation.start();
			break;
		}
		return true;
	}

}
```