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

来源:互联网 发布:nginx 访问500错误 编辑:程序博客网 时间:2024/05/29 16:01

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

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

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

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

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

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. 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. public class ScaleActivity extends Activity {
  10.     /**缩小动画按钮**/
  11.     Button mButton0 = null;
  12.     /**放大动画按钮**/
  13.     Button mButton1 = null;
  14.     /**显示动画的ImageView**/
  15.     ImageView mImageView = null;
  16.     /**缩小动画**/
  17.     Animation mLitteAnimation = null;
  18.     /**放大动画**/
  19.     Animation mBigAnimation = null;
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.scale);
  24.         /**拿到ImageView对象**/
  25.         mImageView = (ImageView)findViewById(R.id.imageView);
  26.         /**加载缩小与放大动画**/
  27.         mLitteAnimation = AnimationUtils.loadAnimation(this, R.anim.scalelitte);
  28.         mBigAnimation = AnimationUtils.loadAnimation(this, R.anim.scalebig);
  29.         mButton0 = (Button)findViewById(R.id.button0);
  30.         mButton0.setOnClickListener(new OnClickListener() {
  31.             @Override
  32.             public void onClick(View arg0) {
  33.                 /**播放缩小动画**/
  34.                 mImageView.startAnimation(mLitteAnimation);
  35.             }
  36.         });
  37.         mButton1 = (Button)findViewById(R.id.button1);
  38.         mButton1.setOnClickListener(new OnClickListener() {
  39.             @Override
  40.             public void onClick(View arg0) {
  41.                 /**播放放大动画**/
  42.                 mImageView.startAnimation(mBigAnimation);
  43.             }
  44.         });
  45.     }
  46. }

复制代码

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. 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. public class RotateActivity extends Activity {
  10.     /**向左旋转动画按钮**/
  11.     Button mButton0 = null;
  12.     /**向右旋转动画按钮**/
  13.     Button mButton1 = null;
  14.     /**显示动画的ImageView**/
  15.     ImageView mImageView = null;
  16.     /**向左旋转动画**/
  17.     Animation mLeftAnimation = null;
  18.     /**向右旋转动画**/
  19.     Animation mRightAnimation = null;
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.retate);
  24.         /**拿到ImageView对象**/
  25.         mImageView = (ImageView)findViewById(R.id.imageView);
  26.         /**加载向左与向右旋转动画**/
  27.         mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
  28.         mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
  29.         mButton0 = (Button)findViewById(R.id.button0);
  30.         mButton0.setOnClickListener(new OnClickListener() {
  31.             @Override
  32.             public void onClick(View arg0) {
  33.                 /**播放向左旋转动画**/
  34.                 mImageView.startAnimation(mLeftAnimation);
  35.             }
  36.         });
  37.         mButton1 = (Button)findViewById(R.id.button1);
  38.         mButton1.setOnClickListener(new OnClickListener() {
  39.             @Override
  40.             public void onClick(View arg0) {
  41.                 /**播放向右旋转动画**/
  42.                 mImageView.startAnimation(mRightAnimation);
  43.             }
  44.         });
  45.     }
  46. }

复制代码

3.Translate移动动画

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

这个动画布局设置动画从左到右(0.0),从上到下(320,480)做匀速移动。

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <translate  xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:fromXDelta=”0″
  4.     android:toXDelta=”320″
  5.     android:fromYDelta=”0″
  6.     android:toYDelta=”480″
  7.     android:duration=”2000″
  8.         android:repeatCount=”infinite”
  9. />

复制代码

代码实现

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class TranslateActivity extends Activity {
  7.     /**显示动画的ImageView**/
  8.     ImageView mImageView = null;
  9.     /**移动动画**/
  10.     Animation mAnimation = null;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.translate);
  15.         /**拿到ImageView对象**/
  16.         mImageView = (ImageView)findViewById(R.id.imageView);
  17.         /**加载移动动画**/
  18.         mAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
  19.         /**播放移动动画**/
  20.         mImageView.startAnimation(mAnimation);
  21.     }
  22. }

复制代码

4 .Alpha:透明渐变动画

<alpha>标签为alpha透明度节点
android:fromAlpha=”1.0″ 设置动画起始透明度为1.0 表示完全不透明
android:toAlpha=”0.0″设置动画结束透明度为0.0 表示完全透明
也就是说alpha的取值范围为0.0 – 1.0 之间

这个动画布局设置动画从完全不透明渐变到完全透明。

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <alpha  xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:fromAlpha=”1.0″
  4.     android:toAlpha=”0.0″
  5.     android:repeatCount=”infinite”
  6.     android:duration=”2000″>
  7. </alpha>

复制代码

代码实现

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class AlphaActivity extends Activity {
  7.     /**显示动画的ImageView**/
  8.     ImageView mImageView = null;
  9.     /**透明动画**/
  10.     Animation mAnimation = null;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.translate);
  15.         /**拿到ImageView对象**/
  16.         mImageView = (ImageView)findViewById(R.id.imageView);
  17.         /**加载透明动画**/
  18.         mAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
  19.         /**播放透明动画**/
  20.         mImageView.startAnimation(mAnimation);
  21.     }
  22. }

复制代码

5.综合动画

可以将上面介绍的4种动画设置在一起同时进行播放,那么就须要使用<set>标签将所有须要播放的动画放在一起。

这个动画布局设置动画同时播放移动、渐变、旋转。

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <set xmlns:android=”http://schemas.android.com/apk/res/android”>
  3.     <rotate
  4.         android:interpolator=”@android:anim/accelerate_interpolator”
  5.         android:fromDegrees=”+360″
  6.         android:toDegrees=”0″
  7.         android:pivotX=”50%”
  8.         android:pivotY=”50%”
  9.         android:duration=”2000″
  10.         android:repeatCount=”infinite”
  11.     />
  12.     <alpha  android:fromAlpha=”1.0″
  13.     android:toAlpha=”0.0″
  14.     android:repeatCount=”infinite”
  15.     android:duration=”2000″>
  16.         </alpha>
  17. <translate
  18.     android:fromXDelta=”0″
  19.     android:toXDelta=”320″
  20.     android:fromYDelta=”0″
  21.     android:toYDelta=”480″
  22.     android:duration=”2000″
  23.         android:repeatCount=”infinite”
  24. />
  25. </set>

复制代码

代码实现

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.animation.Animation;
  4. import android.view.animation.AnimationUtils;
  5. import android.widget.ImageView;
  6. public class AllActivity extends Activity {
  7.     /**显示动画的ImageView**/
  8.     ImageView mImageView = null;
  9.     /**综合动画**/
  10.     Animation mAnimation = null;
  11.     @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.translate);
  15.         /**拿到ImageView对象**/
  16.         mImageView = (ImageView)findViewById(R.id.imageView);
  17.         /**加载综合动画**/
  18.         mAnimation = AnimationUtils.loadAnimation(this, R.anim.all);
  19.         /**播放综合动画**/
  20.         mImageView.startAnimation(mAnimation);
  21.     }
  22. }