Android 动画

本文详细介绍了Android中的两种主要动画系统——属性动画和视图动画,并解释了它们之间的区别及应用场景。属性动画更为灵活,适用于对象属性的动画制作,而视图动画则专用于View组件。此外,还介绍了Drawable动画的应用。

动画

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 差值器

Interpolator

当你没有为动画设置插值器时,系统默认会帮你设置加减速插值器AccelerateDecelerateInterpolator

自定义 Interpolator ,继承 Interpolator,重写 public float getInterpolation(float input) 方法,对返回值做处理。
这里的 input 参数是[0,1]的数,0 表示开始,1 表示结束,是系统根据设置的动画时间计算出来的,从0匀速到1。

TypeEvaluator 估值器

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 会更方便。

更多参考 blog post

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值