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

Android学习笔记14-Tween Animation动画的实现

2013-07-05 10:08 302 查看
在Android中,有两种动画模式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)来产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。

1.通过Java代码实现Tween Animation

  Tween Animation动画效果是通过Animation类来实现的。Animation类有五个直接子类,分别为AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation和AnimationSet。其中,AlphaAnimation用来实现透明度渐变动画效果;ScaleAnimation用来实现尺寸伸缩渐变动画效果;TranslateAnimation用来实现画面转换位置移动动画效果;RotateAnimation用来实现画面转移旋转动画效果;AnimationSet则用于对多个动画进行组合。

1.1AlphaAnimation类的常用方法

  AlphaAnimation类的常用方法如下:

  AlphaAnimation(float fromAlpha, float toAlpha);

  其中,参数fromAlpha表示动画起始时透明度;参数toAlpha表示动画结束时透明度(0.0表示完全透明,1.0表示完全不透明)

  willChangeBounds();//动画是否影响指定的视图范围

  willChangeTransformationMatrix();//动画是否影响转换矩阵

1.2ScaleAnimation类的常用方法

  ScaleAnimation类的常用方法如下:

  ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

  其中,参数fromX、toX分别表示起始和结束时x坐标上的伸缩尺寸;参数fromY、toY分别表示起始和结束时y坐标上的伸缩尺寸;参数pivotXType、pivotYType分别表示x、y的伸缩模式;参数pivotXValue、pivotYValue分别表示伸缩动画相对于x、y的坐标的开始位置。

1.3TranslateAnimation类的常用方法

  TranslateAnimation类的常用方法如下:

  TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);

  其中,参数fromXDelta、fromYDelta分别表示起始时x、y坐标;参数toXDelta、toYDelta分别表示结束时x、y坐标。

1.4RotateAnimation类的常用方法

  RotateAnimation类的常用方法如下:

  RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

  其中,参数fromDegrees表示开始时的角度;参数toDegrees表示结束时的角度;参数pivotXType、pivotYType分别表示x、y的伸缩模式;参数pivotXValue、pivotYValue分别表示伸缩动画相对于x、y的坐标的开始位置。

1.5AnimationSet类的常用方法

  AnimationSet类的常用方法如下:

  AddAnimation(Animation a);//添加一个动画到动画组件中

  computeDurationHint();//动画组件的最大持续时间

  getAnimation();//获取动画

  getDuration();//获取动画组件的持续时间

  getStartTime();//获取动画开始的时间

  setDuration(long durationMillis);//设置动画持续时间

  setFillAfter(boolean fillAfter);//设置动画转换在动画结束后被应用

  setFillBefore(boolean fillBefore);//设置动画转换在动画开始前被应用

  setStartOffset(long startOffset);//设置动画之间的时间间隔

  etRepeatMode(int repeatMode);//设置动画的重复模式

2.通过xml布局文件实现Tween Animation

  使用xml布局文件可以更简单的实现Tween Animation动画效果。

  Animation的xml布局文件存放在工程的res/anim目录下。

  在xml布局文件中必须包含根元素<set>。节点<alpha>、<scale>、<translate>和<rotate>分别对应AlphaAnimation、ScaleAnimation、TranslateAnimation和RotateAnimation四种动画效果。

2.1Tween Animation的共同节点属性

  Tween Animation的共同节点属性有以下一些:

  android:duration[long]//动画的持续时间,单位为ms

  android:fillAfter[boolean]//设置为true时,动画转换在动画结束后被应用

  android:fillBefore[boolean]//设置为true时,动画转换在动画开始前被应用

  android:interpolator//设置动画的插入器,可选值有accelerate_decelerate_interpolator加速减速动画插入器,accelerate_interpolator加速动画插入器,decelerate_interpolator减速动画插入器

  android:repeatCount[int]//动画的重复次数

  android:repeatMode[int]//动画的重复模式,1表示重头开始重新播放,2表示从后往前重新播放

  android:startOffset[long]//设置动画之间的时间间隔

  android:zAdjustment[int]//

2.2节点<alpha>的常用属性

  节点<alpha>的常用属性如下:

  android:fromAlpha[float]表示起始时透明度

  android:toAlpha[float]表示结束时透明度

  取值说明:0.0表示完全透明,1.0表示完全不透明

2.3节点<scale>的常用属性

  节点<scale>的常用属性如下:

  android:fromXScale[float]表示动画起始时x坐标上的伸缩尺寸

  android:toXScale[float]表示动画结束时x坐标上的伸缩尺寸

  android:fromYScale[float]表示动画起始时y坐标上的伸缩尺寸

  android:toYScale[float]表示动画结束时y坐标上的伸缩尺寸

  取值说明:0.0表示收缩到没有,1.0表示正常无收缩,值大于1.0表示放大,值小于1.0表示收缩

  android:pivotX[float]表示动画相对于物件的x坐标的开始位置

  android:pivotY[float]表示动画相对于物件的y坐标的开始位置

2.4节点<translate>的常用属性

  节点<translate>的常用属性如下:

  android:fromXDelta[int]表示动画起始时x坐标上的位置

  android:toXDelta[int]表示动画结束时x坐标上的位置

  android:fromYDelta[int]表示动画起始时y坐标上的位置

  android:toYDelta[int]表示动画结束时y坐标上的位置

2.5节点<translate>的常用属性

  节点<rotate>的常用属性如下:

  android:fromDegrees表示动画起始时物件的角度

  android:toDegrees表示动画结束时物件的角度

  取值说明:负数from—正数to表示顺时针旋转,负数from—负数to表示逆时针旋转,正数from—正数to表示顺时针旋转,正数from—负数to表示逆时针旋转

  android:pivotX表示动画相对于物件的x坐标的开始位置

  android:pivotY表示动画相对于物件的y坐标的开始位置

