分享

Android 动画之补间动画

 greenyun588 2013-09-14

    补间动画是指定开始和结束的图像状态,自动生成需要显示的过度图像的动画。补间动画又分为四种:移动,缩放,旋转,通明度。下面以移动补间动画来做简单说明,效果是把一个ImageView从左上角,向右下方向移动,然后返回到起始点,中间对动画状态进行监听,效果如图:




 


    下面简述其主要步骤:


 


    1、定义动画文件:


Java代码 复制代码 收藏代码
  1. <translate xmlns:android="http://schemas./apk/res/android"    
  2.     android:duration="5000"    
  3.     android:fromXDelta="0"    
  4.     android:fromYDelta="0"    
  5.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"    
  6.     android:toXDelta="200"    
  7.     android:toYDelta="300" />    

 说明:



  • android:interpolator:动画渲染器,有三种渲染器可以设
    置:accelerate_decelerate_interpolator,accelerate_interpolator,decelerate_interpolator,
    它们分别对应的效果是:开始加速中间减速,一直加速,一直减速。

  • fromXDelta;动画起始位置的X坐标;

  • fromYDelta:动画起始位置的Y坐标;

  • toXDelta:动画结束位置的X坐标;

  • toYDelta:动画结束位置的Y坐标;

  • duration:动画持续时间,单位毫秒。


    2、加载并启动动画:


Java代码 复制代码 收藏代码
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.util.Log;  
  4. import android.view.animation.Animation;  
  5. import android.view.animation.AnimationUtils;  
  6. import android.view.animation.Animation.AnimationListener;  
  7. import android.widget.ImageView;  
  8.   
  9. public class TranslateActivity extends Activity implements AnimationListener {  
  10.       
  11.     private static final String TAG = "Translate";  
  12.       
  13.     private ImageView imageView;  
  14.     private Animation translateAnimation;  
  15.       
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         imageView = (ImageView) findViewById(R.id.imageView1);  
  23.           
  24.         // 装载动画文件  
  25.         translateAnimation = AnimationUtils.loadAnimation(this, R.xml.translate);  
  26.           
  27.         // 设置动画监听器  
  28.         translateAnimation.setAnimationListener(this);  
  29.           
  30.         // 设置重复次数  
  31.         translateAnimation.setRepeatCount(1);  
  32.           
  33.         // 设置重复模式  
  34.         translateAnimation.setRepeatMode(Animation.REVERSE);  
  35.           
  36.         // 启动动画  
  37. //        imageView.setAnimation(translateAnimation);  
  38. //        translateAnimation.start();  
  39.           
  40.         imageView.startAnimation(translateAnimation);  
  41.           
  42.     }  
  43.   
  44.     @Override  
  45.     public void onAnimationEnd(Animation animation) {  
  46.         Log.i(TAG, "onAnimationEnd");  
  47.     }  
  48.   
  49.     @Override  
  50.     public void onAnimationRepeat(Animation animation) {  
  51.         Log.i(TAG, "onAnimationRepeat");  
  52.     }  
  53.   
  54.     @Override  
  55.     public void onAnimationStart(Animation animation) {  
  56.         Log.i(TAG, "onAnimationStart");  
  57.     }  
  58.       
  59. }  

    修改上面的某些代码,猜想效果,并和实际效果作对比,有时会发现很有趣的现象!:)


 


    缩放动画的XML示例代码:


 


Java代码 复制代码 收藏代码
  1. <set xmlns:android="http://schemas./apk/res/android"  
  2.     android:shareInterpolator="false" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fromXScale="1"  
  7.         android:fromYScale="0.1"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:startOffset="100"  
  11.         android:toXScale="1"  
  12.         android:toYScale="1.0" />  
  13.   
  14. </set>  

 


    旋转动画的XML示例代码如下:


Java代码 复制代码 收藏代码
  1. <rotate xmlns:android="http://schemas./apk/res/android"  
  2.     android:duration="10000"  
  3.     android:fromDegrees="0"  
  4.     android:interpolator="@anim/linear_interpolator"  
  5.     android:pivotX="200%"  
  6.     android:pivotY="300%"  
  7.     android:repeatCount="infinite"  
  8.     android:repeatMode="restart"  
  9.     android:toDegrees="360" />  

 


    3、多说一句:


    使用代码同样可以实现从XML加载动画一样的效果,有兴趣的话,可以试试看!:)


 


 


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多