【摘录+总结】Android动画之Tween动画 (渐变、缩放、位移、旋转)

程序员论坛——属性动画介绍   http://bbs.itheima.com/thread-172632-1-1.html

2、ValueAnimator 值动画执行类     

优点:结合动画更新监听onAnimationUpdate使用,可以在回调中不断更新View的多个属性,使用起来更加灵活。

结合9.TypeEvaluator  类型估值,获得动画执行的进度。


Android动画学习笔记(基础认识,没事还是多看下)  http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html

这三种动画模式在SDK中被称为property animation,view animation,drawable animation。 可通过NineOldAndroids项目在3.0之前的系统中使用Property Animation。

http://blog.csdn.net/feng88724/article/details/6318430     最好是通过下面的xml文件来实现动画效果,方便复用!!


Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。

第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。

 

下面就讲一下Tweene Animations。

 

主要类:

 

Animation   动画

AlphaAnimation 渐变透明度

RotateAnimation 画面旋转

ScaleAnimation 渐变尺寸缩放

TranslateAnimation 位置移动

AnimationSet  动画集

 

 

有了这些类,那么我们如何来实现动画效果呢? 


通过代码实现 AlphaAnimation,如下:

[java]  view plain  copy
 print ?
  1. //初始化  
  2. Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  
  3. //设置动画时间            alphaAnimation.setDuration(3000);  
  4.                 this.startAnimation(alphaAnimation);  

  1. Animation rotateAnimation = new RotateAnimation(0f, 360f);  
  1. Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);  
  1. Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。

原文中间部分还有,参数详细说明:http://blog.csdn.net/feng88724/article/details/6318430






 

上面通过Java代码,实现了4中不同的Tween动画,其实在Android中完全可以通过 XML文件来实现动画。

这种方式可能更加简洁、清晰,也更利于重用。 


下面我们分别对这几种动画改用xml来实现。

 

首先是AlphaAnimation。

alpha_anim.xml:

[xhtml] view plain copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <alpha  
  4.         android:fromAlpha="0.1"  
  5.         android:toAlpha="1.0"  
  6.         android:duration="2000"  
  7.     />  
  8. </set>  
 

 

不用解释了吧。

 

RotateAnimation

rotate_anim.xml:

[xhtml]  view plain  copy
 print ?
  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="360"  
  7.         android:pivotX="50%"  
  8.         android:pivotY="50%"  
  9.         android:duration="500"  
  10.     />  
  11. </set>  
 

 

 

ScaleAnimation

scale_anim.xml:

[xhtml]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale  
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  5.         android:fromXScale="0.0"  
  6.         android:toXScale="1.0"  
  7.         android:fromYScale="0.0"  
  8.         android:toYScale="1.0"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:fillAfter="false"  
  12.         android:duration="500"  
  13.     />     
  14. </set>  
 

 

TranslateAnimation

translate_anim.xml:

 

[xhtml]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate  
  4.         android:fromXDelta="10"  
  5.         android:toXDelta="100"  
  6.         android:fromYDelta="10"  
  7.         android:toYDelta="100"  
  8.     />  
  9. </set>  
 

 

布局文件都已经写完,那么如何来使用这些文件呢?

 

其实也很简单,此时需要用到AnimationUtils类。 通过该类中 loadAnimation 方法来加载这些布局文件。

如:

[java]  view plain  copy
 print ?
  1. rotateAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.rotate_anim);  





对于文章里面的“附上整个自定义view的源码——就动画写到一个view里面,不是太理解”



在Activity中调用该类时,需要注意一定setFocusable(true), 否则焦点在Activity上的话,onKeyUp方法是不会生效的。

调用该View的代码:

[java]  view plain  copy
 print ?
  1. TweenAnim anim = new TweenAnim(Lesson_11.this);  
  2. anim.setFocusable(true);  
  3. setContentView(anim);  



 

上面通过Java代码,实现了4中不同的Tween动画,其实在Android中完全可以通过 XML文件来实现动画。这种方式可能更加简洁、清晰,也更利于重用。 

下面我们分别对这几种动画改用xml来实现。

 

首先是AlphaAnimation。

alpha_anim.xml:

[xhtml] view plain copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <alpha  
  4.         android:fromAlpha="0.1"  
  5.         android:toAlpha="1.0"  
  6.         android:duration="2000"  
  7.     />  
  8. </set>  
 

 

不用解释了吧。

 

RotateAnimation

rotate_anim.xml:

[xhtml]  view plain  copy
 print ?
  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="360"  
  7.         android:pivotX="50%"  
  8.         android:pivotY="50%"  
  9.         android:duration="500"  
  10.     />  
  11. </set>  
 

 

 

ScaleAnimation

scale_anim.xml:

[xhtml]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale  
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  5.         android:fromXScale="0.0"  
  6.         android:toXScale="1.0"  
  7.         android:fromYScale="0.0"  
  8.         android:toYScale="1.0"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:fillAfter="false"  
  12.         android:duration="500"  
  13.     />     
  14. </set>  
 

 

TranslateAnimation

translate_anim.xml:

 

[xhtml]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate  
  4.         android:fromXDelta="10"  
  5.         android:toXDelta="100"  
  6.         android:fromYDelta="10"  
  7.         android:toYDelta="100"  
  8.     />  
  9. </set>  
 

 

布局文件都已经写完,那么如何来使用这些文件呢?

 

其实也很简单,此时需要用到AnimationUtils类。 通过该类中 loadAnimation 方法来加载这些布局文件。

如:

[java]  view plain  copy
 print ?
  1. rotateAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.rotate_anim);  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值