Android中启动动画源码讲解

来源:互联网 发布:linux eclipse安装失败 编辑:程序博客网 时间:2024/06/08 08:55

首先是主要代码 的实现 ,去调用 各个xml 配置文件:

MainActivity:

[java] view plaincopy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ImageView imageView;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.         //获取显示控件ID        
  9.         imageView = (ImageView) findViewById(R.id.imageView1);  
  10.     }  
  11.   
  12.     @Override  
  13.     public boolean onCreateOptionsMenu(Menu menu) {  
  14.         // Inflate the menu; this adds items to the action bar if it is present.  
  15.         getMenuInflater().inflate(R.menu.main, menu);  
  16.         return true;  
  17.     }  
  18.       
  19.   
  20.     public void animImpl(View v){  
  21.       
  22.         //调用 动画的配置文件      
  23.         Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_demo);  
  24.         imageView.startAnimation(animation);  
  25.     }  
  26.       
  27.     //旋转动画  
  28.     public void rotate(){  
  29.         Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_demo);  
  30.         imageView.startAnimation(animation);  
  31.     }  
  32.       
  33.     //缩放动画  
  34.     public void scaleImpl(){  
  35.         Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_demo);  
  36.         imageView.startAnimation(animation);  
  37.     }  
  38.       
  39.       
  40.     //移动效果  
  41.     public void translateImpl(){  
  42.         //XML文件  
  43.     /*  Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_demo); 
  44.      
  45.         animation.setRepeatCount(Animation.INFINITE);*/  
  46.       
  47.       /* 第一种  
  48.        * imageView.setAnimation(animation); 
  49.         animation.start();*/  
  50.         //第二种  
  51. //      imageView.startAnimation(animation);  
  52.           
  53.         //Java代码  
  54.         TranslateAnimation translateAnimation = new TranslateAnimation(020000);  
  55.         translateAnimation.setDuration(2000);  
  56.         imageView.startAnimation(translateAnimation);  
  57.     }  
  58.     //缩放效果  
  59. }  

 

alpha_demo.xml

[java] view plaincopy
  1. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.1"  
  5.     android:duration="2000"/>  
  6.   
  7.  <!--   
  8.  fromAlpha :起始透明度  
  9.  toAlpha:结束透明度  
  10.    
  11.  1.0表示完全不透明  
  12.  0.0表示完全透明  
  13.   -->  


rotate_demo.xml

[java] view plaincopy
  1. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromDegrees="0"  
  4.     android:toDegrees="360"  
  5.     android:duration="1000"  
  6.     android:repeatCount="1"  
  7.     android:repeatMode="reverse"/>  
  8.   
  9. <!--   
  10. fromDegrees:表示旋转的起始角度  
  11. toDegrees:表示旋转的结束角度  
  12. repeatCount:旋转的次数  默认值是0 代表旋转1次  如果值是repeatCount=4 旋转5次  
  13. repeatCount=-1 或者infinite 循环了  
  14.   
  15.   
  16. repeatMode: 默认值:restart 模式要想其作用   repeatCount值必须大于0 或者是-1  
  17.            "reverse" 反转:  
  18.  -->  


scale_demo.xml

[java] view plaincopy
  1. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_interpolator"  
  3.     android:fromXScale="0.2"  
  4.     android:toXScale="1.5"  
  5.     android:fromYScale="0.2"  
  6.     android:toYScale="1.5"  
  7.     android:pivotX="50%"  
  8.     android:pivotY="50%"  
  9.     android:duration="2000"/>  
  10.   
  11. <!--   
  12. fromXScale:表示沿着x轴缩放的起始比例  
  13. toXScale:表示沿着x轴缩放的结束比例  
  14.   
  15. fromYScale:表示沿着y轴缩放的起始比例  
  16. toYScale:表示沿着y轴缩放的结束比例  
  17.   
  18. 图片中心点:  
  19.   android:pivotX="50%"   
  20.     android:pivotY="50%"  
  21.   
  22.  -->  


translate_demo.xml

[java] view plaincopy
  1. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromXDelta="0"  
  4.     android:toXDelta="320"  
  5.     android:fromYDelta="0"  
  6.     android:toYDelta="0"  
  7.     android:duration="2000"/>   
  8.       
  9. <!--   
  10.   android:interpolator 动画的渲染器  
  11.   1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速  
  12.   2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速  
  13.   3、accelerate_decelerate_interpolator(动画加速减速器)  
  14.            中间位置分层:  使动画在开始的时候 最慢,然后逐渐加速  
  15.              
  16.                                                   使动画在开始的时候 最快,然后逐渐减速  结束的位置最慢  
  17.  fromXDelta  动画起始位置的横坐标  
  18.  toXDelta    动画起结束位置的横坐标  
  19.    
  20.    
  21.  fromYDelta  动画起始位置的纵坐标  
  22.  toYDelta   动画结束位置的纵坐标  
  23.    
  24.  duration 动画的持续时间  
  25.  -->  

activity_main.xml

[java] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentRight="true"  
  17.         android:layout_alignParentTop="true"  
  18.         android:onClick="animImpl"  
  19.         android:text="@string/text_translate" />  
  20.   
  21.     <ImageView  
  22.         android:id="@+id/imageView1"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignLeft="@+id/button1"  
  26.         android:layout_below="@+id/button1"  
  27.         android:layout_marginTop="36dp"  
  28.         android:src="@drawable/ic_launcher" />  
  29.   
  30. </RelativeLayout>  
原创粉丝点击