玩转Android---2D图形及动画---动画分析(Tween详细分析)

来源:互联网 发布:java接口与安卓对接 编辑:程序博客网 时间:2024/05/11 05:09

原址:http://hualang.iteye.com/category/143855

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

 

Tween动画:

Tween动画能完成一系列简单的变化(如位置、尺寸、透明度和旋转等)。例如,在你的程序中有一个ImageView组件,我们通过Tween动画可以使该视图实现放大、缩小、旋转、渐变等。Tween动画类位于android.view.animation包中,该包中包含了一些常用的动画实现类。

 

  • Animation:动画抽象类,其他几个实现类继承它
  • ScaleAnimation:控制尺寸变化的动画类
  • AlphaAnimation:控制透明度变化的动画类
  • RotateAnimation:控制旋转变化的动画类
  • TranslateAnimation:控制位置变化的动画类
  • AnimationSet:定义动画属性集合类
  • AnimationUtils:动画工具类
总的来说,Android系统Tween动画提供了四种实现方式。Tween动画的实现凡是有两种:一种是通过硬编码的凡是在程序代码中实现;另一种是在配置文件中定义。Android系统推荐使用配置文件的方法,这种方式可口占星较好。

常用构造方法

参数说明

AlphaAnimation(float fromAlpha, float toAlpha)

fromAlpha:动画开始透明度

toAlpha:动画结束透明度(取值范围0.0~1.0)

1.0表示完全不透明

0.0表示完全透明

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

fromX:起始X坐标上的伸缩尺寸

toX:结束X坐标上的伸缩尺寸

fromY:其实Y坐标上的伸缩尺寸

toY:结束Y坐标上的伸缩尺寸

pivotXType:X坐标伸缩模式(取值Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT)

pivotXValue:相当于X坐标伸缩值

pivotYType:Y坐标伸缩模式(取值

Animation.ABSOLUTE、

Animation.RELATIVE_TO_SELF、

Animation.RELATIVE_TO_PARENT)

pivotYValue:相当于Y坐标伸缩值

TranslateAnimation(float fromXDelta, float toXDelta,

float fromYDelta, float toYDelta)

fromXDelta:其实X坐标

toXDelta:结束X坐标

fromYDelta:起始Y坐标

toYDelta:结束Y坐标

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

fromDegrees:旋转开始角度

toDegrees:旋转结束角度

pivotXType:X坐标伸缩模式(取值Animation.ABSOLUTE,

Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT)

pivotXValue:相当于X坐标伸缩值

pivotYType:Y坐标伸缩模式(取值Animation.ABSULUTE,

Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT)

pivotYValue:相当于Y坐标伸缩值

如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数。

以x轴为例介绍参照与对应值的关系

 如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位

 如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件,

 对应值应该理解为相对于自身或者父控件的几倍或百分之多少。


下面是个例子,看看硬编码是如何实现的
布局文件main.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content">  
  11.         <Button  
  12.             android:id="@+id/ScaleBtn"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="Scale"  
  16.         />  
  17.         <Button  
  18.             android:id="@+id/AlphaBtn"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="Alpha"  
  22.         />  
  23.         <Button  
  24.             android:id="@+id/TranslateBtn"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="Translate"  
  28.         />  
  29.         <Button  
  30.             android:id="@+id/RotateBtn"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="Rotate"  
  34.         />  
  35.     </LinearLayout>  
  36.     <ImageView  
  37.         android:id="@+id/Imageview01"  
  38.         android:layout_gravity="center"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:src="@drawable/ni_png_0707"  
  42.     />  
  43. </LinearLayout>  
 MainActivity.java
