Android Animation 动画详解

Android Tweened Animation一共有四种

Alpha: 淡入淡出效果

Scale: 缩放效果

Rotate: 旋转效果

Translate:移动效果

使用Tweened Animations的步骤

1.创建一个AnimationSet对象

2.根据需要创建需要的Animation对象

3.根据软件动画的需要,为Animation对象设置相应的数据

4.将Animation对象添加到AnimationSet对象中

5.使控件对象开始执行AnimationSet

Alpha动画

  1.         AnimationSet as=new AnimationSet(true);  
  2. AlphaAnimation al=new AlphaAnimation(1,0);  
  3. //1代表完全不透明,0代表完全透明  
  4. al.setDuration(3000);  
  5. as.addAnimation(al);  
  6. iv.startAnimation(as);  

Rotate动画

  1.                 AnimationSet as=new AnimationSet(true);  
  2. RotateAnimation al=new RotateAnimation (0,720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);  
  3. //前两个参数参数旋转的角度,后面几个参数决定旋转的中心  
  4. //Animation.ABSOLUTE:绝对坐标  
  5. //Animation.RELATIVE_TO_PARENT:相对父控件  
  6. //Animation.RELATIVE_TO_SELF:相对自己  
  7. al.setDuration(3000);  
  8. as.addAnimation(al);  
  9. iv.startAnimation(as);  

Scale动画

  1. AnimationSet as=new AnimationSet(true);  
  2.             ScaleAnimation al=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  3.             //前四个参数是X从多大到多大,Y从多大到多大,后面的参数是缩放的中心点  
  4.             al.setDuration(3000);  
  5.             as.addAnimation(al);  
  6.             iv.startAnimation(as);  


Translate动画

  1. AnimationSet as=new AnimationSet(true);  
  2.             TranslateAnimation al=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);  
  3.             al.setDuration(3000);  
  4.             as.addAnimation(al);  
  5.             iv.startAnimation(as);  


Animation还有几个方法

setFillAfter(boolean fillAfter)

如果fillAfter的值为真的话,动画结束后,控件停留在执行后的状态

setFillBefore(boolean fillBefore)

如果fillBefore的值为真的话,动画结束后,控件停留在动画开始的状态

setStartOffset(long startOffset)

设置动画控件执行动画之前等待的时间

setRepeatCount(int repeatCount)

设置动画重复执行的次数


Animation也可以放在XML文件中,这样程序的可维护性提高了。在XML中写动画的步骤如下

1.在res文件夹下面新建一个名为anim的文件夹

2.创建xml文件,并首先加入set标签,改标签如下

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
  
</set>

3.在该标签当中加入rotate,alpha,scale或者translate标签

4.在代码当中使用AnimationUtils加载xml文件,并生成Animation对象

Alpha动画

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <alpha  
  5.         android:fromAlpha="1.0"  
  6.         android:toAlpha="0.0"  
  7.         android:startOffset="500"  
  8.         android:duration="2000"  
  9.             />  
  10.   
  11. </set></span>  
Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(a);

Scale动画

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <scale  
  5.         android:fromXScale="1.0"  
  6.         android:toXScale="0.0"  
  7.         android:fromYScale="1.0"  
  8.         android:toYScale="0.0"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:duration="2000"  
  12.     />  
  13.   
  14. </set></span>  

Rotate动画
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <rotate  
  5.         android:fromDegrees="0"  
  6.         android:toDegrees="400"  
  7.         android:pivotX="50%"  
  8.         android:pivotY="50%"  
  9.         android:duration="3000"  
  10.     />  
  11.   
  12. </set></span>  

Translate动画
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <translate  
  5.         android:fromXDelta="50%"  
  6.         android:toXDelta="100%"  
  7.         android:fromYDelta="50%"  
  8.         android:toYDelta="100%"  
  9.         android:duration="3000"  
  10.     />  
  11.   
  12. </set></span>  

这里重点提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta

android:pivotX="50"使用绝对坐标

android:pivotX="50%"相对自己

android:pivotX="50%p"相对父控件

AnimationSet可以加入Animation,加入之后设置AnimationSet对加入的所有Animation都有效。

  1. AnimationSet anim=new AnimationSet(true);  
  2.             AlphaAnimation a=new AlphaAnimation(1,0);  
  3.             RotateAnimation ra=new RotateAnimation(0720, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);  
  4.             anim.addAnimation(a);  
  5.             anim.addAnimation(ra);  
  6.             anim.setDuration(3000);  
  7.             anim.setStartOffset(1000);  
  8.             iv.startAnimation(anim);  


