透明度动画、旋转动画、尺寸伸缩动画、移动动画参数详解

Animation 动画:

定义

[java]
  1. private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;  
  2.     private AnimationSet animationSet;  
分别是透明度动画、旋转动画、尺寸伸缩动画、移动动画

具体实现代码如下:

[java]
  1. private void initAnimation() {  
  2.         //透明度控制动画效果 alpha   
  3.         animation_alpha=new AlphaAnimation(0.1f,1.0f);  
  4.         //第一个参数fromAlpha为 动画开始时候透明度   
  5.         //第二个参数toAlpha为 动画结束时候透明度   
  6.         animation_alpha.setRepeatCount(-1);//设置循环   
  7.         animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒   
  8.           
  9.         // 旋转效果rotate   
  10.         animation_rotate = new RotateAnimation(0, -720,  
  11.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  12.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  13.           //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度   
  14.           //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置   
  15.           //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置   
  16.         animation_rotate.setRepeatCount(-1);  
  17.         animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒   
  18.           
  19.         //尺寸伸缩动画效果 scale   
  20.         animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  21.         //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸       
  22.         //第二个参数toX为动画结束时 X坐标上的伸缩尺寸        
  23.         //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸       
  24.         //第四个参数toY为动画结束时Y坐标上的伸缩尺寸     
  25.         /*说明: 
  26.                             以上四种属性值     
  27.               0.0表示收缩到没有  
  28.               1.0表示正常无伸缩      
  29.                            值小于1.0表示收缩   
  30.                            值大于1.0表示放大 
  31.         */  
  32.         //第五个参数pivotXType为动画在X轴相对于物件位置类型     
  33.         //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置   
  34.         //第七个参数pivotXType为动画在Y轴相对于物件位置类型      
  35.         //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置   
  36.         animation_scale.setRepeatCount(-1);  
  37.         animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒   
  38.           
  39.         //移动动画效果translate   
  40.         animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);  
  41.         //第一个参数fromXDelta为动画起始时 X坐标上的移动位置       
  42.         //第二个参数toXDelta为动画结束时 X坐标上的移动位置         
  43.         //第三个参数fromYDelta为动画起始时Y坐标上的移动位置    
  44.         //第三个参数toYDelta为动画结束时Y坐标上的移动位置    
  45.         animation_translate.setRepeatCount(-1);//设置动画执行多少次,如果是-1的话就是一直重复   
  46.         animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒   
  47.           
  48.         animationSet=new AnimationSet(true);  
  49.           
  50.         animationSet.addAnimation(animation_alpha);//透明度   
  51.         animationSet.addAnimation(animation_rotate);//旋转   
  52.         animationSet.addAnimation(animation_scale);//尺寸伸缩   
  53.         animationSet.addAnimation(animation_translate);//移动   
  54.         image.startAnimation(animationSet);//开始播放   
  55.     }  


原文:http://www.linuxidc.com/Linux/2012-01/51450.htm

1、Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换 ( 平移、缩放、旋转 ) 产生动画效果;第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似。我们先介绍Tween来实现简单的动画;

2、首先看效果图


        

