Android Tween Animations(动画效果)的使用


程序功能:

程序中有四个按钮(Alpha, Scale, Rotate, Translate)和一个图片:

分别点击四个按钮,则图片和对应的按钮分别执行:淡入淡出、缩放、旋转、移动动画效果。

代码如下:

AppMain.java

  1. package lxy.litsoft;  
  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.AnimationSet;  
  10. import android.view.animation.RotateAnimation;  
  11. import android.view.animation.ScaleAnimation;  
  12. import android.view.animation.TranslateAnimation;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15.   
  16. public class AppMain extends Activity {  
  17.       
  18.     //声明对象   
  19.     private ImageView imageView;  
  20.     private Button btAlpha;  
  21.     private Button btScale;  
  22.     private Button btRotate;  
  23.     private Button btTranslate;  
  24.       
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.           
  29.         //实例化对象   
  30.         imageView = (ImageView)findViewById(R.id.imageView01);  
  31.           
  32.         btAlpha = (Button)findViewById(R.id.button01);  
  33.         btAlpha.setOnClickListener(new ButtonListener());  
  34.           
  35.         btScale = (Button)findViewById(R.id.button02);  
  36.         btScale.setOnClickListener(new ButtonListener());  
  37.           
  38.         btRotate = (Button)findViewById(R.id.button03);  
  39.         btRotate.setOnClickListener(new ButtonListener());  
  40.           
  41.         btTranslate = (Button)findViewById(R.id.button04);  
  42.         btTranslate.setOnClickListener(new ButtonListener());  
  43.     }  
  44.       
  45.     class ButtonListener implements OnClickListener{  
  46.   
  47.         public void onClick(View v) {  
  48.             if(v.getId() == R.id.button01){ //Alpha:淡入淡出效果   
  49.                 //创建AnimationSet对象   
  50.                 AnimationSet animationSet = new AnimationSet(true);   
  51.                 //从透明度1,到透明度0   
  52.                 AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);  
  53.                 //动画执行时间   
  54.                 alphaAnimation.setDuration(1000);     
  55.                 //把动画添加到动画集中   
  56.                 animationSet.addAnimation(alphaAnimation);    
  57.                 //把动画绑定控件   
  58.                 imageView.startAnimation(animationSet);                   
  59.                 btAlpha.startAnimation(animationSet);  
  60.                   
  61.             } else if(v.getId() == R.id.button02){  //Scale:缩放效果   
  62.                 AnimationSet animationSet = new AnimationSet(true);  
  63.                 ScaleAnimation scaleAnimation = new ScaleAnimation(1,1,0,1,  
  64.                         Animation.RELATIVE_TO_SELF,0.5f,  
  65.                         Animation.RELATIVE_TO_SELF,0.5f);  
  66.                 scaleAnimation.setDuration(1000);  
  67.                 animationSet.addAnimation(scaleAnimation);  
  68.                 imageView.startAnimation(animationSet);  
  69.                 btScale.startAnimation(animationSet);  
  70.                   
  71.             } else if(v.getId() == R.id.button03){  //Rotate:旋转效果   
  72.                 AnimationSet animationSet = new AnimationSet(true);  
  73.                 RotateAnimation rotateAnimation = new RotateAnimation(0f,360f,//旋转角度的变化范围   
  74.                         Animation.RELATIVE_TO_SELF, 0.5f,       //旋转中心X的位置确定   
  75.                         Animation.RELATIVE_TO_SELF,0.5f);       //旋转中心Y的位置确定   
  76.                 rotateAnimation.setDuration(1000);  
  77.                 animationSet.addAnimation(rotateAnimation);  
  78. //              imageView.startAnimation(animationSet);   
  79.                 btRotate.startAnimation(animationSet);  
  80.             }else if(v.getId() == R.id.button04){   //Translate:移动效果   
  81.                 AnimationSet animationSet = new AnimationSet(true);  
  82.                 TranslateAnimation translateAnimation = new TranslateAnimation(  
  83.                         Animation.RELATIVE_TO_SELF, 1,  
  84.                         Animation.RELATIVE_TO_SELF,0,  
  85.                         Animation.RELATIVE_TO_SELF,1,  
  86.                         Animation.RELATIVE_TO_SELF,0);  
  87.                 translateAnimation.setDuration(1000);  
  88.                 animationSet.addAnimation(translateAnimation);  
  89.                 imageView.startAnimation(animationSet);  
  90.                 btTranslate.startAnimation(animationSet);  
  91.             }  
  92.         }  
  93.     }  
  94. }  
main.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.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello" />  
  11.        
  12.     <LinearLayout  
  13.         android:orientation="vertical"  
  14.         android:layout_weight="1"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="fill_parent" >  
  17.         <ImageView  
  18.             android:id="@+id/imageView01"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_marginTop="150dip"  
  22.             android:layout_marginLeft="200dip"  
  23.             android:src="@drawable/icon"></ImageView>  
  24.           
  25.     </LinearLayout>     
  26.     <LinearLayout  
  27.         android:orientation="vertical"  
  28.         android:layout_weight="1"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="fill_parent" >  
  31.         <Button  
  32.             android:id="@+id/button01"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="Alpha"></Button>  
  36.         <Button  
  37.             android:id="@+id/button02"  
  38.             android:layout_width="fill_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:text="Scale"></Button>  
  41.         <Button  
  42.             android:id="@+id/button03"  
  43.             android:layout_width="fill_parent"  
  44.             android:layout_height="wrap_content"  
  45.             android:text="Rotate"></Button>  
  46.         <Button  
  47.             android:id="@+id/button04"  
  48.             android:layout_width="fill_parent"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="Translate"></Button>  
  51.     </LinearLayout>  
  52. </LinearLayout>  
另外:

Tween Animations的通用属性
1.setDuration(long durationMills);//设置动画执行事件(单位:毫秒)
2.setFillAfter(boolean fillAfter);//如果fillAfter的值为true,则动画执行后,
控件将停留在执行结果的状态
3.setFillBefore(boolen fillBefore);//如果fillBefore的值为true,则动画执行后,
控件将回到动画执行之前的状态
4.setStartOffSet(long startOffSet);//设置动画执行之前的等待时间
5.setRepeatCount(int repeatCount);//设置动画重复执行的次数

相关内容