Android(4)——Property Animation属性动画

原文地址:Animation Resources

原文地址:Property Animation


Property Animation

Value Description
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats

Creates an animation by modifying an object's property values over a set period of time with an  Animator .就是说通过Animator来改变一个对象某段时间内的属性值来创建一个动画。


FILELOCATION:

res/animator/filename.xml

这个文件名用作resource ID。


COMPILED RESOURCE DATATYPE:

Resource pointer to a ValueAnimatorObjectAnimator, or AnimatorSet.


RESOURCE REFERENCE:

在代码中:R.animator.filename

在xml文件中:@[package:]animator/filename


语法Syntax:

<set
  android:ordering=["together" | "sequentially"]>

    <objectAnimator
        android:propertyName="string"
        android:duration="int"
        android:valueFrom="float | int | color"
        android:valueTo="float | int | color"
        android:startOffset="int"
        android:repeatCount="int"
        android:repeatMode=["repeat" | "reverse"]
        android:valueType=["intType" | "floatType"]/>

    <animator
        android:duration="int"
        android:valueFrom="float | int | color"
        android:valueTo="float | int | color"
        android:startOffset="int"
        android:repeatCount="int"
        android:repeatMode=["repeat" | "reverse"]
        android:valueType=["intType" | "floatType"]/>

    <set>
        ...
    </set>
</set>
这个文件必须有一个唯一的根元素,三者之一<set>、<objectAnimator>、<valueAnimator>。<set>是可以嵌套的。


<set>

它的作用是为了内部包含其他的元素,比如说<set>、<objectAnimator>、<valueAnimator>。

它的属性:

android:ordering  指定这个set中动画的播放顺序。

Value Description
sequentially Play animations in this set sequentially
together (default) Play animations in this set at the same time.

<objectAnimator>

它的作用是为一个对象在某个时间段内设置特定的属性。

它的属性:

android:propertyName StringRequired 就是这个动画的名称啦。

android:valueFrom float, int, or color. 动画开始值。可以不写,通过object目前的值设定。

android:valueTo  float, int, or colorRequired.动画结束值。

android:duration int. 动画持续时间,毫秒为单位,300毫秒是默认值。

android:startOffset int.start()函数启动以后动画延迟开始的时间。

android:repeatCount int.动画重复次数。-1表示无限次重复。默认值为0直播放一次。1就表示要重复两次了。

android:repeatMode int. 重复播放的时候是倒着播放还是顺着播放。前提是android:repeatCount属性必须是非0值。

android:valueType Keyword.如果value是一个color就不要设置这个属性了。color值会自动处理的。

Value Description
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats


<animator>

它的作用是在一段时间内执行某个动画。

它的属性:

android:valueFrom float, int, or colorRequired.动画开始值。

android:valueTo float, int, or colorRequired. 动画结束值。

android:duration int. 动画持续时间,毫秒为单位,300毫秒是默认值。

android:startOffset int.start()函数启动以后动画延迟开始的时间。

android:repeatCount int.动画重复次数。-1表示无限次重复。默认值为0直播放一次。1就表示要重复两次了。

android:repeatMode int. 重复播放的时候是倒着播放还是顺着播放。前提是android:repeatCount属性必须是非0值。

android:valueType Keyword.如果value是一个color就不要设置这个属性了。color值会自动处理的。

Value Description
intType Specifies that the animated values are integers
floatType (default) Specifies that the animated values are floats

举例吧:XML file saved at res/animator/property_animator.xml:

<set android:ordering="sequentially">
    <set>
        <objectAnimator
            android:propertyName="x"
            android:duration="500"
            android:valueTo="400"
            android:valueType="intType"/>
        <objectAnimator
            android:propertyName="y"
            android:duration="500"
            android:valueTo="300"
            android:valueType="intType"/>
    </set>
    <objectAnimator
        android:propertyName="alpha"
        android:duration="500"
        android:valueTo="1f"/>
</set>
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);
set.setTarget(myObject);
set.start();
AnimatorSet表示就是说这个文件下所有的动画集合。然后把这个动画集合都给了那个对象。



原文地址:Property Animation

二、Property Animation的特性:

Duration: 持续时间。

Time interpolation: 加速度啦,就是根据当前的时间为函数的值。

Repeat count and behavior:重复次数和重复时反向还是正向。

Animator sets: 一系列的逻辑动画。

Frame refresh delay: 多久刷新一次。默认是10ms一次,但实际的效果最终取决于你的系统的繁忙程度。


三、属性动画工作原理:




ValueAnimator对象会一直监测动画的持续时间和当前属性的值。

在ValueAnimator的内部封装着TimeInterpolator,它定义了动画的interpolation。在Figure2中它的值为AccelerateDecelerateInterpolator,表示现加速后减速。

在ValueAnimator的内部还封装着一个TypeEvaluator,它定义了如何计算动画改变属性的值。在Figure2中它的值为IntEvaluator。

