Property Animation

Property Animation

You can define an animation to change any object property over time, regardless of whether id draws to the screen or not. A property animation changes a property's (a field in an object) value over a specified length of time.

The property animation system lets you define the following characteristics of an animation:

a> Duration: the duration of an animation. The default length is 300 ms.

b> Time interpolation: how the values for the property are calculated as a function of the animation's current elapsed time.

c> Repeat count and behavior: whether or not to have an animation repeat when is reaches the end of a duration and how many times to repeat the animation. Also, you can specify whether the animation to play back in reverse.

d> Frame refresh delay: how often to refresh frames of your animation. The default is set to refresh every 10 ms.

How Property Animation Works


The ValueAnimator object keeps track of your animation's timing, such as how long the animation has been running, and the current value of the property that it is animating.

The ValueAnimator encapsulates a TimeInterpolator, which defines animation interpolation, and a TypeEvaluator, which defines how to calculate values for the property being animated.

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 elapses. The elapsed fraction represents the percentage of time that the animation has completed.

When the ValueAnimator is done calculating an elapsed fraction, it calls the TimeInterpolator that is currently set, to calculator an interpolated fraction. An interpolated fraction maps the elapsed fraction to a new fraction that takes into account the time interpolation that is set.

When the interpolated fraction is calculated, ValueAnimator calls the appropriate TypeEvaluator, to calculate the value of the property that you are animating, based on the interpolated fraction, the starting value, and the ending value of the animation.

How Property Animation Differs from View Animation

The view animation system provides the capability to only animate View objects, so if you wanted to animate non-view objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotating of a view but not the background color, for instance.

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself.

The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything you need to do, or if your existing code already works the way you want, there is no need to use the property animation system.

API Overview

a> animators

The animators provides the basic structure for creating animations.

b> evaluators

Evaluators tell the property animation system how to calculate values for a given property.

c> time interpolators

A time interpolator defines how specific values in an animation are calculated as a function of time.

Animating with ValueAnimator

The ValueAnimator class lets you animate values of some type for the duration of an animation by specifying a set of int, float, or color values to animate through.

The ValueAnimator does not operate on objects or properties directly. The most likely thing that you want to do is modify the objects that you want to animate with these calculated values. You need to implement the ValueAnimator.AnimatorUpdateListener, with which you can obtain the calculated value for that specific frame refresh by calling getAnimatedValued().

Animating with ObjectAnimator

The ObjectAnimator is a subclass of the ValueAnimator and combines the timing engine and value computation of ValueAnimator with the ability to animate a named property of a target object.

To have the ObjectAnimator update properties correctly, you must do the following:

a> The object property that you are animating must have a setter function in the form of set<propertyName>().

b> If you specify only one value for the values... parameter in one of the ObjectAnimator factory methods, it is assumed to be the ending value of the animation. Therefore, the object property that you are animating must have a getter function that is used to obtain the starting value of the animation. The getter function must be in the form of get<propertyName>().

c> The getter(if needed) and setter methods of the property that you are animating must operate on the same type as the starting and ending values that you specify to ObjectAnimator.

d> Depending on what property to object you are animating, you might need to call the invalidate() method on a view to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback.

Choreographing Multiple Animations with AnimatorSet

In many cases, you want to play an animation that depends on when another animation starts or finished. The Android system lets you bundle animations together into an AnimatorSet, so that you can specify whether to start animations simultaneously, sequentially, or after a specified delay.

Animation Listeners

Animator.AnimatorListener

a> onAnimationStart()

b> onAnimationEnd()

c> onAnimationRepeat()

d>onAnimationCancel()

Called when the animation is canceled. A cancelled animation also calls onAnimationEnd(), regardless of how they were ended.

ValueAnimator.AnimatorUpdatedListener

a> onAnimationUpdate()

Called on every frame of the animation. To use the value, query the ValueAnimator object passed into the event to get the current animated value with the getAnimatedValue() method.

Depending on what property or object you are animating, you might need to call invalidate() on a View to force that area of the screen to redraw itself with the new animated values.

Animating Layout Changed to ViewGroups

The property animation system provides the capability to animate changes to view group objects as well as provide an easy way to animate view objects themselves.

You can animate layout changes within a view group with the LayoutTransition class.

Using a TypeEvaluator

If you want to animate a type that is unknown to the Android system, you can create your own evaluator by implementing the TypeEvaluator interface. The types that are known by the Android System are int, float, or a color, which are supported by the IntEvaluator, FloatEvaluator, and ArgbEvaluator type evaluators.

There is only one method to implement in the TypedEvaluator interface, the evaluate() method.

When ValueAnimator (or ObjectAnimator) runs, it calculates a current elapsed fraction of the animation and then calculates an interpolated version of that depending on what interpolator that you are using. The interpolated fraction is what your TypedEvaluator receives through the fraction parameter, so you do not have to take into account the interpolator when calculating animated values.

Using Interpolators

An interpolator define how specific values in an animation are calculated as a function of time.

Interpolators in the animation system receive a fraction from Animators that represent the elapsed time of the animation. The Android system provides a set of common interpolators. If none of these suit your needs, you can implement the TimeInterpolator interface and create your own.

Specifying Keyframes

A Keyframe object consists of a time/value pair that lets you define a specific state at a specific time of an animation. Each keyframe can also have its own interpolator to control the behavior of the animation in the interval between the previous keyframe's time and the time of this keyframe.

Animating Views

The view animation system transformed View objects by changing the way that they were drawn. This resulted in the view being animated, but caused no change in the view object itself. This led to behavior such as an object still existing in its original location, even though it was drawn on a different location on the screen.

In Android 3.0, new properties and the corresponding getter and setter methods were added to eliminate this drawback.

The property animation system can animate Views on the screen by changing the actual properties in the view objects. In addition, views also automatically call the invalidate() method refresh the screen whenever its properties are changed. The new properties in the View class that facilitate property animations are: 

a> translationX and translationY

b> rotation, rotationX, and rotationY

c> scaleX and scaleY

d> pivotX and pivotY

e> x and y

f> alpha

Animating with ViewPropertyAnimator

The ViewPropertyAnimator provides a simple way to animate several properties of a View in parallel, using a single underlying Animator object. It behaves much like an ObjectAnimator, because it modifies the actual values of the view's properties, but is more efficient when animating many properties at once. In addition, the code for using the ViewPropertyAnimator is much more concise and easier to read.

Declaring Animations in XML

To distinguish animation files that use the new property animation APIs from those that are the legacy view animation framework, starting with Android 3.1, you should save the XML files for property animations in the res/animator/ directory (instead of res/anim/).






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值