安卓基础知识Property Animation属性动画文档格式.docx
《安卓基础知识Property Animation属性动画文档格式.docx》由会员分享,可在线阅读,更多相关《安卓基础知识Property Animation属性动画文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
您可以指定如何经常刷新你的动画帧。
默认设置每10毫秒刷新,但在您的应用程序可以指定刷新帧的速度,最终取决于系统整体的状态和提供多快服务的速度依据底层的定时器。
(计算机学校
属性动画工作机制
首先,让我们去如何动画一个简单的例子。
图1描绘了一个假想的动画对象的x属性,代表其在屏幕上的水平位置。
动画的持续时间设置为40毫秒和旅行的距离是40像素。
每隔10毫秒,这是默认的帧刷新速率,物体水平移动10个像素。
在40ms的结束,动画停止,对象在水平位置40结束。
这是一个线性插值动画的例子,这意味着对象在一个恒定的速度移动。
图1线性动画的例子。
您还可以指定动画有一个非直线插补。
图2说明了一个假想的对象,加速在开头动画,在动画结束时减速。
对象仍然在40毫秒移动40个像素,但非线性。
在开始的时候,这个动画加速的中间点,然后从中间点减速,直到动画结束。
如图2所示,动画的开始和结束移动距离小于中间。
图2非线性动画的例子。
让我们看在属性动画系统的重要组成部分,如何计算像上面显示的动画的详细介绍。
图3描述了主要类是怎么工作的。
图3。
动画是如何计算的
ValueAnimator对象保持动画的实时跟踪,如动画已经运行的时间,和当前动画的属性值。
在ValueAnimator封装TimeInterpolator,它定义动画插值,和TypeEvaluator,它定义了如何计算的动画属性的值。
例如,如图2,使用的TimeInterpolator将是AccelerateDecelerateInterpolator和TypeEvaluator的将会是IntEvaluator的。
启动动画,创建一个ValueAnimator,给你想要的动画开始和结束值,定义动画的持续时间。
当你调用的start()动画开始。
在整个动画,ValueAnimator计算经过部分的分数介于0和1,基于动画的持续时间和多少时间已过。
经过的部分代表,动画已完成的时间百分比,0表示0%和100%1的含义。
例如,在图1中,在t=10毫秒时间的比例是0.25,因为总工期为T=40毫秒。
计算经过部分ValueAnimator时,它调用TimeInterpolator当前设置,计算插值分数。
一个插值分数经过部分映射到一个新的,考虑到设置的时间内插的分数。
例如,在图2中,因为动画慢慢加速,插约0.15分数,是比过去0.25部分少,在t=10毫秒内。
在图1中,插值分数始终是经过分数相同。
当插值分数计算,ValueAnimator的的调用适当的TypeEvaluator,计算你的动画属性值,基于内插的分数,起始值,结束值和动画。
例如,在图2中,插值部分是在t=0.15在10毫秒内,所以当时时间属性值为0.15x(40-0),或6。
关于如何使用属性动画,系统的com.example.android.apis.animation包中的API演示示例项目提供了很多例子。
(郑州北大青鸟
HowPropertyAnimationDiffersfromViewAnimation
TheviewanimationsystemprovidesthecapabilitytoonlyanimateViewobjects,soifyouwantedtoanimatenon-Viewobjects,youhavetoimplementyourowncodetodoso.TheviewanimationsystemisalsoconstrainedinthefactthatitonlyexposesafewaspectsofaViewobjecttoanimate,suchasthescalingandrotationofaViewbutnotthebackgroundcolor,forinstance.
AnotherdisadvantageoftheviewanimationsystemisthatitonlymodifiedwheretheViewwasdrawn,andnottheactualViewitself.Forinstance,ifyouanimatedabuttontomoveacrossthescreen,thebuttondrawscorrectly,buttheactuallocationwhereyoucanclickthebuttondoesnotchange,soyouhavetoimplementyourownlogictohandlethis.
Withthepropertyanimationsystem,theseconstraintsarecompletelyremoved,andyoucananimateanypropertyofanyobject(Viewsandnon-Views)andtheobjectitselfisactuallymodified.Thepropertyanimationsystemisalsomorerobustinthewayitcarriesoutanimation.Atahighlevel,youassignanimatorstothepropertiesthatyouwanttoanimate,suchascolor,position,orsizeandcandefineaspectsoftheanimationsuchasinterpolationandsynchronizationofmultipleanimators.
Theviewanimationsystem,however,takeslesstimetosetupandrequireslesscodetowrite.Ifviewanimationaccomplisheseverythingthatyouneedtodo,orifyourexistingcodealreadyworksthewayyouwant,thereisnoneedtousethepropertyanimationsystem.Italsomightmakesensetousebothanimationsystemsfordifferentsituationsiftheusecasearises.
API概述
您可以在android.animation里面找到属性动画系统大部分API。
因为视图的动画系统已经定义了许多插值在android.view.animation,你可以使用属性动画系统的插值。
下表描述的属性动画系统的主要组成部分。
Animator类提供了用于创建动画的基本结构。
你通常不使用这个类,因为它直接提供最基本的功能,必须扩展到完全支持动画值。
以下子类扩展Animator:
表1。
动画家(Animators)
类
描述
ValueAnimator
属性动画时序引擎也计算属性动画的值。
它拥有所有的核心功能,计算动画值,并包含每个动画,有关时序的详细信息是否动画重复,听众接收更新事件,并设置自定义类型的能力评估。
有两件,以生动活泼的属性:
动画值计算和设置这些对象的属性动画值。
ValueAnimator不进行第二件,所以你一定要更新计算值ValueAnimator和修改你想用自己的逻辑动画的对象。
请参阅有关更多信息AnimatingwithValueAnimator部分。
ObjectAnimator
ValueAnimator的子类,允许你设置一个目标对象和对象属性的动画。
当计算出一个新的动画值,本类更新相应的属性。
你大部分情况使用ObjectAnimator,因为它使得动画的目标对象的值更简单。
然而,有时你直接使用ValueAnimator,因为ObjectAnimator有一些限制,如对目标对象目前要求的具体acessor方法。
AnimatorSet
提供机制,以组合动画一起,让他们关联性运行。
你可以设置动画一起播放,顺序,或在指定的延迟之后。
请参阅有关部分ChoreographingmultipleanimationswithAnimatorSets更多信息。
评估人员告诉属性动画系统如何计算一个给定的属性值。
他们采取的时机,是由一个数据Animator类,动画的开始和结束值,并计算基于此数据属性的动画值。
属性动画系统提供了以下评价:
表2。
评价者(Evaluators)
类/接口
IntEvaluator
默认的计算器来计算int属性值
FloatEvaluator
默认评估值来计算浮动属性。
ArgbEvaluator
默认的计算器计算值表示为十六进制值的色彩属性。
TypeEvaluator
一个接口,允许你创建自己的评估。
如果你是动画对象的属性,不是一个整数,浮点数,或颜色,你必须实现的TypeEvaluator接口指定如何计算对象属性的动画值。
您也可以指定自定义TypeEvaluator整数,浮点数,颜色值以及,如果你想对比默认行为来处理这些类型的不同。
请参阅有关部分UsingaTypeEvaluator更多关于如何编写自定义评估信息。
一个时间插补定义如何在一个动画的特定值作为时间函数的计算。
例如,你可以指定动画发生线性在整个动画,这意味着动画均匀地移动整个时间,或者可以指定使用非线性时间的动画,例如,在开始加速,并在最后减速动画。
表3说明中所含的插值android.view.animation。
如果没有提供插值适合您的需要,实施TimeInterpolator接口,并创建自己的。
请参阅Usinginterpolators如何编写一个定制的插补的更多信息。
表3。
插值(Interpolators)
AccelerateDecelerateInterpolator
插补,其变化率慢慢开始和结束,但通过中间加速。
AccelerateInterpolator
插补,其变化率开始缓慢,然后加快。
AnticipateInterpolator
内插的变化开始落后,然后向前甩。
AnticipateOvershootInterpolator
内插的变化,开始落后,甩向前过冲目标值,然后终于可以追溯到最终值。
BounceInterpolator
插补,其变化在最后反弹。
CycleInterpolator
内插动画重复指定的周期数。
DecelerateInterpolator
插补,其变化的速度开始很快,然后减速。
LinearInterpolator
插补,其变化率是恒定的
OvershootInterpolator
内插的变化甩向前和过冲的最后一个值,然后回来。
TimeInterpolator
一个接口,使您实现自己的插补。
ValueAnimator动画
ValueAnimator类让你动画动画的持续时间由某种类型的值指定了一套整,浮,或颜色值动画。
您获得通过ValueAnimator调用工厂方法之一:
ofInt(),ofFloat(),orofObject()。
例如:
ValueAnimatoranimation=ValueAnimator.ofFloat(0f,1f);
animation.setDuration(1000);
animation.start();
在这段代码中,ValueAnimator开始动画的计算值,1000毫秒,当时间为0和1之间,运行的start()方法。
你也可以指定一个自定义类型的动画通过执行下列操作:
ValueAnimatoranimation=ValueAnimator.ofObject(newMyTypeEvaluator(),startPropertyValue,endPropertyValue);
在这段代码中,ValueAnimator开始计算之间的动画值,使用所提供的逻辑MyTypeEvaluator的start()方法运行时间为1000毫秒,当startPropertyValue和endPropertyValue。
然而,前面的代码片段,有没有对象的实际效果,因为在ValueAnimator不直接操作对象或属性。
最有可能的事情,你想要做的是修改这些计算值要进行动画的对象。
你定义在听众ValueAnimator妥善处理动画的寿命期间的重要事件,如帧更新。
实施的听众时,你可以通过调用特定的帧刷新计算值getAnimatedValue()。
对听众的更多信息,请参阅有关部分AnimationListeners。
AnimatingwithObjectAnimator
TheObjectAnimatorisasubclassoftheValueAnimator(discussedintheprevioussection)andcombinesthetimingengineandvaluecomputationofValueAnimatorwiththeabilitytoanimateanamedpropertyofatargetobject.Thismakesanimatinganyobjectmucheasier,asyounolongerneedtoimplementtheValueAnimator.AnimatorUpdateListener,becausetheanimatedpropertyupdatesautomatically.
InstantiatinganObjectAnimatorissimilartoaValueAnimator,butyoualsospecifytheobjectandthenameofthatobject'
sproperty(asaString)alongwiththevaluestoanimatebetween:
ObjectAnimatoranim=ObjectAnimator.ofFloat(foo,"
alpha"
0f,1f);
anim.setDuration(1000);
anim.start();
TohavetheObjectAnimatorupdatepropertiescorrectly,youmustdothefollowing:
∙Theobjectpropertythatyouareanimatingmusthaveasetterfunction(incamelcase)intheformofset<
propertyName>
().BecausetheObjectAnimatorautomaticallyupdatesthepropertyduringanimation,itmustbeabletoaccessthepropertywiththissettermethod.Forexample,ifthepropertynameisfoo,youneedtohaveasetFoo()method.Ifthissettermethoddoesnotexist,youhavethreeoptions:
◦Addthesettermethodtotheclassifyouhavetherightstodoso.
Useawrapperclassthatyouhaverightstochangeandhavethatwrapperreceivethevaluewithavalidsettermethodandforwardittotheoriginalobject.
UseValueAnimatorinstead.
∙Ifyouspecifyonlyonevalueforthevalues...parameterinoneoftheObjectAnimatorfactorymethods,itisassumedtobetheendingvalueoftheanimation.Therefore,theobjectpropertythatyouareanimatingmusthaveagetterfunctionthatisusedtoobtainthestartingvalueoftheanimation.Thegetterfunctionmustbeintheformofget<
().Forexample,ifthepropertynameisfoo,youneedtohaveagetFoo()method.
∙Thegetter(ifneeded)andsettermethodsofthepropertythatyouareanimatingmustoperateonthesametypeasthestartingandendingvaluesthatyouspecifytoObjectAnimator.Forexample,youmusthavetargetObject.setPropName(float)andtargetObject.getPropName(float)ifyouconstructthefollowingObjectAnimator:
ObjectAnimator.ofFloat(targetObject,"
propName"
1f)
∙Dependingonwhatpropertyorobjectyouareanimating,youmightneedtocalltheinvalidate()methodonaViewforcethescreentoredrawitselfwiththeupdatedanimatedvalues.YoudothisintheonAnimationUpdate()callback.Forexample,animatingthecolorpropertyofaDrawableobjectonlycauseupdatestothescreenwhenthatobjectredrawsitself.AllofthepropertysettersonView,suchassetAlpha()andsetTranslationX()invalidatetheViewproperly,soyoudonotneedtoinvalidatetheViewwhencallingthesemethodswithnewvalues.Formoreinformationonlisteners,seethesectionaboutAnimationListeners.
AnimatorSet创编动画
在许多情况下,你要播放的动画,取决于另一个动画开始或者结束时。
Android系统,让你捆绑动画到AnimatorSet一起,使您可以指定是否要同时,按顺序,或在指定的延迟后开始动画。
你可以在对方还AnimatorSet对象。
从下面的示例代码弹弹球样品(简单修改)扮演下面的动画对象以下列方式:
∙PlaysbounceAnim.
∙PlayssquashAnim1,squashAnim2,stretchAnim1,andstretchAnim2atthesametime.
∙PlaysbounceBackAnim.
∙PlaysfadeAnim.
AnimatorSetbouncer=newAnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
bouncer.play(bounceBackAnim).after(stretchAnim2);
ValueAnimatorfadeAnim=ObjectAnimator.ofFloat(newBall,"
1f,0f);
fadeAnim.setDuration(250);
AnimatorSetanimatorSet=newAnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
animatorSe