Android平台提供了两类动画,一类是Tween动画,即对场景里的对象不断进行图像变换(平移、缩放、旋转)来产生动画效果;另一类是Frame动画,即顺序播放事先做好的图片。
Tween动画在Android中分为4类,它们分别是:AlphaAnimation(透明度动画)、TranslateAnimation(平移动画)、ScaleAnimation(缩放动画)、RotateAnimation(旋转动画)。都继承自android.view.Animation类,它们都是表示从一个状态A向状态B变化的一个过程,所以英文名字叫Tween动画、中文名叫:“补间动画”、“中间动画”。它们总的说来有两种实现方式:java code(java源代码)、xml(xml配置文件),这里先从java code开始
以前就是因为每中Tween动画都有很多构造函数不清楚,现在仔细看了下,记录下来方便以后查看
AlphaAnimation(透明度动画)
AlphaAnimation有两个构造函数,分别是:
—— AlphaAnimation(Context context, AttributeSet attrs):第二个参数是个属性集,之后会详细对AttributeSet 讲解
——AlphaAnimation(float fromAlpha, float toAlpha):第一个参数是初始透明度,第二个参数是终止透明度
TranslateAnimation(平移动画)
TranslateAnimation有三个构造函数,分别是:
——TranslateAnimation(Context context, AttributeSet attrs):略过
——TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta):分别对应x轴的起始、终点 坐标,与y轴的起始、终点坐标
——TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue):第一个参数是x轴方向的值的参照(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT);第二个参数是第一个参数类型的起始值;第三个参数与第四个参数是x轴方向的
终点参照与对应值;后面四个参数就不用解释了。如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数。
以x轴为例介绍参照与对应值的关系
如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位
如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件,
对应值应该理解为相对于自身或者父控件的几倍或百分之多少。一定要多试试这几个参数类型!
ScaleAnimation(缩放动画)
ScaleAnimation(缩放动画)有四个构造函数,分别是:
——ScaleAnimation(Context context, AttributeSet attrs):略过
——ScaleAnimation(float fromX, float toX, float fromY, float toY):同TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
——ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY):这里解释后面两个参数,pivot
英文意思为“枢轴”,也就是支点。通过这两个参数可以控制缩放动画的放大方向,这个点不会随对象大小变化而变化
——ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue):如果理解了前面所讲的,这个就不做多的说明,如果不清楚,请回头多用代码试试。
RotateAnimation(旋转动画)
RotateAnimation(旋转动画)同样有四个构造函数,分别是:
——RotateAnimation(Context context, AttributeSet attrs)
——RotateAnimation(float fromDegrees, float toDegrees)
——RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
——RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Frame动画主要是通过AnimationDrawable类来实现的,它有start()和stop()两个重要的方法来启动和停止动画。Frame动画一般通过XML文件配置,在工程的res/anim目录下创建一个XML配置文件,该配置文件有一个<animation-list>根元素和若干个<item>子元素。
<?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>
示例如下:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
public class GameView extends View
{
/* 定义Alpha动画 */
private Animation mAnimationAlpha = null;
/* 定义Scale动画 */
private Animation mAnimationScale = null;
/* 定义Translate动画 */
private Animation mAnimationTranslate = null;
/* 定义Rotate动画 */
private Animation mAnimationRotate = null;
/* 定义Bitmap对象 */
Bitmap mBitQQ = null;
public GameView(Context context)
{
super(context);
/* 装载资源 */
mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
/* 绘制图片 */
canvas.drawBitmap(mBitQQ, 0, 0, null);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch ( keyCode )
{
case KeyEvent.KEYCODE_DPAD_UP:
/* 创建Alpha动画 */
mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
/* 设置动画的时间 */
mAnimationAlpha.setDuration(3000);
/* 开始播放动画 */
this.startAnimation(mAnimationAlpha);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
/* 创建Scale动画 */
mAnimationScale =new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* 设置动画的时间 */
mAnimationScale.setDuration(500);
/* 开始播放动画 */
this.startAnimation(mAnimationScale);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
/* 创建Translate动画 */
mAnimationTranslate = new TranslateAnimation(10, 100,10, 100);
/* 设置动画的时间 */
mAnimationTranslate.setDuration(1000);
/* 开始播放动画 */
this.startAnimation(mAnimationTranslate);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
/* 创建Rotate动画 */
mAnimationRotate=new RotateAnimation(0.0f, +360.0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* 设置动画的时间 */
mAnimationRotate.setDuration(1000);
/* 开始播放动画 */
this.startAnimation(mAnimationRotate);
break;
}
return true;
}
}