为了启动一个animation,需要创建一个ValueAnimator,还要给它设置动画属性的起始、结束值和持续时间。当你调用start()时动画开始。在整个动画期间,ValueAnimator计算已运行时间(elapsed fraction)和总的运行时间的比例值,为0到1,分别为0%和100%。

这里有两种值啦:interpolated fraction 和 elapsed fraction,前者指的是动画运行比例,后者指的是时间比例。

当ValueAnimator每计算过一次运行部分elapsed fraction后它会调用TimeInterpolator,去计算interpolated fraction。比如在figure2中,因为动画是缓慢加速的,所以在t=10ms的时候,interpolated fraction为0.15,而elapsed fraction为0.25。在Figure1中,elapsed fraction和interpolated fraction一直都是一样的。

当interpolated fraction(运行比例)计算过后,ValueAnimator就会调用TypeEvaluator来根据interpolated fraction、开始值和结束值去计算动画涉及属性的值。比如在Figure2中,t=10ms时,它的值这样计算:15 X (40 - 0), or 6.

The com.example.android.apis.animation package in the API Demos sample project provides many examples on how to use the property animation system.


四、属性动画和View Animation的区别:

1、View Animation只能对View对象设置动画,其他的non-View对象智能通过代码去实现。

2、View Animation提供能动画类型太少了,例如放大缩小旋转等,但是不能改变背景颜色之类的。

3、View Animation只能改变VIew在哪里显示,但不是真正的改变View它本身。就是说虽然移动了图标,但是这个控件原本的点击事件依然还在原来的位置,你只能通过代码逻辑去处理。

4、Property animation确是真实的移动改变对象属性,并且这个对象可以是任意的对象。

5、Property animation可以改变color、position、size和动画的interpolation、synchronization。

6、View Animation有个优点就是需要更少的时间建立,也需要更少的代码。

7、同样的效果优先使用View Animation。


五、API概述

 android.animation中可以找到绝大多数的属性,因为view animation已经在 android.view.animation定义了许多的interpolators,你也可以把它们拿来在Property animation中使用。下面的表中提供了Property animation的组件。

Animator class:提供了创建animation的基本结构。你通常是不用这个类的,因为它仅仅提供了最基本的功能,它们是需要被继承然后去实现更具体的功能的。下面的一些子类就是继承自Animator的:



Evaluators:提供了如何去计算给定属性的值。根据开始值、结束值和Animator class 提供的timing data值来计算属性值。




Interpolators:定义了在运行期内如何计算一个特定的值。例如在计算加速度的时候。Table3提供了一些在android.view.animation中的加速器。如果这里面还没有你想要的,那么可以通过实现 TimeInterpolator接口来创建你自己的加速器。




六、ValueAnimator class

ValueAnimator class可以帮助你改变一些类型的值,例如int、float or color values。

可以通过它的factory methods来获取ValueAnimator :ofInt()、ofFloat()、ofObject()。

举例:

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start();
还有一个例子:

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();
上面的这两个例子的动画都没有和特定的对象对应,通常我们都会给对象去设置监听器 Animation Listeners关联一个动画的。在监听器的实现方法中可以通过 getAnimatedValue()来获取具体值。


六、ObjectAnimator class

ObjectAnimator是ValueAnimator的子类,并且还添加了timing机制和计算目标对象属性值的功能。这样它更方便关联任意的对象,你也再不用去实现ValueAnimator.AnimatorUpdateListener,因为动画中会自动为对象更新。

实例化一个ObjectAnimator和实例化一个ValueAnimator是类似的,但是你需要去指定一个对象和这个对象的一些属性:

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);
anim.setDuration(1000);
anim.start();
被绑定的对象必须要有一个setter function,以 set<propertyName>()的形式。因为ObjectAnimator是自动在动画过程中更新对象属性的,所以对象必须由setter method作为接口。例如,如果你有一个属性名称为foo,那你就不许有一个setFoo()方法。如果这个setter method不存在,你有三种解决方法:

1、如果你有权限就自己在类中添加setter method。

2、用一个你有权限可以修改的wrapper class,并且让这个类有一个有效的setter method来接收值并将这个值赋给初始的对象。

3、那就不用ObjectAnimator了,用上面的ValueAnimator好了。


如果你在传参的时候只指定了一个值,那就默认这个值是结束值,但与此同时,你的对象属性必须有一个getter函数以便动画可以获取开始值。这个getter函数的形式是get<propertyName>()。比如你有属性名称foo,那你就必须有一个getFoo()方法。

ObjectAnimator.ofFloat(targetObject, "propName", 1f)
像上面这个例子中,我们就必须同时有 targetObject.setPropName(float)  and  targetObject.getPropName(float)两个方法。

在动画的过程中,你可能需要调用invalidate()方法来迫使屏幕去重新生成它更新的动画值。这个更新是在回调onAnimationUpdate()过程中实现的。当如果只是对象的颜色变化啊什么的,就不需要调用这个函数了。




























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值