可以再xml文件中定义多个Animation,这样多个Animation可以一起运行

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator"  
  4.     android:shareInterpolator="true"  
  5.     >  
  6.     <alpha  
  7.         android:fromAlpha="1.0"  
  8.         android:toAlpha="0.0"  
  9.         android:startOffset="500"  
  10.         android:duration="3000"  
  11.             />  
  12.     <rotate  
  13.         android:fromDegrees="0"  
  14.         android:toDegrees="400"  
  15.         android:pivotX="50%"  
  16.         android:pivotY="50%"  
  17.         android:duration="3000"  
  18.     />  
  19.   
  20. </set>  


Interpolator可以定义动画播放的速度

 AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速

     AccelerateInterpolator  在动画开始的地方速率改变比较慢,然后开始加速

   AnticipateInterpolator 开始的时候向后然后向前甩

   AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值

   BounceInterpolator   动画结束的时候弹起

   CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

   DecelerateInterpolator 在动画开始的地方快然后慢

     LinearInterpolator   以常量速率改变

     OvershootInterpolator    向前甩一定值后再回到原来位置

如果android定义的interpolators不符合你的效果也可以自定义interpolators



在xml文件中定义Interpolator

android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"

这样所有的Animation共用一个Interpolator。

在代码中用代码设置如下

anim.setInterpolator(new AccelerateInterpolator());

在new一个AnimationSet中传入true则所有的Animation共用Interpolator。



现在使ImageView中的图片可以动起来

1.在drawable-mdpi文件夹下加入图片,并加入一个xml文件,文件如下

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   <item android:drawable="@drawable/a" android:duration="500"/>   
  4.   <item android:drawable="@drawable/b" android:duration="500"/>   
  5.   <item android:drawable="@drawable/c" android:duration="500"/>   
  6.   <item android:drawable="@drawable/d" android:duration="500"/>   
  7.   
  8. </animation-list>  


2.代码如下

  1. iv.setBackgroundResource(R.drawable.anim);  
  2.             AnimationDrawable an=(AnimationDrawable)iv.getBackground();  
  3.             an.start();  


其实可以用一个线程加Handler来实现动画的,在线程中隔一定时间发送消息,更改ImageView的图片。

LayoutAnimationController可以控制一组控件按照规定显示,有两种方法来实现

1.下面以XML文件实现,先在res下新建anim文件夹,新建一个文件alpha.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator"  
  4.     android:shareInterpolator="true"  
  5.     >  
  6.     <alpha  
  7.         android:fromAlpha="0"  
  8.         android:toAlpha="1"  
  9.         android:duration="3000"  
  10.             />  
  11. </set>  


然后新建一个文件layoutanimation.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layoutAnimation  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:delay="0.5"  
  5.     android:animationOrder="normal"  
  6.     android:animation="@anim/alpha"  
  7.     />  


在listview中使用下面代码

  1. <ListView  
  2.         android:id="@+id/listView1"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"         
  5.  android:layoutAnimation="@anim/layoutanimation"/>  


   

这样就完成了

2.代码实现

  1.                 AlphaAnimation alpha=new AlphaAnimation(01);  
  2. alpha.setDuration(3000);  
  3. LayoutAnimationController lac=new LayoutAnimationController(alpha);  
  4. lac.setOrder(LayoutAnimationController.ORDER_NORMAL);  
  5. lv.setLayoutAnimation(lac);  


下面是显示的顺序

LayoutAnimationController.ORDER_NORMAL;    //顺序显示
  LayoutAnimationController.ORDER_REVERSE;//反显示
  LayoutAnimationController.ORDER_RANDOM//随机显示

通过AnimationListener可以监听Animation的运行过程

  1. AnimationSet as=new AnimationSet(true);  
  2.             RotateAnimation al=new RotateAnimation(0,-720,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);  
  3.             al.setDuration(3000);  
  4.             al.setAnimationListener(new AnimationListener(){  
  5.   
  6.                 public void onAnimationStart(Animation animation) {  
  7.                     // TODO Auto-generated method stub  
  8.                       
  9.                 }  
  10.   
  11.                 public void onAnimationEnd(Animation animation) {  
  12.                     // TODO Auto-generated method stub  
  13.                       
  14.                 }  
  15.   
  16.                 public void onAnimationRepeat(Animation animation) {  
  17.                     // TODO Auto-generated method stub  
  18.                       
  19.                 }  
  20.                   
  21.             });  
  22.             as.addAnimation(al);  
  23.             iv.startAnimation(as);  
有三个方法分别是Animation开始的时候调用,完成的时候调用,重复的时候调用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值