Java代码  收藏代码
  1. package com.loulijun.AnimaitonTest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.AlphaAnimation;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.RotateAnimation;  
  10. import android.view.animation.ScaleAnimation;  
  11. import android.view.animation.TranslateAnimation;  
  12. import android.widget.Button;  
  13. import android.widget.ImageView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.     private Button scaleBtn,rotateBtn,alphaBtn,translateBtn;  
  17.     private ImageView img;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         img = (ImageView)findViewById(R.id.Imageview01);  
  23.         scaleBtn = (Button)findViewById(R.id.ScaleBtn);  
  24.         rotateBtn = (Button)findViewById(R.id.RotateBtn);  
  25.         alphaBtn = (Button)findViewById(R.id.AlphaBtn);  
  26.         translateBtn = (Button)findViewById(R.id.TranslateBtn);  
  27.           
  28.         scaleBtn.setOnClickListener(new Button.OnClickListener()  
  29.         {  
  30.   
  31.             @Override  
  32.             public void onClick(View arg0) {  
  33.                 //创建伸缩动画  
  34.                 Animation scaleAnimation = new ScaleAnimation(0f,1f,0f,1f,  
  35.                         Animation.RELATIVE_TO_SELF, 0.5f,  
  36.                         Animation.RELATIVE_TO_SELF, 0.5f);  
  37.                 //设置动画持续时间  
  38.                 scaleAnimation.setDuration(2000);  
  39.                 //开始动画  
  40.                 img.startAnimation(scaleAnimation);  
  41.             }  
  42.               
  43.         });  
  44.           
  45.         rotateBtn.setOnClickListener(new OnClickListener()  
  46.         {  
  47.   
  48.             @Override  
  49.             public void onClick(View v) {  
  50.                 //创建rotate旋转动画  
  51.                 Animation rotateAnimation = new RotateAnimation(0f, +36f,   
  52.                         Animation.RELATIVE_TO_SELF, 0.5f,  
  53.                         Animation.RELATIVE_TO_SELF, 0.5f);  
  54.                 //设置动画持续时间  
  55.                 rotateAnimation.setDuration(2000);  
  56.                 //开始动画  
  57.                 img.startAnimation(rotateAnimation);  
  58.             }  
  59.               
  60.         });  
  61.           
  62.         translateBtn.setOnClickListener(new OnClickListener()  
  63.         {  
  64.   
  65.             @Override  
  66.             public void onClick(View v) {  
  67.                 // 创建位置变化动画  
  68.                 Animation translateAnimation = new TranslateAnimation(1010010100);  
  69.                 //设置动画持续时间  
  70.                 translateAnimation.setDuration(2000);  
  71.                 //开始动画  
  72.                 img.startAnimation(translateAnimation);  
  73.             }  
  74.               
  75.         });  
  76.           
  77.         alphaBtn.setOnClickListener(new OnClickListener()  
  78.         {  
  79.   
  80.             @Override  
  81.             public void onClick(View v) {  
  82.                 //创建渐变动画  
  83.                 Animation alphaAnimation = new AlphaAnimation(0.1f,1.0f);  
  84.                 //设置动画持续时间  
  85.                 alphaAnimation.setDuration(2000);  
  86.                 //开始动画  
  87.                 img.startAnimation(alphaAnimation);  
  88.             }  
  89.               
  90.         });  
  91.     }  
  92. }  
 运行效果(四个效果自己尝试下就可以了)


 
当然,最好是在配置文件中来实现动画
在res/anim目录下定义不同的动画的配置文件,一般要有个set根元素,根元素里面定义不同的动画了,然后通过调用
AnimationUtils.loadAnimation()方法获取动画实例,调用视图组件的startAnimation()方法开启动画即可

主布局文件main.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content">  
  11.         <Button  
  12.             android:id="@+id/ScaleBtn"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:text="Scale"  
  16.         />  
  17.         <Button  
  18.             android:id="@+id/AlphaBtn"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="Alpha"  
  22.         />  
  23.         <Button  
  24.             android:id="@+id/TranslateBtn"  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="Translate"  
  28.         />  
  29.         <Button  
  30.             android:id="@+id/RotateBtn"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="Rotate"  
  34.         />  
  35.     </LinearLayout>  
  36.     <ImageView  
  37.         android:id="@+id/Imageview01"  
  38.         android:layout_gravity="center"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:src="@drawable/ni_png_0707"  
  42.     />  
  43. </LinearLayout>  
 然后在res/anim/目录下创建四个布局文件,分别对应四个动画效果的配置
