Android 帧动画、View动画、属性动画的概念、原理、使用以及它们间的区别和联系


帧动画

  • 概念
    AnimationDrawable,帧动画,由一组图片集合而成,是一种具有动画效果的图片资源,对应的xml标签是animation-list。

  • 使用
    anim_loading.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true"> 
    <item android:drawable="@drawable/filel" android:duration="200" 
    <item android:drawable="@drawable/file2" android:duration="200" 
    <item android:drawable="@drawable/file3" android:duration="200" 
</animation-list> 
img.setImageResource(R.drawable.anim_loading);  
animationDrawable = ((AnimationDrawable) img.getDrawable());  
animationDrawable.start();  

视图动画

  • 概念
    是补间动画,主要是向View对象设置动画效果,包括AlphaAnimation 、RotateAnimation 、ScaleAnimation 、TranslateAnimation 这4种效果,对应的xml标签分别是alpha、rotate、scale、translate。

  • 使用
    1、设置相应动画效果的起点值、终点值、duration(时长)、Interpolator(加速度)。(注:RotateAnimation还需要设置旋转中心坐标值)
    2、View.startAnimation(animation)。

    Interpolator:插值器,随时间流逝动画路程所应走过的百分比。比如,若设置为LinearInterpolator(匀速),时间过一半则插值器的值是0.5;若设置的是DecelerateInterpolator(加速插值器),时间过一半则插值器的值大于0.5。

参考该demo下的Animation:游戏中弹出宝箱动画


属性动画

  • 概念
    属性动画可以对任何对象的属性做动画而不仅仅是View,甚至可以没有对象。除了作用对象进行扩展外,属性动画的效果也加强了,不仅能实现View动画的4中效果,还能实现其它多种效果,这些效果都是通过ValuAnimator或ObjectAnimator、AnimatorSet等来实现的。

  • 使用
    1、设置作用对象、属性的起点值、属性的终点值、TypeEvaluator(路线)、duration(时长)、Interpolator(加速度)
    2、animator.start()。

属性动画原理:属性动画要求作用的对象(如View)提供该属性(如View的scaleX属性)的getter、setter方法(如setScaleX()方法)。属性动画根据作用对象的属性的起点值、终点值、TypeEvaluator(这三者结合形成动画的路线)在动画过程中据Interpolator计算出当前已走过的路程,并以此设置属性的当前值。具体例子参考该demo下的Animation:游戏中弹出宝箱动画
详细原理参考:《Android开发艺术探索》7.3.4对任意属性做动画 这一小节。

当对象不提供某个属性的getter、setter时,如何对这一属性做动画效果?有下面3种解决方法
1. 给该对象加上getter、setter。
2. 用一个类来包装原始对象,间接为其提供getter、setter方法。
3. 采用ValueAnimator,监听动画过程,自己实现属性的改变。

第3种解决方法参考该demo下的Animation:游戏中弹出宝箱动画


总结(区别和联系)

View动画、属性动画的使用:
1. 确定动画路线(起点值、终点值、路线TypeEvaluator)。
2. 设置动画效果变化的时长(duration)和加速度(Interpolator)。
注:View动画没有TypeEvaluator,路线都是直线的。

猜你喜欢

转载自blog.csdn.net/cheneasternsun/article/details/80511295