程序员论坛——属性动画介绍 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,如下:
- //初始化
- Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
- //设置动画时间 alphaAnimation.setDuration(3000);
- this.startAnimation(alphaAnimation);
- Animation rotateAnimation = new RotateAnimation(0f, 360f);
- Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);
- Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);
原文中间部分还有,参数详细说明:http://blog.csdn.net/feng88724/article/details/6318430
上面通过Java代码,实现了4中不同的Tween动画,其实在Android中完全可以通过 XML文件来实现动画。
这种方式可能更加简洁、清晰,也更利于重用。
下面我们分别对这几种动画改用xml来实现。
首先是AlphaAnimation。
alpha_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="2000"
- />
- </set>
不用解释了吧。
RotateAnimation
rotate_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="360"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="500"
- />
- </set>
ScaleAnimation
scale_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.0"
- android:fromYScale="0.0"
- android:toYScale="1.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="500"
- />
- </set>
TranslateAnimation
translate_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="10"
- android:toXDelta="100"
- android:fromYDelta="10"
- android:toYDelta="100"
- />
- </set>
布局文件都已经写完,那么如何来使用这些文件呢?
其实也很简单,此时需要用到AnimationUtils类。 通过该类中 loadAnimation 方法来加载这些布局文件。
如:
- rotateAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.rotate_anim);
对于文章里面的“附上整个自定义view的源码——就动画写到一个view里面,不是太理解”
在Activity中调用该类时,需要注意一定setFocusable(true), 否则焦点在Activity上的话,onKeyUp方法是不会生效的。
调用该View的代码:
- TweenAnim anim = new TweenAnim(Lesson_11.this);
- anim.setFocusable(true);
- setContentView(anim);
上面通过Java代码,实现了4中不同的Tween动画,其实在Android中完全可以通过 XML文件来实现动画。这种方式可能更加简洁、清晰,也更利于重用。
下面我们分别对这几种动画改用xml来实现。
首先是AlphaAnimation。
alpha_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="2000"
- />
- </set>
不用解释了吧。
RotateAnimation
rotate_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="360"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="500"
- />
- </set>
ScaleAnimation
scale_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <scale
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.0"
- android:fromYScale="0.0"
- android:toYScale="1.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="500"
- />
- </set>
TranslateAnimation
translate_anim.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="10"
- android:toXDelta="100"
- android:fromYDelta="10"
- android:toYDelta="100"
- />
- </set>
布局文件都已经写完,那么如何来使用这些文件呢?
其实也很简单,此时需要用到AnimationUtils类。 通过该类中 loadAnimation 方法来加载这些布局文件。
如:
- rotateAnimation = AnimationUtils.loadAnimation(this.getContext(), R.anim.rotate_anim);