Android property animation-property animation (7)

Source: Internet
Author: User

Use plug-in

The plug-in defines how to specify the computing value used for the time function in the animation. For example, if you specify a linear playing animation throughout the animation period, it means that the animation moves evenly throughout the entire time, and you can also specify a non-linear animation, such: use the acceleration or deceleration animation at the beginning or end of the animation.

In an animation system, the plug-in receives a ratio from an animation or object, which represents the animation's past time. The plug-in tool modifies the ratio so that it matches the provided target animation type. The Android system provides a set of common plug-ins in the Android. View. animation package. If this package does not meet your needs, you can implement the timeinterpolator interface to create your own plug-in.

For example, the following is a comparison of how the acceleratedecelerateinterpolator and linearinterpolator plug-ins calculate the interpolation ratio. Linearinterpolator has no effect on the latency ratio. acceleratedecelerateinterpolator enables the animation to accelerate and slows down and exit. The logic defined in these plug-in methods is as follows:

Acceleratedecelerateinterpolator

Public float getinterpolation (float input ){


Return (float) (math. Cos (input + 1) * Math. Pi)/2.0f) + 0.5f;

}

Linearinterpolator

Public float getinterpolation (float input ){


Return input;

}

The following table lists the approximate values of an animation that lasts for 1000 milliseconds using the plug-in:

Playback time (MS)

Playback ratio/interpolation ratio (linear)

Interpolation ratio (acceleration/deceleration)

0

0

0

200

0.2

0.1

400

0.4

0.345

600

0.6

0.8

800

0.8

0.9

1000

1

1

As shown in the preceding table, the computation result of the linearinterpolator plug-in changes at a constant speed, increasing by 200 every 0.2 milliseconds. The calculation result of the accelerateinterpolator plug-in is faster than that of linearinterpolator between 200 milliseconds and 600 milliseconds, while that of linearinterpolator is slower than that of linearinterpolator between 600 milliseconds and 1000 milliseconds.

Key Frame

A keyframe object consisting of time and value defines the State of an animation at a specific time point. Each key frame also has its own plug-in to control the animation behavior between the current key frame and the previous key frame.

To instantiate a keyframe object, you must use one of the following factory methods: ofint (), offloat (), or ofobject (). Use these factory methods to obtain key frames of the corresponding type, and then call the ofkeyframe factory method to obtain a propertyvaluesholder object. Once this object is obtained, you can get an animation maker object passed in the propertyvaluesholder object. The following code demonstrates how to do this:

Keyframe kf0 = keyframe. offloat (0f, 0f );

Keyframe kf1 = keyframe. offloat (. 5f, 360f );

Keyframe kf2 = keyframe. offloat (1f, 0f );

Propertyvaluesholder pvhrotation = propertyvaluesholder. ofkeyframe ("rotation", kf0, kf1, kf2 );

Objectanimator rotationanim = objectanimator. ofpropertyvaluesholder (target, pvhrotation)

Rotationanim. setduration (5000 ms );

For a complete example of how to use a key frame, the multipropertyanimation example in apidemo is used.

Http://developer.android.com/tools/samples/index.html

Create a view animation

The property animation system allows you to simplify the animation of a view object and provides some advantages in the view animation system. The view animation system converts a view object by changing the drawing method of the view object. This type of transformation is processed in the container of each view object, because the view object itself does not execute this processing attribute. This processing will produce an animated effect on the view object, but will not change the view object itself. In this way, even if a view object is drawn at different locations on the screen, the object will remain in its original position. In android3.0, a new property and the getter and setter methods of the object are added to eliminate this defect.

The property animation system allows the view object to display the animation effect on the screen by changing the actual attributes of the view object. In addition, the view object automatically calls the invalidate () method to display the attribute when the attribute changes. In the View class, new attributes that facilitate animation settings include:

1. translationx and translationy: These two attributes are used as an increment to control the position of the view object starting from the coordinates in the upper left corner of its layout container.

2. rotation, rotationx, and rotationy: these three attributes control the 2D and 3D Rotation of the view object around the pivot.

3. scalex and scaley: These two attributes control the 2D scaling of the view object around its pivot.

4. Adjust Tx and adjust Ty: These two attributes control the Pivot Position of the view object, and rotate and scale around the pivot. By default, the pivot point is the center point of the view object.

5. X and Y: these are two simple and practical attributes. They describe the final position of the view object in its container, which is the sum of the coordinates of the first upper left corner and the values of translationx and translationy.

6. ALPHA: it indicates the Alpha transparency of the view object. The default value is 1 (not transparent), and 0 indicates completely transparent (invisible ).

To make an attribute of a view object have an animation effect, such as its color or rotation value, you need to create an attribute animation maker and specify the desired animation effect for the object property, for example:

Objectanimator. offloat (myview, "rotation", 0f, 360f );

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.