Introduction and usage of animation in Android

Source: Internet
Author: User

Android SDK introduces two types of Animation:

Tween animation (gradient animation ):Animation effects are produced by performing image transformations on specific objects, such as translation, scaling, rotation, fade-out, and fade-in.

Frame Animation ):Create a drawable sequence. The drawable can be displayed one by one at a specified interval, that is, to play the prepared images in sequence.

1. tween animation Animation:

(1) tween animation has four styles:

ALPHA:Gradient transparency animation effect

Scale:Gradient scaling animation effect

Translate:Mobile animation effect

Rotate:Rotating animation effect


(2) There are two ways to use tween animation:

A. Define the animation in the XML resource and use the loadanimation () function in the animationutils to load the animation;

B. Use the constructor of the animation subclass to initialize the animation object.

Define animation in an XML Resource:

ALPHA:

<Alpha Android: interpolator = "@ Android: anim/accelerate_decelerate_interpolator" Android: fromalpha = "1.0" Android: toalpha = "0.0" Android: duration = "3000"> </alpha> <! -- Interpolator: Specifies an animation plug-in to control the animation speed change fromalpha: transparency 0.0 indicates full transparency 1.0 indicates full transparency 0.0 indicates full opacity. The value above is a float data type number between 1.0 and toalpha: Transparency duration at animation end: duration -->

Scale:

<Scaleandroid: interpolator = "@ Android: anim/accelerate_decelerate_interpolator" Android: fromxscale = "0.0" Android: toxscale = "1.4" Android: fromyscale = "0.0" Android: toyscale = "1.4" Android: rjtx = "50%" Android: 0000ty = "50%" Android: fillafter = "false" Android: startoffset = "700" Android: duration = "700" Android: repeatcount = "10"/> <! -- Fromxscale [float]: it is the scaling size on the X coordinate when the animation starts. The value 0.0 indicates the scaling size on the Y coordinate when the animation starts, 0.0 indicates that the animation is scaled down to 1.0, indicating that the normal scaling value is less than 1.0, indicating that the scale-in value is greater than 1.0, indicating that the toxscale [float] Is zoomed in, toyscale [float]: The scaling size of X coordinate when the animation ends. Optional TX [float]: it is the starting position of the animation relative to the X coordinate of the object. Ty [float]: it is the starting position of the animation relative to the X and Y coordinates of the object, 50%, P. The three statements represent absolute, relative_to_self, and relative_to_parent respectively. Attribute value description: from 0%-100%, 50% is the midpoint position fillafter [Boolean] on the X or Y coordinate of the object. If it is set to true, apply startoffset [long] After the animation ends: the time interval between the animations. Execute the next animation repeatcount [int] After the animation stops: number of animation repetitions -->

Translate:

<Translate Android: interpolator = "@ Android: anim/accelerate_decelerate_interpolator" Android: fromxdelta = "30" Android: toxdelta = "-80" Android: fromydelta = "30" Android: toydelta = "300" Android: Duration = "2000"/> <! -- Fromxdelta: the position on the X coordinate when the animation starts. toxdelta: the position on the X coordinate when the animation ends. fromydelta: the position on the Y coordinate when the animation starts. toydelta: it is the position on Y coordinate when the animation ends -->

Rotate:

<Rotateandroid: interpolator = "@ Android: anim/accelerate_decelerate_interpolator" Android: fromdegrees = "0" Android: todegrees = "+ 350" Android: Export Tx = "50%" Android: required ty = "50%" Android: Duration = "3000"/> <! -- Fromdegrees: Specifies the object angle todegrees when the animation starts: when the animation ends, the rotation angle of the object can be greater than 360 degrees. When the angle is negative, it indicates the clockwise rotation. When the angle is positive, it indicates the clockwise rotation (negative from -- to positive: clockwise rotation) (negative from -- to negative: counter-clockwise rotation) (positive from -- to positive: clockwise rotation) (positive from -- to negative: counter-clockwise rotation) rotate TX ;: it is the starting position of the animation relative to the X and Y coordinates of the object. Ty: it is the starting position of the animation relative to the X and Y coordinates of the object. 50% is the midpoint position on the X or Y coordinates of the object. -->

Use the constructor of the animation subclass to initialize the animation object:

The corresponding classes are provided in the android SDK. The animation class is derived fromAlphaanimation,Scaleanimation,Translateanimation,RotateanimationThe animation formats include translation, rotation, gradient size, and transparency.


Tween animation implements animation effects by performing a series of graphic transformations (including translation, scaling, rotation, and transparency change) on The View content. Specifically, a group of commands are pre-defined, which specify the type, trigger time, and duration of the graphic transformation. These commands can be defined in XML or source code. The program executes these commands along the timeline to achieve the animation effect.

(3) Use interpolator to control animation running in Android SDK

Interpolator defines the rate of change of an animation ). This allows the basic animation effects (alpha, scale, translate, and rotate) to accelerate, slow down, and repeat.

 

Interpolator is a base class. Android provides several interpolator sub-classes to implement different speed curves, as shown below:

 

Acceleratedecelerateinterpolator slows down at the animation start and end, and accelerates at the center

Accelerateinterpolator slows down at the beginning of the animation and starts acceleration.

Cycleinterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.

Decelerateinterpolator accelerates at the beginning of the animation, and then starts to slow down

Linearinterpolator changes at an even rate in the animation

 

(4) animation Running Mode

There are two animation modes:

 

Exclusive mode: the main thread of the program enters a loop and refreshes the screen continuously according to the animation instructions until the animation ends;

Interrupt mode: there is a separate thread that counts the time and sends a notification to the main thread at a certain time. The main thread updates the screen after receiving the notification.

2. Frame Animation:

As mentioned earlier, frame animation is used to play images prepared in sequence, similar to movies. The android SDK provides a class of animationdrawable to define and use frame animation.


Frame Animation can be defined in XML resource or by using the API definition in animationdrawable. Since tween animation and frame animation are very different, the XML definition format is also completely different. The format is: first, the root node of the animation-list, the animation-list root node contains multiple item subnodes. Each item node defines an animation: The drawable resource of the current frame and the duration of the current frame.

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.