Android游戏开发之Tween动画的实现


今天和大伙讨论一下Android开发中的Tween动画的实现。首先它和上一章()我们讨论的Frame动画同属于系统提供的绘制动画的方法。Tween动画主要的功能是在绘制动画前设置动画绘制的轨迹,包括时间, 位置 ,等等。但是Tween动画的缺点是它只能设置起始点与结束点的两帧,中间过程全部由系统帮我们完成。所以在帧数比较多的游戏开发中是不太会用到它的。

Tween一共提供了4中动画的效果

Scale:缩放动画
Rotate:旋转动画
Translate:移动动画
Alpha::透明渐变动画

Tween与Frame动画类似都需要在res\anim路径下创建动画的 布局文件



补充:最近有盆友提问可不可以不用XML配置动画,希望可以在代码中配置。那MOMO当然要向大家补充了噢~~~

1.Scale缩放动画



<scale>标签为缩放节点
android:fromXscale="1.0" 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
android:toXscale="0.0"表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)
android:fromYscale="1.0" 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)
android:toYscale="0.0"表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)
android:pivotX="50%" X轴缩放的位置为中心点
android:pivotY="50%" Y轴缩放的位置为中心点
android:duration="2000" 动画播放时间 这里是2000毫秒也就是2秒

这个动画布局设置动画从大到小进行缩小。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.             android:fromXScale="1.0"   
  4.             android:toXScale="0.0"  
  5.             android:fromYScale="1.0"   
  6.             android:toYScale="0.0"   
  7.             android:pivotX="50%"  
  8.             android:pivotY="50%"   
  9.             android:duration="2000">  
  10. </scale>  
在代码中加载动画
  1. mLitteAnimation =  new ScaleAnimation(0.0f, 1.0f, 0.0f,  1.0f,  
  2.                  Animation.RELATIVE_TO_SELF, 0.5f,    
  3.                  Animation.RELATIVE_TO_SELF, 0.5f);    
  4. mLitteAnimation.setDuration(2000);  
代码如下
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.   
  10. public class ScaleActivity extends Activity {  
  11.   
  12.     /**缩小动画按钮**/  
  13.     Button mButton0 = null;  
  14.     
  15.     /**放大动画按钮**/  
  16.     Button mButton1 = null;  
  17.     
  18.     /**显示动画的ImageView**/  
  19.     ImageView mImageView = null;  
  20.     
  21.     /**缩小动画**/  
  22.     Animation mLitteAnimation = null;  
  23.       
  24.     /**放大动画**/  
  25.     Animation mBigAnimation = null;   
  26.       
  27.       
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.     super.onCreate(savedInstanceState);  
  31.     setContentView(R.layout.scale);  
  32.   
  33.     /**拿到ImageView对象**/  
  34.     mImageView = (ImageView)findViewById(R.id.imageView);  
  35.   
  36.     /**加载缩小与放大动画**/  
  37.     mLitteAnimation = AnimationUtils.loadAnimation(this, R.anim.scalelitte);  
  38.     mBigAnimation = AnimationUtils.loadAnimation(this, R.anim.scalebig);  
  39.       
  40.     mButton0 = (Button)findViewById(R.id.button0);  
  41.     mButton0.setOnClickListener(new OnClickListener() {  
  42.           
  43.         @Override  
  44.         public void onClick(View arg0) {  
  45.           
  46.         /**播放缩小动画**/  
  47.         mImageView.startAnimation(mLitteAnimation);  
  48.           
  49.         }  
  50.     });  
  51.       
  52.     mButton1 = (Button)findViewById(R.id.button1);  
  53.     mButton1.setOnClickListener(new OnClickListener() {  
  54.           
  55.         @Override  
  56.         public void onClick(View arg0) {  
  57.         /**播放放大动画**/  
  58.         mImageView.startAnimation(mBigAnimation);  
  59.         }  
  60.     });  
  61.     }  
  62. }  
2.Rotate旋转动画




<rotate>标签为旋转节点
Tween一共为我们提供了3种动画渲染模式。
android:interpolator="@android:anim/accelerate_interpolator" 设置动画渲染器为加速动画(动画播放中越来越快)
android:interpolator="@android:anim/decelerate_interpolator" 设置动画渲染器为减速动画(动画播放中越来越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)
如果不写的话 默认为匀速运动

android:fromDegrees="+360"设置动画开始的角度
android:toDegrees="0"设置动画结束的角度

这个动画布局设置动画将向左做360度旋转加速运动。
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"   
  3.         android:interpolator="@android:anim/accelerate_interpolator"    
  4.         android:fromDegrees="+360"    
  5.         android:toDegrees="0"    
  6.         android:pivotX="50%"    
  7.         android:pivotY="50%"    
  8.         android:duration="2000"   
  9. />   
在代码中加载动画
  1. mLeftAnimation = new RotateAnimation(360.0f, 0.0f,  
  2.     Animation.RELATIVE_TO_SELF, 0.5f,    
  3.     Animation.RELATIVE_TO_SELF, 0.5f);              
  4. mLeftAnimation.setDuration(2000);    
代码实现
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ImageView;  
  9.   
  10. public class RotateActivity extends Activity {  
  11.   
  12.     /**向左旋转动画按钮**/  
  13.     Button mButton0 = null;  
  14.     
  15.     /**向右旋转动画按钮**/  
  16.     Button mButton1 = null;  
  17.     
  18.     /**显示动画的ImageView**/  
  19.     ImageView mImageView = null;  
  20.     
  21.     /**向左旋转动画**/  
  22.     Animation mLeftAnimation = null;  
  23.       
  24.     /**向右旋转动画**/  
  25.     Animation mRightAnimation = null;   
  26.       
  27.       
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.     super.onCreate(savedInstanceState);  
  31.     setContentView(R.layout.retate);  
  32.   
  33.     /**拿到ImageView对象**/  
  34.     mImageView = (ImageView)findViewById(R.id.imageView);  
  35.   
  36.     /**加载向左与向右旋转动画**/  
  37.     mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);  
  38.     mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);  
  39.       
  40.     mButton0 = (Button)findViewById(R.id.button0);  
  41.     mButton0.setOnClickListener(new OnClickListener() {  
  42.           
  43.         @Override  
  44.         public void onClick(View arg0) {  
  45.           
  46.         /**播放向左旋转动画**/  
  47.         mImageView.startAnimation(mLeftAnimation);  
  48.           
  49.         }  
  50.     });  
  51.       
  52.     mButton1 = (Button)findViewById(R.id.button1);  
  53.     mButton1.setOnClickListener(new OnClickListener() {  
  54.           
  55.         @Override  
  56.         public void onClick(View arg0) {  
  57.         /**播放向右旋转动画**/  
  58.         mImageView.startAnimation(mRightAnimation);  
  59.         }  
  60.     });  
  61.     }  
  62. }  
3.Translate移动动画





<translate>标签为移动节点
android:repeatCount="infinite" 设置动画为循环播放,这里可以写具体的int数值,设置动画播放几次,但是它记录次数是从0开始数的,比如这里设置为2 那么动画从0开始数数0 、1、 2 、实际上是播放了3次。
  • 1
  • 2
  • 下一页

相关内容