3.实例

  在本实例中,分别使用Java代码和xml布局文件实现了Tween Animation动画效果。通过使用上下左右键可以分别实现AlphaAnimation、ScaleAnimation、TranslateAnimation和RotateAnimation四种动画效果。

  TweenAnimation.java源代码如下:

TweenAnimation.java源代码

TweenAnimation.java源代码
package com.example.android_tweenanimation;

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.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class TweenAnimation extends View {

private Animation mAnimationAlpha = null;                        //Alpha动画
private Animation mAnimationScale = null;                         //Scale动画
private Animation mAnimationTranslate = null;                //Translate动画
private Animation mAnimationRotate = null;                      //Rotate动画

Context mContext = null;
Bitmap mBitmap_fuwa = null;                      //Bitmap对象

public TweenAnimation(Context context) {
super(context);
mContext = context;
mBitmap_fuwa = ((BitmapDrawable) getResources().getDrawable
(R.drawable.fuwa)).getBitmap();                  //加载Bitmap对象
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap_fuwa, 0, 0, null);
}

//按键按下事件
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:                              //上键:Alpha动画效果
mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
mAnimationAlpha.setDuration(3000);
//            mAnimationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.alpha_animation);
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(3000);
//            mAnimationScale = AnimationUtils.loadAnimation(mContext, R.anim.scale_animation);
this.startAnimation(mAnimationScale);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:                         //左键:Translate动画效果
mAnimationTranslate = new TranslateAnimation(10, 100, 10, 100);
mAnimationTranslate.setDuration(3000);
//            mAnimationTranslate = AnimationUtils.loadAnimation(mContext, R.anim.translate_animation);
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(3000);
//            mAnimationRotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_animation);
this.startAnimation(mAnimationRotate);
break;
}
return true;
}

//按键弹起事件
public boolean onKeyUp(int keyCode, KeyEvent event) {
return false;
}
}


  alpha_animation.xml源代码如下:

alpha_animation.xml源代码

alpha_animation.xml源代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/>
</set>


  scale_animation.xml源代码如下:

scale_animation.xml源代码

scale_animation.xml源代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="500"
/>
</set>


  translate_animation.xml源代码如下:

translate_animation.xml源代码

translate_animation.xml源代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100"
android:duration="3000"
/>
</set>


  rotate_animation.xml源代码如下:

rotate_animation.xml源代码

rotate_animation.xml源代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
</set>


4.实例(补充)

  在本实例中,首先在MainActivity.java中定义了四个Animation对象:mAnimation_alpha、mAnimation_scale、mAnimation_translate和mAnimation_rotate,分别表示透明度渐变动画对象、尺寸伸缩渐变动画对象、位置移动动画对象和画面旋转动画对象。并定义了四个Button对象,当点击这四个按钮时,可以分别运行上述四种不同的动画效果。该实例运行界面如图1所示。


图1 效果图

  MainActivity.java源代码如下:

MainActivity.java源代码
 

MainActivity.java源代码
package com.example.android_tweenanimation;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;

public class MainActivity extends Activity {

Animation mAnimation_alpha;          //透明度渐变动画对象
Animation mAnimation_scale;           //尺寸伸缩渐变动画对象
Animation mAnimation_translate;  //位置移动动画对象
Animation mAnimation_rotate;        //画面旋转动画对象

ImageView mImageView;                    //图像视图对象

Button mButton_alpha;                         //透明度渐变按钮
Button mButton_scale;                          //尺寸伸缩渐变按钮
Button mButton_translate;                 //位置移动按钮
Button mButton_rotate;                       //画面旋转按钮

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//加载动画效果
mAnimation_alpha = AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
mAnimation_scale = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
mAnimation_translate = AnimationUtils.loadAnimation(this, R.anim.translate_animation);
mAnimation_rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);

//加载布局
mImageView =(ImageView)this.findViewById(R.id.imageview);
mButton_alpha = (Button)this.findViewById(R.id.button_alpha);
mButton_scale = (Button)this.findViewById(R.id.button_scale);
mButton_translate = (Button)this.findViewById(R.id.button_translate);
mButton_rotate = (Button)this.findViewById(R.id.button_rotate);

//透明度渐变按钮事件监听
mButton_alpha.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mImageView.startAnimation(mAnimation_alpha);
}
});

//尺寸伸缩渐变按钮事件监听
mButton_scale.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mImageView.startAnimation(mAnimation_scale);
}
});

//位置移动按钮事件监听
mButton_translate.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mImageView.startAnimation(mAnimation_translate);
}
});

//画面旋转按钮事件监听
mButton_rotate.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mImageView.startAnimation(mAnimation_rotate);
}
});
}
}


 对应的activity_main.xml源代码如下:

activity_main.xml源代码

activity_main.xml源代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/imageview"
android:layout_marginLeft="25dp"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fuwa"
android:contentDescription="fuwa"    />

<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"    >

<Button
android:id="@+id/button_alpha"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="透明度渐变"    />

<Button
android:id="@+id/button_scale"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="尺寸伸缩渐变"    />

</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"    >

<Button
android:id="@+id/button_translate"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="画面位置移动"    />

<Button
android:id="@+id/button_rotate"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="画面旋转"    />

</LinearLayout>

</LinearLayout>


相关资料:

Android Animation学习笔记 /article/4716173.html

分析Android动画模块 http://www.linuxgraphics.cn/animation/android_tween_animation.html

Android中AnimationSet的使用 http://blog.csdn.net/yuzhiboyi/article/details/7731826
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: