Android中两种使用Animation的方法

来源:互联网 发布:99宿舍下载 软件 编辑:程序博客网 时间:2024/05/18 03:38

Android中,分别可以在xml中定义Animation,也可以在程序代码中定义,下面的小例子是利用RotateAnimation简单展示一下两种方法的用法,对于其他动画,如ScaleAnimation,AlphaAnimation,原理是一样的。

 

方法一:在xml中定义动画:

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.           
  4. <rotate   
  5.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  6.         android:fromDegrees="0"   
  7.         android:toDegrees="+360"  
  8.         android:duration="3000" />  
  9.           
  10. <!-- rotate 旋转动画效果  
  11.        属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化  
  12.         fromDegrees 属性为动画起始时物件的角度      
  13.         toDegrees   属性为动画结束时物件旋转的角度,+代表顺时针  
  14.         duration  属性为动画持续时间,以毫秒为单位  
  15. -->  
  16. </set>  

 

使用动画的Java代码,程序的效果是点击按钮,TextView旋转一周:

Java代码 
  1. package com.ray.animation;  
  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.TextView;  
  11.   
  12. public class TestAnimation extends Activity implements OnClickListener{  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Button btn = (Button)findViewById(R.id.Button01);  
  17.         btn.setOnClickListener(this);       
  18.     }  
  19.   
  20.     @Override  
  21.     public void onClick(View v) {  
  22.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);  
  23.         findViewById(R.id.TextView01).startAnimation(anim);  
  24.     }  
  25. }  

 

 方法二:直接在代码中定义动画(效果跟方法一类似):

Java代码 
  1. package com.ray.animation;  
  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.AccelerateDecelerateInterpolator;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.RotateAnimation;  
  10. import android.widget.Button;  
  11.   
  12. public class TestAnimation extends Activity implements OnClickListener{  
  13.   
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         Button btn = (Button)findViewById(R.id.Button);  
  18.         btn.setOnClickListener(this);       
  19.     }  
  20.   
  21.     public void onClick(View v) {  
  22.         Animation anim = null;  
  23.         anim = new RotateAnimation(0.0f,+360.0f);  
  24.         anim.setInterpolator(new AccelerateDecelerateInterpolator());  
  25.         anim.setDuration(3000);  
  26.         findViewById(R.id.TextView01).startAnimation(anim);   
  27.     }  
  28. }  

 

原文:http://rayleung.javaeye.com/blog/413842

 

原创粉丝点击