`
landyer
  • 浏览: 139012 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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秒

这个动画布局设置动画从大到小进行缩小。
  • <?xml version="1.0" encoding="utf-8"?>
  • <scale xmlns:android="http://schemas.android.com/apk/res/android"
  •                 android:fromXScale="1.0"
  •                     android:toXScale="0.0"
  •                 android:fromYScale="1.0"
  •                 android:toYScale="0.0"
  •                 android:pivotX="50%"
  •                 android:pivotY="50%"
  •                 android:duration="2000">
  • </scale>

复制代码
代码如下
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.view.animation.Animation;
  • import android.view.animation.AnimationUtils;
  • import android.widget.Button;
  • import android.widget.ImageView;
  • public class ScaleActivity extends Activity {
  •     /**缩小动画按钮**/
  •     Button mButton0 = null;
  •     /**放大动画按钮**/
  •     Button mButton1 = null;
  •     /**显示动画的ImageView**/
  •     ImageView mImageView = null;
  •     /**缩小动画**/
  •     Animation mLitteAnimation = null;
  •     /**放大动画**/
  •     Animation mBigAnimation = null;
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.scale);
  •         /**拿到ImageView对象**/
  •         mImageView = (ImageView)findViewById(R.id.imageView);
  •         /**加载缩小与放大动画**/
  •         mLitteAnimation = AnimationUtils.loadAnimation(this, R.anim.scalelitte);
  •         mBigAnimation = AnimationUtils.loadAnimation(this, R.anim.scalebig);
  •         mButton0 = (Button)findViewById(R.id.button0);
  •         mButton0.setOnClickListener(new OnClickListener() {
  •             @Override
  •             public void onClick(View arg0) {
  •                 /**播放缩小动画**/
  •                 mImageView.startAnimation(mLitteAnimation);
  •             }
  •         });
  •         mButton1 = (Button)findViewById(R.id.button1);
  •         mButton1.setOnClickListener(new OnClickListener() {
  •             @Override
  •             public void onClick(View arg0) {
  •                 /**播放放大动画**/
  •                 mImageView.startAnimation(mBigAnimation);
  •             }
  •         });
  •     }
  • }

复制代码
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度旋转加速运动。
  • <?xml version="1.0" encoding="utf-8"?>
  • <rotate xmlns:android="http://schemas.android.com/apk/res/android"
  •         android:interpolator="@android:anim/accelerate_interpolator"
  •         android:fromDegrees="+360"
  •         android:toDegrees="0"
  •         android:pivotX="50%"
  •         android:pivotY="50%"
  •         android:duration="2000"
  • />

复制代码
代码实现
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.view.animation.Animation;
  • import android.view.animation.AnimationUtils;
  • import android.widget.Button;
  • import android.widget.ImageView;
  • public class RotateActivity extends Activity {
  •     /**向左旋转动画按钮**/
  •     Button mButton0 = null;
  •     /**向右旋转动画按钮**/
  •     Button mButton1 = null;
  •     /**显示动画的ImageView**/
  •     ImageView mImageView = null;
  •     /**向左旋转动画**/
  •     Animation mLeftAnimation = null;
  •     /**向右旋转动画**/
  •     Animation mRightAnimation = null;
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.retate);
  •         /**拿到ImageView对象**/
  •         mImageView = (ImageView)findViewById(R.id.imageView);
  •         /**加载向左与向右旋转动画**/
  •         mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
  •         mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
  •         mButton0 = (Button)findViewById(R.id.button0);
  •         mButton0.setOnClickListener(new OnClickListener() {
  •             @Override
  •             public void onClick(View arg0) {
  •                 /**播放向左旋转动画**/
  •                 mImageView.startAnimation(mLeftAnimation);
  •             }
  •         });
  •         mButton1 = (Button)findViewById(R.id.button1);
  •         mButton1.setOnClickListener(new OnClickListener() {
  •             @Override
  •             public void onClick(View arg0) {
  •                 /**播放向右旋转动画**/
  •                 mImageView.startAnimation(mRightAnimation);
  •             }
  •         });
  •     }
  • }

复制代码
3.Translate移动动画






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

这个动画布局设置动画从左到右(0.0),从上到下(320,480)做匀速移动。
  • <?xml version="1.0" encoding="utf-8"?>
  • <translate  xmlns:android="http://schemas.android.com/apk/res/android"
  •     android:fromXDelta="0"
  •     android:toXDelta="320"
  •     android:fromYDelta="0"
  •     android:toYDelta="480"
  •     android:duration="2000"
  •         android:repeatCount="infinite"
  • />

复制代码
代码实现
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.animation.Animation;
  • import android.view.animation.AnimationUtils;
  • import android.widget.ImageView;
  • public class TranslateActivity extends Activity {
  •     /**显示动画的ImageView**/
  •     ImageView mImageView = null;
  •     /**移动动画**/
  •     Animation mAnimation = null;
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.translate);
  •         /**拿到ImageView对象**/
  •         mImageView = (ImageView)findViewById(R.id.imageView);
  •         /**加载移动动画**/
  •         mAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
  •         /**播放移动动画**/
  •         mImageView.startAnimation(mAnimation);
  •     }
  • }

复制代码
4 .Alpha:透明渐变动画



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

这个动画布局设置动画从完全不透明渐变到完全透明。
  • <?xml version="1.0" encoding="utf-8"?>
  • <alpha  xmlns:android="http://schemas.android.com/apk/res/android"
  •     android:fromAlpha="1.0"
  •     android:toAlpha="0.0"
  •     android:repeatCount="infinite"
  •     android:duration="2000">
  • </alpha>

复制代码
代码实现
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.animation.Animation;
  • import android.view.animation.AnimationUtils;
  • import android.widget.ImageView;
  • public class AlphaActivity extends Activity {
  •     /**显示动画的ImageView**/
  •     ImageView mImageView = null;
  •     /**透明动画**/
  •     Animation mAnimation = null;
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.translate);
  •         /**拿到ImageView对象**/
  •         mImageView = (ImageView)findViewById(R.id.imageView);
  •         /**加载透明动画**/
  •         mAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
  •         /**播放透明动画**/
  •         mImageView.startAnimation(mAnimation);
  •     }
  • }

复制代码
5.综合动画





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

这个动画布局设置动画同时播放移动、渐变、旋转。
  • <?xml version="1.0" encoding="utf-8"?>
  • <set xmlns:android="http://schemas.android.com/apk/res/android">
  •     <rotate
  •         android:interpolator="@android:anim/accelerate_interpolator"
  •         android:fromDegrees="+360"
  •         android:toDegrees="0"
  •         android:pivotX="50%"
  •         android:pivotY="50%"
  •         android:duration="2000"
  •         android:repeatCount="infinite"
  •     />
  •     <alpha  android:fromAlpha="1.0"
  •     android:toAlpha="0.0"
  •     android:repeatCount="infinite"
  •     android:duration="2000">
  •         </alpha>
  • <translate
  •     android:fromXDelta="0"
  •     android:toXDelta="320"
  •     android:fromYDelta="0"
  •     android:toYDelta="480"
  •     android:duration="2000"
  •         android:repeatCount="infinite"
  • />
  • </set>

复制代码
代码实现
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.animation.Animation;
  • import android.view.animation.AnimationUtils;
  • import android.widget.ImageView;
  • public class AllActivity extends Activity {
  •     /**显示动画的ImageView**/
  •     ImageView mImageView = null;
  •     /**综合动画**/
  •     Animation mAnimation = null;
  •     @Override
  •     public void onCreate(Bundle savedInstanceState) {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.translate);
  •         /**拿到ImageView对象**/
  •         mImageView = (ImageView)findViewById(R.id.imageView);
  •         /**加载综合动画**/
  •         mAnimation = AnimationUtils.loadAnimation(this, R.anim.all);
  •         /**播放综合动画**/
  •         mImageView.startAnimation(mAnimation);
  •     }
  • }


分享到:
评论

相关推荐

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

    雨松MOMO带你做游戏 Android 游戏开发之Tween动画的实现 欢迎大家下载阅读 哇咔咔~~~

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

    android 游戏开发之Tween动画的实现~~~~~~~~~~~

    Android Tween动画的实现

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

    安卓动画效果相关-android开发中的Tween动画动画演示demo。此demo为androidstudio2.0所写.rar

    android开发中的Tween动画,动画演示demo。 此demo为android studio 2.0 所写.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    Android开发之Tween动画实现方法

    在Android系统中提供了两种动画实现方式:一种是Tween动画,通过视图组件移动、放大、缩小,以及产生透明度的变化等来实现动画;另一种是Frame动画,这是一种传统的通过顺序播放排列好的图片来实现的动画方法,类似...

    tween动画效果

    tween可以实现android手机开发中的移动、缩放等动画效果

    android开发揭秘PDF

    5.3.1 Tween动画 5.3.2 Frame动画 5.3.3 GIF动画播放 5.4 小结 第6章 Android数据存储 6.1 Android数据存储初探 6.2 数据存储之Shared Preferences 6.3 数据存储之Files 6.4 数据存储之Network 6.5 Android数据库...

    Android控件Tween动画(补间动画)实现方法示例

    本文实例讲述了Android控件Tween动画(补间动画)实现方法。分享给大家供大家参考,具体如下: Android动画中的Tween动画:是把控件对象不断的进行图像变化来产生旋转、平移、放缩和渐变等动画效果。 /** * 控件...

    《Android应用开发揭秘》附带光盘代码.

     5.3.1 Tween动画  5.3.2 Frame动画  5.3.3 GIF动画播放  5.4 小结  第6章 Android数据存储  6.1 Android数据存储初探  6.2 数据存储之Shared Preferences  6.3 数据存储之Files  6.4 数据存储之Network  ...

    Android 百战经典-Android补间动画(Tween)大观园

    Android 百战经典-Android补间动画(Tween)大观园,博客:http://blog.csdn.net/yayun0516

    《Android应用开发揭秘》源码

     5.3.1 Tween动画  5.3.2 Frame动画  5.3.3 GIF动画播放  5.4 小结  第6章 Android数据存储  6.1 Android数据存储初探  6.2 数据存储之Shared Preferences  6.3 数据存储之Files  6.4 数据存储之Network  ...

    Android动画框架详解

    Android 平台提供了一套完整的...本文是第一部分原理篇,主要分析 Tween 动画的实现原理, 最后简单介绍在 Android 中如何通过播放 Gif 文件来实现动画。第二部分实例篇将在原理篇的基础上,向您展示一个动画实例的实现

    Android应用开发揭秘pdf高清版

    5.3.1 Tween动画 5.3.2 Frame动画 5.3.3 GIF动画播放 5.4 小结 第6章 Android数据存储 6.1 Android数据存储初探 6.2 数据存储之Shared Preferences 6.3 数据存储之Files 6.4 数据存储之Network 6.5 Android数据库...

    Android动画之补间动画(Tween Animation)基础学习

    之前说过了在Android中,动画Animation的实现有两种方式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是...

    Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移、旋转)

    本文实例讲述了Android动画之渐变动画(Tween Animation)。分享给大家供大家参考,具体如下: Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、...

Global site tag (gtag.js) - Google Analytics