Android property animation-property animation (1)

Source: Internet
Author: User

This article translated from: http://developer.android.com/guide/topics/graphics/prop-animation.html

The property animation system is a robust framework that allows almost any object to be animated. You can define an animation by changing the attributes of any object over time without worrying about whether the object is to be drawn on the screen. An Attribute animation changes the value of an attribute (a member field in an object) over a specified length of time. To convert an object into an animation, you must specify the desired animation attribute for the object, such as the position of the object on the screen, the animation stay time, and the value between animations.

The property animation system can define the following animation features:

1. Duration: Specifies the animation duration. The default length is 300 milliseconds.

2. Time interpolation: This value can be specified as the attribute value of the function for calculating the current animation running time. It determines the animation change frequency.

3. Repeat count and Behavior)

This attribute specifies whether to play the animation again at the end of the animation, and the number of times the animation is played repeatedly. You can also specify whether or not the animation can be played in reverse playback. If reverse playback is set, the animation will play back and forth until the playback times are reached.

4. animation sets: you can organize an animation into a logical set and play them at the same time, in sequence, or at a later time.

5. Frame refresh delay: You can specify the refresh frequency of the animation frame. By default, refresh is performed every 10 seconds. However, the final frame refresh speed of the application depends on the system's busy schedule and the response speed of the underlying timer provided by the system.

How Property animation works

First, let's take a simple example to see how the animation works. Figure 1 draws a hypothetical animation object, which uses the X attribute to represent its horizontal position on the screen. The animation duration is set to 40 milliseconds and the moving distance is 40 pixels. The default frame refresh frequency is 10 milliseconds, that is, 10 pixels are moved horizontally every 10 milliseconds. At the end of 40 ms, the animation stops, and the animation will stay at the position of 40 pixels horizontally. This is an animation example of linear interpolation, which means that the animation is moving at a constant speed.

Figure 1. Linear animation example

You can also specify the animation of the non-linear difference. Figure 2 assumes that an animation object starts and stops acceleration. The object still moves 40 pixels within 40 milliseconds, but is non-linear. At the beginning, the animation is accelerated to half the position, and then starts to slow down until the animation ends. As shown in 2, the distance between object running is shorter than that in the start and end stages.

Figure 2. Non-linear animation example

Next, let's take a closer look at how animation is calculated as an important component of the property animation system. Figure 3 shows how the main class and other classes work together.

Figure 3. animation Calculation Method

The valueanimator object maintains the animation time track, such as the animation running time and the current value of the animation attribute.

The valueanimator class encapsulates a timeinterpolator class, which defines the animation difference and a typeevaluator class, which defines the calculation method of animation attribute values. For example, in Figure 2, The timeinterpolator object uses the acceleratedecelerateinterpolator definition, and the typeevaluator uses the intevaluator definition.

To start an animation, you must create a valueanimator object and set the start and end values of the desired animation attribute and the animation duration. When you call the START () method to start an animation, The valueanimator object is between 0 and 1 Based on the animation duration and the executed time, calculate an elapsed fraction (past coefficient ). This coefficient represents the percentage of animation completed, 0 means 0%, 1 means 100%. For example, in Figure 1
= The coefficient of the past 10 ms is 0.25, because the total duration is t = 40 ms.

When the valueanimator object completes the calculation of the past coefficient, it will call the currently set timeinterpolator object to calculate an interpolated fraction ). Interpolated fraction maps the past coefficient (elapsed fraction) to a new coefficient that considers setting the time difference. For example, in Figure 2, because the animation is slowly accelerated, the difference coefficient is about 0.15 at t = 10 milliseconds, which is greater than the previous coefficient (elapsed
Fraction) 0.25 should be small. In Figure 1, the interpolated fraction is always the same as the former elapsed fraction.

When calculating the interpolated fraction, the valueanimator object calls the corresponding typeevaluator object to calculate the animation attribute value based on the Difference Coefficient, animation start value, and end value. For example, in Figure 2, the difference coefficient is 0.15 at t = 10 ms, so the attribute value at this time should be 0.15 * (40-0) = 6.

The com. example. Android. APIs. animation package in the API demos sample project provides many examples of how to use the property animation system. Http://developer.android.com/tools/samples/index.html)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.