Android动画学习-视图动画&属性动画(一)

  参考文章:http://blog.csdn.net/linmiansheng/article/details/18676845

View Animation提供了一些在View上简单的动画效果,包括Tranlsate(平移),Rotate(旋转),Scale(缩放)和 Alpha(透明)这几种动画效果。


 View Animation的实现方式有两种:一是在XML中定义;二是在Java 代码中定义。


XML 定义

1. Scale(缩放)

首先我们要先在 res/amin/中创建一个定义动画的xml文件,就叫transform.xml(这里要注意,关于View Animation的动画效果,都是要放在 /res/anim 路径下的) 代码如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"  
  4.     android:fromXScale="1.0"  
  5.     android:fromYScale="1.0"  
  6.     android:pivotX="50%"  
  7.     android:pivotY="50%"  
  8.     android:toXScale="0.5"  
  9.     android:toYScale="0.5"  
  10.     android:repeatCount="1"  
  11.     android:repeatMode="reverse" />  
关于scale的参数有:

fromXScale, fromYScale:X/Y 方向开始的比例(1.0 就是原来比例的1.0倍)

toXScale, toYScale:X/Y 方向最终的比例

pivotX, pivotY:缩放的轴(50则是在其父容器50%的位置,,加上百分号,50%,则是相对于其本身,也就是绕其中心)

2. Translate (平移)

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"  
  4.     android:fromXDelta="0"      
  5.     android:toXDelta="100"  
  6.     android:fillAfter="true" />  
关于translate的参数有:

fromXDelta : 平移开始的位置

toXDelta:      平移结束的位置

3. Rotate (旋转)

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"  
  4.     android:fromDegrees="-180"  
  5.     android:pivotX="50%"  
  6.     android:pivotY="50%"  
  7.     android:repeatCount="1"  
  8.     android:repeatMode="reverse"  
  9.     android:toDegrees="0" />  
fromDegrees : 开始的角度 

toDegrees    : 结束的角度

pivotX, pivotY:旋转的轴

4. Alpha (透明 )

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"  
  4.     android:duration="1000"  
  5.     android:repeatCount="1"  
  6.     android:repeatMode="reverse"  
  7.     android:toAlpha="0" />  
fromAlpha: 开始时的透明度(1.0 完全不透明, 0 完全透明,等于不见了)

toAlpha:结束时的透明度

5. 共有的属性

每个动画我们都必须通过Duration来设置其时长。

duration:动画的时长 

而repeatCount和repeatMode等则不是必须的,但可以应用到每个动画效果上。

repeatCount:重复的次数

repeatMode:reverse (从结束位置反过来进行动画),restart(在开始位置重新开始动画)。

fillAfter :当为true的时候,动画会停在结束的那一刻。

fillBefore:当为true的时候,动画结束后,当前View会停留在开始的那一刻。

等等等,就不多说了。

6.在Java中调用 

当把这些参数都定义好之后,那么就可以在Java中调用我们定义好的这个动画文件了,具体代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (cbFromXml.isChecked()) {  
  2.     animation1 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform1);  
  3.     animation2 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform2);  
  4.     animation3 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform3);  
  5.     animation4 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.transform4);  
  6. }  

把定义的动画效果通过 AnimationUtils.loadAnimation这个方法拿出来,将通过view的startAnimation函数,将animation设置给view,并开始运行。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public void onClick(View v) {  
  2.     switch (v.getId()) {  
  3.     case R.id.button1:  
  4.         animation1.setStartOffset(0);  
  5.         v.startAnimation(animation1);  
  6.         break;  
这样,我们就可以在上面的效果图中看到各种的效果了。

当然,这只是一种效果的动画,那我们如果要将各种效果同时进行,或者按顺序来进行,怎么办呢?

我们可以通过定义Set,来实现这个效果:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fillAfter="true" >  
  4.   
  5.     <rotate  
  6.         android:duration="2000"  
  7.         android:fromDegrees="-180"  
  8.         android:pivotX="50%"  
  9.         android:pivotY="50%"  
  10.         android:toDegrees="0" />  
  11.   
  12.     <scale  
  13.         android:duration="2000"  
  14.         android:fromXScale="0.5"  
  15.         android:fromYScale="0.5"  
  16.         android:pivotX="50%"  
  17.         android:pivotY="50%"  
  18.         android:toXScale="2.0"  
  19.         android:startOffset="2000"  
  20.         android:toYScale="2.0" />  
  21.   </set>  

     而在Java中,调用方法是一样的。在上面的设置中,每个动画效果都有一个startOffset参数,是设置动画开始的偏移量的,如果我们要动画顺序执行,那我们就必须设置各个效果的偏移时间,如上面,rotate是马上执行的,而scale效果会在2秒后才执行,刚好在rotate效果执行之后。

 

Java中定义


很简单
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. animation1 = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,50,50); //Scale Animation  
  2. animation1.setDuration(DURATION);  
  3. animation1.setRepeatCount(1);  
  4. animation1.setInterpolator(interpolator);  
  5. animation1.setRepeatMode(Animation.REVERSE);  
  6.   
  7. animation2 = new RotateAnimation(01805050); //Rotate Animation  
  8. animation2.setDuration(DURATION);             
  9. animation2.setRepeatCount(1);  
  10. animation2.setInterpolator(interpolator);  
  11. animation2.setRepeatMode(Animation.REVERSE);  
  12.   
  13. animation3 = new TranslateAnimation(010000); //Translate Animation  
  14. animation3.setDuration(DURATION);             
  15. animation3.setRepeatCount(1);  
  16. animation3.setInterpolator(interpolator);  
  17. animation3.setRepeatMode(Animation.REVERSE);  
  18.   
  19. animation4 = new AlphaAnimation(1.0f, 0f); //Alpha Animation  
  20. animation4.setDuration(DURATION);             
  21. animation4.setRepeatCount(1);  
  22. animation4.setInterpolator(interpolator);  
  23. animation4.setRepeatMode(Animation.REVERSE);  
  24.   
  25. animationSet = new AnimationSet(false);   //Animation Set  
  26. animationSet.addAnimation(animation1);  
  27. animationSet.addAnimation(animation2);  
  28. animationSet.addAnimation(animation3);  

设置的各种属性其实是一一对应于我们在XML中定义的各种属性的,定义好了之后,其实就跟XML中定义好并加载到Java代码中之后一样,直接调用View.startAnimation函数就可以了,在这里就不详细说了。

总结

View Animation 其实是非常简单的,而且很容易用,一般如果不需要太复杂的效果,其实是够用的了。
那么它到底有什么优缺点呢,为什么在3.0后还要再加入Property Animation呢?
1)View Animation, 顾名思义,只能用在View上面,没法用在非View的对象上,比如我们自定义出来的对象
2)只是提供了有限的几种效果,比如旋转,平移,缩放和透明,如果我们要实现其他的效果,则没有办法。。。
3)动画效果执行完之后,只是在界面上重画了这个View,而View的实际位置跟属性其实是没有变化的。怎么理解呢,比如一个按钮,我们将它从左边移到右边了,在界面上看到这个按钮已经在右边了,但是当我们去点击它的时候,却没有事件响应,只有当回原来的位置的时候,事件才被响应,就是说,人跑了,心还留在原地。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值