Android 动画效果


本来以为动画都会了,谁知道今天写代码的时候忘记了,所以把动画总结了一遍,在res目录下新建一个anim文件夹,然后把动画的xml文件写在这个文件夹下面。

直接上图上代码 

CartoonActivity.java

  1. package rw.cartoon;  
  2.   
  3. import Android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class CartoonActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     private Button TraslateButton,RoteButton,ScaleButton,AlphaButton;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         TraslateButton=(Button)findViewById(R.id.translatebutton3);  
  18.         RoteButton=(Button)findViewById(R.id.rotatebutton4);  
  19.         ScaleButton=(Button)findViewById(R.id.scalebutton2);  
  20.         AlphaButton=(Button)findViewById(R.id.alplebutton1);  
  21.           
  22.         TraslateButton.setOnClickListener(new ButtonListener());  
  23.         RoteButton.setOnClickListener(new ButtonListener());  
  24.         AlphaButton.setOnClickListener(new ButtonListener());  
  25.         ScaleButton.setOnClickListener(new ButtonListener());  
  26.     }  
  27.     private class ButtonListener implements OnClickListener{  
  28.   
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             // TODO Auto-generated method stub   
  32.             Intent intent=new Intent();  
  33.             switch (v.getId()) {  
  34.             case R.id.alplebutton1:  
  35.                   
  36.                 intent.setClass(getApplicationContext(), alpha.class);  
  37.                 CartoonActivity.this.startActivity(intent);  
  38.                 break;  
  39.            case R.id.rotatebutton4:  
  40.                
  41.                 intent.setClass(getApplicationContext(), rotate.class);  
  42.                 CartoonActivity.this.startActivity(intent);  
  43.               break;  
  44.           case R.id.scalebutton2:  
  45.               
  46.                 intent.setClass(getApplicationContext(), sacle.class);  
  47.                 CartoonActivity.this.startActivity(intent);  
  48.                break;  
  49.           case R.id.translatebutton3:  
  50.           
  51.                 intent.setClass(getApplicationContext(), translate.class);  
  52.                 CartoonActivity.this.startActivity(intent);  
  53.                 break;  
  54.             default:  
  55.                 break;  
  56.             }  
  57.         }  
  58.           
  59.     }  
  60. }  

alpha.java

  1. package rw.cartoon;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.ImageView;  
  8.   
  9. public class alpha extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     ImageView imageVie;  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.test);  
  15.         imageVie=(ImageView)findViewById(R.id.imageView1);  
  16.       Animation animation=AnimationUtils.loadAnimation(alpha.this, R.anim.alpha);  
  17.       imageVie.startAnimation(animation);  
  18.         
  19.     }  
  20.       
  21. }  

rotate.java

  1. package rw.cartoon;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.widget.ImageView;  
  9.   
  10. public class rotate extends Activity {  
  11.     ImageView imageVie;  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.test);  
  15.         imageVie=(ImageView)findViewById(R.id.imageView1);  
  16.       Animation animation=AnimationUtils.loadAnimation(rotate.this, R.anim.rotate);  
  17.       imageVie.startAnimation(animation);  
  18.         
  19.     }  
  20. }  

scale.java

  1. package rw.cartoon;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.ImageView;  
  8.   
  9. public class sacle extends Activity {  
  10.     ImageView imageVie;  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.test);  
  14.         imageVie=(ImageView)findViewById(R.id.imageView1);  
  15.       Animation animation=AnimationUtils.loadAnimation(sacle.this, R.anim.scale);  
  16.       imageVie.startAnimation(animation);  
  17.         
  18.     }  
  19. }  

translate.java

  1. package rw.cartoon;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.widget.ImageView;  
  9.   
  10. public class translate extends Activity {ImageView imageVie;  
  11. public void onCreate(Bundle savedInstanceState) {  
  12.     super.onCreate(savedInstanceState);  
  13.     setContentView(R.layout.test);  
  14.     imageVie=(ImageView)findViewById(R.id.imageView1);  
  15.   Animation animation=AnimationUtils.loadAnimation(translate.this, R.anim.translate);  
  16.   imageVie.startAnimation(animation);  
  17.     
  18. }}  

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. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/alplebutton1" android:text="alpha 渐变透明度"></Button>  
  13. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/scalebutton2" android:text="scale 渐变尺寸伸缩"></Button>  
  14. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/translatebutton3" android:text="translate 画面转换位置移动"></Button>  
  15. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rotatebutton4" android:text="rotate 画面转移旋转动"></Button>  
  16. </LinearLayout>  

test.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent" android:weightSum="1">  
  7.     <ImageView android:src="@drawable/ttt2" android:id="@+id/imageView1" android:layout_width="218dp" android:layout_height="219dp"></ImageView>  
  8.       
  9. </LinearLayout>  

alpha.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="1.0"  
  5. android:toAlpha="0.1"  
  6. android:duration="3000"  
  7. />  
  8. <!-- 透明度控制动画效果 alpha  
  9.         浮点型值:  
  10.             fromAlpha 属性为动画起始时透明度  
  11.             toAlpha   属性为动画结束时透明度  
  12.             说明:   
  13.                 0.0表示完全透明  
  14.                 1.0表示完全不透明  
  15.             以上值取0.0-1.0之间的float数据类型的数字  
  16.         长整型值:  
  17.             duration  属性为动画持续时间  
  18.             说明:       
  19.                 时间以毫秒为单位  
  20. -->  
  21. </set>  

