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

在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源代码

 

 1 TweenAnimation.java源代码
 2  package com.example.android_tweenanimation;
 3  
 4  import android.content.Context;
 5  import android.graphics.Bitmap;
 6  import android.graphics.Canvas;
 7  import android.graphics.drawable.BitmapDrawable;
 8  import android.view.KeyEvent;
 9  import android.view.View;
10  import android.view.animation.AlphaAnimation;
11  import android.view.animation.Animation;
12  import android.view.animation.AnimationUtils;
13  import android.view.animation.RotateAnimation;
14  import android.view.animation.ScaleAnimation;
15  import android.view.animation.TranslateAnimation;
16  
17  public class TweenAnimation extends View {
18  
19      private Animation mAnimationAlpha = null;                        //Alpha动画
20      private Animation mAnimationScale = null;                         //Scale动画
21      private Animation mAnimationTranslate = null;                //Translate动画
22      private Animation mAnimationRotate = null;                      //Rotate动画
23      
24      Context mContext = null;
25      Bitmap mBitmap_fuwa = null;                      //Bitmap对象
26      
27      public TweenAnimation(Context context) {
28          super(context);
29          mContext = context;
30          mBitmap_fuwa = ((BitmapDrawable) getResources().getDrawable
31                  (R.drawable.fuwa)).getBitmap();                  //加载Bitmap对象
32      }
33  
34      public void onDraw(Canvas canvas) {
35          super.onDraw(canvas);
36          canvas.drawBitmap(mBitmap_fuwa, 0, 0, null);
37      }
38      
39      //按键按下事件
40      public boolean onKeyDown(int keyCode, KeyEvent event) {
41          switch(keyCode) {
42          case KeyEvent.KEYCODE_DPAD_UP:                              //上键:Alpha动画效果
43              mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
44              mAnimationAlpha.setDuration(3000);
45  //            mAnimationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.alpha_animation);
46              this.startAnimation(mAnimationAlpha);
47              break;
48          case KeyEvent.KEYCODE_DPAD_DOWN:                      //下键:Scale动画效果
49              mAnimationScale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
50                      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
51              mAnimationScale.setDuration(3000);
52  //            mAnimationScale = AnimationUtils.loadAnimation(mContext, R.anim.scale_animation);
53              this.startAnimation(mAnimationScale);
54              break;
55          case KeyEvent.KEYCODE_DPAD_LEFT:                         //左键:Translate动画效果
56              mAnimationTranslate = new TranslateAnimation(10, 100, 10, 100);
57              mAnimationTranslate.setDuration(3000);
58  //            mAnimationTranslate = AnimationUtils.loadAnimation(mContext, R.anim.translate_animation);
59              this.startAnimation(mAnimationTranslate);
60              break;
61          case KeyEvent.KEYCODE_DPAD_RIGHT:                       //右键:Rotate动画效果
62              mAnimationRotate = new RotateAnimation(0.0f, 360.0f, 
63                      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
64              mAnimationRotate.setDuration(3000);
65  //            mAnimationRotate = AnimationUtils.loadAnimation(mContext, R.anim.rotate_animation);
66              this.startAnimation(mAnimationRotate);
67              break;
68          }
69          return true;
70      }
71      
72      //按键弹起事件
73      public boolean onKeyUp(int keyCode, KeyEvent event) {
74          return false;
75      }
76  }

  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源代码

 

 1 MainActivity.java源代码
 2  package com.example.android_tweenanimation;
 3  
 4  import android.os.Bundle;
 5  import android.view.View;
 6  import android.view.animation.Animation;
 7  import android.view.animation.AnimationUtils;
 8  import android.widget.Button;
 9  import android.widget.ImageView;
10  import android.app.Activity;
11  
12  public class MainActivity extends Activity {
13  
14      Animation mAnimation_alpha;          //透明度渐变动画对象
15      Animation mAnimation_scale;           //尺寸伸缩渐变动画对象
16      Animation mAnimation_translate;  //位置移动动画对象
17      Animation mAnimation_rotate;        //画面旋转动画对象
18      
19      ImageView mImageView;                    //图像视图对象
20      
21      Button mButton_alpha;                         //透明度渐变按钮
22      Button mButton_scale;                          //尺寸伸缩渐变按钮
23      Button mButton_translate;                 //位置移动按钮
24      Button mButton_rotate;                       //画面旋转按钮
25      
26      @Override
27      public void onCreate(Bundle savedInstanceState) {
28          super.onCreate(savedInstanceState);
29          setContentView(R.layout.activity_main);    
30          
31          //加载动画效果
32          mAnimation_alpha = AnimationUtils.loadAnimation(this, R.anim.alpha_animation);
33          mAnimation_scale = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
34          mAnimation_translate = AnimationUtils.loadAnimation(this, R.anim.translate_animation);
35          mAnimation_rotate = AnimationUtils.loadAnimation(this, R.anim.rotate_animation);
36          
37          //加载布局
38          mImageView =(ImageView)this.findViewById(R.id.imageview);   
39          mButton_alpha = (Button)this.findViewById(R.id.button_alpha);
40          mButton_scale = (Button)this.findViewById(R.id.button_scale);    
41          mButton_translate = (Button)this.findViewById(R.id.button_translate);
42          mButton_rotate = (Button)this.findViewById(R.id.button_rotate);   
43         
44          //透明度渐变按钮事件监听
45          mButton_alpha.setOnClickListener(new Button.OnClickListener() {
46              public void onClick(View v) {
47                  mImageView.startAnimation(mAnimation_alpha);
48              }            
49          });
50          
51          //尺寸伸缩渐变按钮事件监听
52          mButton_scale.setOnClickListener(new Button.OnClickListener() {
53              public void onClick(View v) {
54                  mImageView.startAnimation(mAnimation_scale);
55              }            
56          });
57          
58          //位置移动按钮事件监听
59          mButton_translate.setOnClickListener(new Button.OnClickListener() {
60              public void onClick(View v) {
61                  mImageView.startAnimation(mAnimation_translate);
62              }            
63          });
64          
65          //画面旋转按钮事件监听
66          mButton_rotate.setOnClickListener(new Button.OnClickListener() {
67              public void onClick(View v) {
68                  mImageView.startAnimation(mAnimation_rotate);
69              }            
70          });
71      }
72  }

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

activity_main.xml源代码
 1 activity_main.xml源代码
 2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3      xmlns:tools="http://schemas.android.com/tools"
 4      android:orientation="vertical"
 5      android:layout_width="match_parent"
 6      android:layout_height="match_parent" >
 7      
 8      <ImageView
 9          android:id="@+id/imageview"
10          android:layout_marginLeft="25dp"
11          android:layout_marginTop="50dp"
12          android:layout_width="wrap_content"
13          android:layout_height="wrap_content"
14          android:src="@drawable/fuwa"
15          android:contentDescription="fuwa"    />
16      
17      <LinearLayout 
18          android:orientation="horizontal"
19          android:layout_marginTop="50dp"
20          android:layout_width="match_parent"
21          android:layout_height="wrap_content"    >
22          
23          <Button 
24              android:id="@+id/button_alpha"
25              android:layout_weight="1"
26              android:layout_width="0dp"
27              android:layout_height="wrap_content"
28              android:text="透明度渐变"    />
29          
30          <Button 
31              android:id="@+id/button_scale"
32              android:layout_weight="1"
33              android:layout_width="0dp"
34              android:layout_height="wrap_content"
35              android:text="尺寸伸缩渐变"    />
36          
37      </LinearLayout>
38      
39      <LinearLayout 
40          android:orientation="horizontal"
41          android:layout_width="match_parent"
42          android:layout_height="wrap_content"    >
43          
44          <Button 
45              android:id="@+id/button_translate"
46              android:layout_weight="1"
47              android:layout_width="0dp"
48              android:layout_height="wrap_content"
49              android:text="画面位置移动"    />
50          
51          <Button 
52              android:id="@+id/button_rotate"
53              android:layout_weight="1"
54              android:layout_width="0dp"
55              android:layout_height="wrap_content"
56              android:text="画面旋转"    />
57          
58      </LinearLayout>
59      
60  </LinearLayout>

相关资料:

Android Animation学习笔记 http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html

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

Android中AnimationSet的使用 http://blog.csdn.net/yuzhiboyi/article/details/7731826

原文地址:https://www.cnblogs.com/britalient/p/3173156.html