在项目res文件夹下建一个anim文件夹里建一个tween.xml文件:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <alpha  
  5.         android:duration="5000"  
  6.         android:fromAlpha="0"  
  7.         android:toAlpha="1.0" />  
  8.     <!--  
  9.         透明度控制动画效果 alpha  
  10.         浮点型值:  
  11.             fromAlpha 属性为动画起始时透明度  
  12.             toAlpha   属性为动画结束时透明度  
  13.             说明:   
  14.                 0.0表示完全透明  
  15.                 1.0表示完全不透明  
  16.             以上值取0.0-1.0之间的float数据类型的数字  
  17.           
  18.         长整型值:  
  19.             duration  属性为动画持续时间  
  20.             说明:       
  21.                 时间以毫秒为单位  
  22.   
  23.     -->  
  24.   
  25.     <rotate  
  26.         android:duration="5000"  
  27.         android:fromDegrees="0"  
  28.         android:pivotX="50%"  
  29.         android:pivotY="50%"  
  30.         android:toDegrees="1080" />  
  31.     <!--  
  32.        rotate 旋转动画效果     
  33.        浮点数型值:  
  34.             fromDegrees 属性为动画起始时物件的角度      
  35.             toDegrees   属性为动画结束时物件旋转的角度 可以大于360度     
  36.   
  37.             说明:  
  38.                      当角度为负数——表示逆时针旋转  
  39.                      当角度为正数——表示顺时针旋转                
  40.                      (负数from——to正数:顺时针旋转)     
  41.                      (负数from——to负数:逆时针旋转)   
  42.                      (正数from——to正数:顺时针旋转)   
  43.                      (正数from——to负数:逆时针旋转)         
  44.   
  45.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  46.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  47.                   
  48.             说明:        以上两个属性值 从0%-100%中取值  
  49.                          50%为物件的X或Y方向坐标上的中点位置  
  50.   
  51.         长整型值:  
  52.             duration  属性为动画持续时间  
  53.             说明:       时间以毫秒为单位  
  54.   
  55.     -->  
  56.   
  57.     <scale  
  58.         android:duration="5000"  
  59.         android:fromXScale="0.1"  
  60.         android:fromYScale="0.1"  
  61.         android:pivotX="50%"  
  62.         android:pivotY="50%"  
  63.         android:toXScale="3.0"  
  64.         android:toYScale="3.0" />  
  65.     <!--  
  66.       尺寸伸缩动画效果 scale  
  67.         
  68.       浮点型值:  
  69.            
  70.             fromXScale 属性为动画起始时 X坐标上的伸缩尺寸      
  71.             toXScale   属性为动画结束时 X坐标上的伸缩尺寸       
  72.           
  73.             fromYScale 属性为动画起始时Y坐标上的伸缩尺寸      
  74.             toYScale   属性为动画结束时Y坐标上的伸缩尺寸      
  75.           
  76.             说明:  
  77.                  以上四种属性值      
  78.       
  79.                     0.0表示收缩到没有   
  80.                     1.0表示正常无伸缩       
  81.                     值小于1.0表示收缩    
  82.                     值大于1.0表示放大  
  83.           
  84.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  85.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  86.           
  87.             说明:  
  88.                     以上两个属性值 从0%-100%中取值  
  89.                     50%为物件的X或Y方向坐标上的中点位置  
  90.           
  91.         长整型值:  
  92.             duration  属性为动画持续时间  
  93.             说明:   时间以毫秒为单位  
  94.   
  95.         布尔型值:  
  96.             fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用  
  97.   
  98.     -->  
  99.   
  100.     <translate  
  101.         android:duration="5000"  
  102.         android:fromXDelta="-20"  
  103.         android:fromYDelta="-20"  
  104.         android:toXDelta="300"  
  105.         android:toYDelta="300" />  
  106.     <!--  
  107.          translate 位置转移动画效果  
  108.         整型值:  
  109.             fromXDelta 属性为动画起始时 X坐标上的位置      
  110.             toXDelta   属性为动画结束时 X坐标上的位置  
  111.             fromYDelta 属性为动画起始时 Y坐标上的位置  
  112.             toYDelta   属性为动画结束时 Y坐标上的位置  
  113.             注意:  
  114.                      没有指定fromXType toXType fromYType toYType 时候,  
  115.                      默认是以自己为相对参照物               
  116.         长整型值:  
  117.             duration  属性为动画持续时间  
  118.             说明:   时间以毫秒为单位  
  119.   
  120.     -->  
  121.   
  122. </set>  

Android的animation由四种类型组成

alpha

渐变透明度动画效果

scale

渐变尺寸伸缩动画效果

translate

画面转换位置移动动画效果

rotate

画面转移旋转动画效果


在main.xml文件中定义一个按钮和一个ImageView:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     >  
  7.     <Button   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/button"  
  11.         android:text="重播"/>  
  12.     <ImageView   
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_gravity="center_vertical"  
  16.         android:src="@drawable/pic"  
  17.         android:id="@+id/image"/>  
  18.   
  19. </LinearLayout>  
button按钮用来重播,imageView用来显示那个动画效果的!

最后是java代码,MyAnimation.java:

[java]
  1. package cn.csdn.anim;  
  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.Animation;  
  8. import android.view.animation.AnimationUtils;  
  9. import android.widget.Button;  
  10. import android.widget.ImageView;  
  11.   
  12. public class MyAnimation extends Activity implements OnClickListener {  
  13.     private ImageView image = null;  
  14.     private Button button = null;  
  15.     private Animation animation;  
  16.   
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         init();  
  21.     }  
  22.   
  23.     private void init() {  
  24.         image = (ImageView) this.findViewById(R.id.image);  
  25.         button = (Button) findViewById(R.id.button);  
  26.         button.setOnClickListener(this);  
  27.         animation = AnimationUtils.loadAnimation(this, R.anim.tween);// 使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件   
  28.         image.startAnimation(animation);// 开始动画播出   
  29.     }  
  30.   
  31.     @Override  
  32.     public void onClick(View v) {  
  33.         image.startAnimation(animation);// 开始动画播出   
  34.     }  
  35. }  
是不是很简单!!

alpha

渐变透明度动画效果

scale

渐变尺寸伸缩动画效果

translate

画面转换位置移动动画效果

rotate

画面转移旋转动画效果




原文:http://www.linuxidc.com/Linux/2012-01/51449.htm

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值