rotate.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <rotate   
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  5.         android:fromDegrees="0"   
  6.         android:toDegrees="+350"           
  7.         android:pivotX="50%"   
  8.         android:pivotY="50%"       
  9.         android:duration="3000" />    
  10. <!-- rotate 旋转动画效果  
  11.        属性:interpolator 指定一个动画的插入器  
  12.              在我试验过程中,使用android.res.anim中的资源时候发现  
  13.              有三种动画插入器:  
  14.                 accelerate_decelerate_interpolator   加速-减速 动画插入器  
  15.                 accelerate_interpolator               加速-动画插入器  
  16.                 decelerate_interpolator               减速- 动画插入器  
  17.              其他的属于特定的动画效果  
  18.   
  19.        浮点数型值:  
  20.             fromDegrees 属性为动画起始时物件的角度      
  21.             toDegrees   属性为动画结束时物件旋转的角度 可以大于360度     
  22.   
  23.   
  24.             说明:  
  25.                      当角度为负数——表示逆时针旋转  
  26.                      当角度为正数——表示顺时针旋转                
  27.                      (负数from——to正数:顺时针旋转)     
  28.                      (负数from——to负数:逆时针旋转)   
  29.                      (正数from——to正数:顺时针旋转)   
  30.                      (正数from——to负数:逆时针旋转)         
  31.   
  32.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  33.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  34.   
  35.             说明:        以上两个属性值 从0%-100%中取值  
  36.                          50%为物件的X或Y方向坐标上的中点位置  
  37.   
  38.         长整型值:  
  39.             duration  属性为动画持续时间  
  40.             说明:       时间以毫秒为单位  
  41. -->  
  42. </set>  

scale.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5.    <scale    
  6.   
  7.           android:interpolator=  
  8.   
  9.                      "@android:anim/accelerate_decelerate_interpolator"  
  10.   
  11.           android:fromXScale="0.0"  
  12.   
  13.           android:toXScale="1.4"  
  14.   
  15.           android:fromYScale="0.0"  
  16.   
  17.           android:toYScale="1.4"  
  18.   
  19.           android:pivotX="50%"  
  20.   
  21.           android:pivotY="50%"  
  22.   
  23.           android:fillAfter="false"  
  24.   
  25.           android:duration="700" />  
  26.   
  27. </set>  
  28.   
  29. <!-- 尺寸伸缩动画效果 scale  
  30.   
  31.        属性:interpolator 指定一个动画的插入器  
  32.   
  33.         在我试验过程中,使用android.res.anim中的资源时候发现  
  34.   
  35.         有三种动画插入器:  
  36.   
  37.             accelerate_decelerate_interpolator  加速-减速 动画插入器  
  38.   
  39.             accelerate_interpolator        加速-动画插入器  
  40.   
  41.             decelerate_interpolator        减速- 动画插入器  
  42.   
  43.         其他的属于特定的动画效果  
  44.   
  45.       浮点型值:  
  46.   
  47.            
  48.   
  49.             fromXScale 属性为动画起始时 X坐标上的伸缩尺寸      
  50.   
  51.             toXScale   属性为动画结束时 X坐标上的伸缩尺寸       
  52.   
  53.           
  54.   
  55.             fromYScale 属性为动画起始时Y坐标上的伸缩尺寸      
  56.   
  57.             toYScale   属性为动画结束时Y坐标上的伸缩尺寸      
  58.   
  59.           
  60.   
  61.             说明:  
  62.   
  63.                  以上四种属性值      
  64.   
  65.       
  66.   
  67.                     0.0表示收缩到没有   
  68.   
  69.                     1.0表示正常无伸缩       
  70.   
  71.                     值小于1.0表示收缩    
  72.   
  73.                     值大于1.0表示放大  
  74.   
  75.           
  76.   
  77.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  78.   
  79.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  80.   
  81.           
  82.   
  83.             说明:  
  84.   
  85.                     以上两个属性值 从0%-100%中取值  
  86.   
  87.                     50%为物件的X或Y方向坐标上的中点位置  
  88.   
  89.           
  90.   
  91.         长整型值:  
  92.   
  93.             duration  属性为动画持续时间  
  94.   
  95.             说明:   时间以毫秒为单位  
  96.   
  97.   
  98.   
  99.         布尔型值:  
  100.   
  101.             fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用  
  102.   
  103. -->  

translate.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   
  5. <translate  
  6.   
  7. android:fromXDelta="30"  
  8.   
  9. android:toXDelta="-80"  
  10.   
  11. android:fromYDelta="30"  
  12.   
  13. android:toYDelta="300"  
  14.   
  15. android:duration="2000"  
  16.   
  17. />  
  18.   
  19. <!-- translate 位置转移动画效果  
  20.   
  21.         整型值:  
  22.   
  23.             fromXDelta 属性为动画起始时 X坐标上的位置      
  24.   
  25.             toXDelta   属性为动画结束时 X坐标上的位置  
  26.   
  27.             fromYDelta 属性为动画起始时 Y坐标上的位置  
  28.   
  29.             toYDelta   属性为动画结束时 Y坐标上的位置  
  30.   
  31.             注意:  
  32.   
  33.                      没有指定fromXType toXType fromYType toYType 时候,  
  34.   
  35.                      默认是以自己为相对参照物               
  36.   
  37.         长整型值:  
  38.   
  39.             duration  属性为动画持续时间  
  40.   
  41.             说明:   时间以毫秒为单位  
  42.   
  43. -->  
  44.   
  45. </set>  

 

相关内容