myalpha.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <alpha  
  4.         android:fromAlpha="0.1"  
  5.         android:toAlpha="1.0"  
  6.         android:duration="2000"  
  7.     />  
  8. </set>  
 myrotate.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <rotate  
  4.         android:fromDegrees="0"  
  5.         android:toDegrees="-180"  
  6.         android:pivotX="50%"  
  7.         android:pivotY="50%"  
  8.         android:duration="2000"  
  9.     />  
  10. </set>  
 mytranslate.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate  
  4.         android:fromXDelta="10"  
  5.         android:toXDelta="100"  
  6.         android:fromYDelta="10"  
  7.         android:toYDelta="100"  
  8.         android:duration="2000"  
  9.     />  
  10. </set>  
 myscale.xml
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale  
  4.         android:fromXScale="0.0"  
  5.         android:toXScale="1.0"  
  6.         android:fromYScale="0.0"  
  7.         android:toYScale="1.0"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:duration="2000"  
  11.     />  
  12. </set>  
 下面这个是主程序了,在里面主要是通过AnimationUtils.loadAnimaiton()将上面的动画配置文件加载后,开始动画即可
MainActivity.java
Java代码  收藏代码
  1. package com.loulijun.animationtest2;  
  2.   
  3. import android.app.Activity;  
  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.   
  11. public class MainActivity extends Activity {  
  12.     private Button alphaBtn,rotateBtn,translateBtn,scaleBtn;  
  13.     private ImageView img;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         img = (ImageView)findViewById(R.id.Imageview01);  
  19.         alphaBtn = (Button)findViewById(R.id.AlphaBtn);  
  20.         rotateBtn = (Button)findViewById(R.id.RotateBtn);  
  21.         translateBtn = (Button)findViewById(R.id.TranslateBtn);  
  22.         scaleBtn = (Button)findViewById(R.id.ScaleBtn);  
  23.         alphaBtn.setOnClickListener(new Button.OnClickListener()  
  24.         {  
  25.   
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 //设置渐变动画  
  29.                 Animation alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.myalpha);  
  30.                 //开始动画  
  31.                 img.startAnimation(alphaAnimation);  
  32.             }  
  33.               
  34.         });  
  35.           
  36.         rotateBtn.setOnClickListener(new Button.OnClickListener()  
  37.         {  
  38.   
  39.             @Override  
  40.             public void onClick(View v) {  
  41.                 //设置旋转动画  
  42.                 Animation rotateAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.myrotate);  
  43.                 //开始动画  
  44.                 img.startAnimation(rotateAnimation);  
  45.             }  
  46.               
  47.         });  
  48.           
  49.         translateBtn.setOnClickListener(new Button.OnClickListener()  
  50.         {  
  51.   
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 //设置位置变化动画  
  55.                 Animation translateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.mytranslate);  
  56.                 //开始动画  
  57.                 img.startAnimation(translateAnimation);  
  58.             }  
  59.               
  60.         });  
  61.           
  62.         scaleBtn.setOnClickListener(new Button.OnClickListener()  
  63.         {  
  64.   
  65.             @Override  
  66.             public void onClick(View v) {  
  67.                 //创建伸缩动画  
  68.                 Animation scaleAnimation = AnimationUtils.loadAnimation(MainActivity.this,   
  69.                         R.anim.myscale);  
  70.                 //开始动画  
  71.                 img.startAnimation(scaleAnimation);  
  72.             }  
  73.               
  74.         });  
  75.     }  
  76. }  
 
效果如下:


 

0 0