From: http://hi.baidu.com/wendaoeryu/item/203d40efbf86583f5a7cfbcf
1. Introduction to animation
1. Introduction animation provides a series of animation effects for Android: rotation, scaling, movement, fade-in and fade-out. These animation effects can be applied to most controls.
2. Category
Animation can be divided into two types:
Tweenedanimations: gradient animation, which includes rotation, scaling, movement, and fade-in and fade-out.
Frame-by-frameanimations: plays a series of drawable sequences at a time. It is similar to the movie mode and is often used to regularly update the background.
3. Implementation Method
There are two implementation methods for Animation:
Implemented in XML files, the advantage is good reusability and maintainability. Multiple controls can use the same XML file. The disadvantage is that the XML file is not compiled, so troubleshooting is difficult.
In code implementation, the advantage is that troubleshooting is very convenient, but the disadvantage is that there are many duplicate codes and the reusability is low.
Ii. tweenedanimations
1. Effect
ALPHA: fade in and out
Scale: Scale
Rotate: Rotate
Translate: Move
2. Implementation
Implement in code
(1) Create an animationset object (you can soften multiple animation effects)
Animationset is a subclass of animation. An animationset contains a series of animations. the settings of the animationset attribute are applied to every animation.
Animationset = new animationset (true );
(2) Create the corresponding animation object as needed (four sub-classes are available based on the four effects)
Fade in and out: alphaanimation = new alphaanimation (); // two parameters indicate the initial transparency and the target transparency respectively,
// 1 indicates opacity, and 0 indicates full transparency.
Rotation: rotateanimation = new rotateanimation (0,360, animation. relative_to_parent, 1f, animation. relative_to_parent, 1f );
Parameter description:
Parameter 1: initial angle. 0 indicates the current position of the image.
Parameter 2: Target angle.
Parameters 3, 4, 5, and 6 define the center of the rotation
Parameter 3: X coordinate, which has three default values: relative_to_parent
Relative_to_self
Relative_to_absolute absolute coordinates
Parameter 4: X-axis offset ratio, with a change range of 0f-1f
Parameter 5: Y coordinate type, which also has three default values
Parameter 6: Y-axis offset ratio, in the range of 0f-1f
Scaling: scaleanimation = new scaleanimation (1, 0.1f, 1, 0.1f, animation. relative_to_self, 0.5f, animation. relative_to_self, 0.5f );
Parameter description:
Parameter 1, 2: Scale X axis-scale from 1 to 0.1
3, 4: Y-axis scaling-scale from 1 to 0.1
5, 6: X axis (type and proportion)
7, 8: Y axis (type and proportion)
Move: translateanimation = new translateanimation (animation. relative_to_self, 0f, animation. animation, 0.5f, animation. animation, 0f, animation. animation, 0.5f );
Parameter description:
Parameter 1, 2: Start position of the X axis
3, 4: end position of the X axis
Parameter 5, 6: Start position of the Y axis
7, 8: end position of the Y axis
(3) configure data for animation
Here are some general attributes of tweenedanimations.
. Setduration (long durationmills); // specifies the animation execution time, in milliseconds.
. Setfillenabled -- when it is set to true, both fillafter and fillbefroe will be true, and the fillbefore and fillafter attributes will be ignored.
. Setfillafter (Boolean fillafter); // if it is set to true, the animation remains in the final state after completion.
. Setfillbefore (Boolean fillbefore); // if it is set to true, the animation remains in the initial state after completion.
. Setstartoffset (long startoffset); // the waiting time before the animation is executed.
. Setrepeatcount (INT repeatcount); // Number of animation executions
(4) Add the animation object to the animationset
Animationset. addanimation (alphaanimation );
(5) use the control object to start executing the animationset
* **. Startanimation (animationset );
Implement in XML
(1) Create an anim folder under the res directory
(2) create an XML file in anim and add the set tag first:
<Set xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ Android: anim/accelerate_interpolator">
</Set>
(3) You can add rotate, Alpha, scale, or translate tags to the set tag.
Example:
<Rotate
Android: fromdegrees = "0" // start angle
Android: todegrees = "360" // Target angle. The angle here can be a negative number, and the rotation will be counter-clockwise.
Android: Rotate Tx = "% 50" // rotate the center of the X axis, 50, corresponds to relative_to_absolute, 50%, then
// Relative_to_self, 50% P, corresponding to relative_to_parent.
Android: Required ty = "% 50" // rotate the center of the Y axis
>
For more information about the use of these tags, see the android help documentation.
(4) use animationutils in the code to load XML and generate an animation object
Animation animation = animationutils. loadanimation (mainactivity. This, R. anim. ***); // load the specified XML file
3. About interpolator
(1) Introduction
Interpolator defines the animation speed. Several default interpolator types are as follows:
Accelerateinterpolator: the animation starts slowly and then accelerates gradually.
Decelerateinterpolator: the animation starts faster and then gradually slows down.
Acceleratedecelerateinterpolator: the animation starts and ends slowly, and the intermediate process is accelerated.
Linearinterpolator: animation speed.
Cycleinterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.
(2) Implementation
Defined in code:
Animationset = new animationset (true); // if this parameter is set to true
// Applies to all animation objects in the animationset.
Animationset. setinterpolator (New accelerateinterpolator); // set interpolator for the animationset.
Or:
Animationset = new animationset (false );
Animation1. setinterpolator (New accelerateinterpolator );
Animation2.setinterpolator (New accelerateinterpolator );
... // Set interpolator for each animation object.
Defined in XML:
Add attributes to the set tag,
Android: Required interpolator = "true" // if this parameter is set to true, the set interpolator applies to all animations in the tag.
Android: interpolator = "@ Android: anim/accelerate_interpolator" // set interpolator for set.
Or:
Android: Required interpolator = "false" // if it is set to false, you need to add the interpolator attribute for each animation.
Iii. frame-by-frameanimations
1. Implementation
(1) Create an XML file in the drawable Folder:
<? XML version = "1.0" encoding = "UTF-8"?>
<Animation-list xmlms: Android = "http://schemas.android.com/apk/res/android"
Android: onshot = "false">
<Item Android: drawable = "" @ drawable/pic1 Android: Duration = "500"/>
<Item Android: drawable = "" @ drawable/pic2android: Duration = "500"/>
<Item Android: drawable = "" @ drawable/pic3android: Duration = "500"/>
</Animation-List>
This file defines the image resources that require one playback. Each <item> is a resource. The drawable attribute is the resource image, and the duration value is the playback duration.
(2) Add:
Imageview. setbackgroundresource (R. drawable. ***); // load the created XML into imageview
Animationdrawable = (animationdrawable) imageview. getbackground ();
Animationdrawable. Start (); // start the animation.
Iv. layoutanimaioncontroller
1. Introduction
Layoutanimaioncontroller sets an animation effect for the controls in layout or viewgroup. It features that each of the controls has the same animation effect. The animation effects of these controls can be displayed at different times.
2. Implementation (combined with listview)
Layoutanimaioncontroller can also be set in the code or in XML.
In the Code:
(1) Create an animation object. You can use constructors or load XML files.
(2) create a layoutanimaioncontroller object
Layoutanimaioncontroller LAC = new layoutanimaioncontroller (animation );
(3) set attributes
LAC. setorder (layoutanimaioncontroller. order_normal); // you can specify the order.
There are three default sequence:
Layoutanimaioncontroller. order_normal // order
Layoutanimaioncontroller. order_reverse // Reverse Order
Layoutanimaioncontroller. order_random // random
LAC. setdelay (*** F); // sets the animation execution delay time, that is, the interval, in seconds.
(4) set for listview
Listview. setlayoutanimation (LAC );
In the XML file:
(1) Create an XML file in the Res/anim folder
<Layoutanimation xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: delay = "0.5" // The animation execution delay time, that is, the time interval, in seconds.
Android: animationorder = "random"/"normal"/"reverse" // execution sequence
Android: animation = "@ anim/***" // This is an XML file that loads a specific animation.
(2) Configure attributes in a specific layout File
Android: layoutanimation = "@ anim /***"
5. animationlistener
1. Introduction
The animationlistener is a listener that is notified at all stages of animation execution and calls corresponding methods.
2. Method
(1) onanimationstart (animation) is called at the beginning of the animation.
(2) onanimationend (animation) is called at the end of the animation.
(3) onanimationrepeate (animation) is called when the animation is repeated.
3. Implementation
(1) construct the listener
Private class myanimationlistener implements animationlistener {
Three methods are overwritten.
}
(2) Use
Animation. setanimationlistener (New myanimationlistener ());