动画
The Android framework provides two animation systems: property animation and view animation. Both animation systems are viable options, but the property animation system, in general, is the preferred method to use, because it is more flexible and offers more features. In addition to these two systems, you can utilize Drawable animation, which allows you to load drawable resources and display them one frame after another.
Android Framework 提供了两个动画系统:属性动画和视图动画。它们都提供了可行的选项,但是一般情况下推荐使用属性动画,因为它更加灵活并且提供了更多特性。除了这两种动画系统,还可以利用 Drawable 动画来加载 drawable 资源进行一帧帧的显示。
Property Animation(属性动画)
Android 3.0(API 11)加入,属性动画让你可以给对象的属性进行 key 动画,即使是在屏幕上没渲染出来的。这套系统是可扩展的,让你也可以对自定义类型的属性进行 key 动画。
类继承关系
- Animator
-
- AnimatorSet
-
- ValueAnimator
-
-
- ObjectAnimator
-
-
-
- TimeAnimator
setStartDelay() 设置延迟播放动画
-
Interpolator 差值器 与 TypeEvaluator 估值器区别
The ValueAnimator encapsulates a TimeInterpolator, which defines animation interpolation, and a TypeEvaluator, which defines how to calculate values for the property being animated. For example, in Figure 2, the TimeInterpolator used would be AccelerateDecelerateInterpolator and the TypeEvaluator would be IntEvaluator.
属性动画的核心类:ValueAnimator 封装了一个 TimeInterpolator 和一个 TypeEvaluator,TimeInterpolator 规定了动画的差值,TypeEvaluator 定义了如何计算要进行动画的属性的值。
一个针对时间的变化,一个针对值得变化。
To start an animation, create a ValueAnimator and give it the starting and ending values for the property that you want to animate, along with the duration of the animation. When you call start() the animation begins. During the whole animation, the ValueAnimator calculates an elapsed fraction between 0 and 1, based on the duration of the animation and how much time has elapsed. The elapsed fraction represents the percentage of time that the animation has completed, 0 meaning 0% and 1 meaning 100%. For example, in Figure 1, the elapsed fraction at t = 10 ms would be .25 because the total duration is t = 40 ms.
[elapsed fraction 过去的时间分数(时间因子)]
自定义 Interpolator 与 TypeEvaluator
Interpolator 差值器
当你没有为动画设置插值器时,系统默认会帮你设置加减速插值器AccelerateDecelerateInterpolator
自定义 Interpolator ,继承 Interpolator,重写 public float getInterpolation(float input) 方法,对返回值做处理。
这里的 input 参数是[0,1]的数,0 表示开始,1 表示结束,是系统根据设置的动画时间计算出来的,从0匀速到1。
TypeEvaluator 估值器
自定义TypeEvaluator需要重写evaluate()方法,计算对象的属性值并将其封装成一个新对象返回
重写:
public T evaluate(float fraction, T startValue, T endValue);
使用XML编写动画
Animating Layout Changes to ViewGroups
Specifying Keyframes
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
rotationAnim.setDuration(5000ms);
更多参考 MultiPropertyAnimation.java
Animating with ViewPropertyAnimator
Multiple ObjectAnimator objects
ObjectAnimator animX = ObjectAnimator.ofFloat(myView, "x", 50f);
ObjectAnimator animY = ObjectAnimator.ofFloat(myView, "y", 100f);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(animX, animY);
animSetXY.start();
One ObjectAnimator
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start();
ViewPropertyAnimator
myView.animate().x(50f).y(100f);
如果只是对 View 的一个或两个属性进行动画,使用 ObjectAnimator 就可以完成,如果想要对 View 的多个属性同时进行动画,使用 ViewPropertyAnimator 会更方便。
View Animation(视图动画)
视图动画是一款老的动画系统,只能给 View 视图 key 动画。使用简单并且提供了足够的功能来满足一般应用的需要。
类继承关系
- Animation
-
- AlphaAnimation
-
- TranslateAnimation
-
- ScaleAnimation
-
- RotateAnimation
-
- AnimationSet
Drawable Animation(帧动画)
drawable 动画涉及一个接一个的显示 drawable 资源文件,就像播放电影。这种动画方式在你需要使用 Drawable 资源文件来做动画的情况下是非常有用的。
郭霖属性动画:http://blog.youkuaiyun.com/guolin_blog/article/details/43536355#t6
浅析Android动画 http://www.cnblogs.com/wondertwo/p/5295976.html
本文详细介绍了Android中的两种主要动画系统——属性动画和视图动画,并解释了它们之间的区别及应用场景。属性动画更为灵活,适用于对象属性的动画制作,而视图动画则专用于View组件。此外,还介绍了Drawable动画的应用。
1714

被折叠的 条评论
为什么被折叠?



