Animations is a set of animation implementation APIs.
Animations Classification
1. tweened animations gradient animation and compensation Animation
Provides effects such as rotation, movement, stretching, and fading out.
2. frame-by-frame animations
You can create a drawable sequence, which can be displayed one by one based on the specified time interval.
Tweened animations category
1. ALPHA: fade in and fade out
2. Scale: Scaling Effect
3. Rotate: rotation effect
4. Translate: Moving Effect
The first method of using animations:
Procedure for using tweened animations
1. Create an animationset object
2. Create an animation object as needed
3. Set the corresponding data for the animation object based on the needs of the software animation.
4. Add the animation object to the animationset object.
5. Use the control object to start executing the animationset
The following four sub-classes of Animation:
Alphaanimation, translateanimation, scaleanimation, rotateanimation
Private class rotatebuttonlistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// Create an animationset object
Animationset = new animationset (true );
// Create a rotateanimation object
Rotateanimation = new rotateanimation (0,360,
Animation. relative_to_parent, 0.5f,
Animation. relative_to_parent, 0.2f); // The X coordinate of the rotation point is 0.5 times the width of the parent control, and Y coordinate is 0.2 times the length of the parent control.
// Set the animation execution time (unit: milliseconds)
Rotateanimation. setduration (5000 );
// Add the rotateanimation object to the animationset
Animationset. addanimation (rotateanimation );
// Start the animation
Imageview. setanimation (animationset );
}
}
Private class scalebuttonlistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// Create an animationset object
Animationset = new animationset (true );
// Create a scaleanimation object
Scaleanimation = new scaleanimation (1, 0.1f, 1, 0.1f,
Animation. relative_to_self, 0.5f,
Animation. relative_to_self, 0.5f );
// Set the animation execution time (unit: milliseconds)
Scaleanimation. setduration (5000 );
// Add the scaleanimation object to the animationset
Animationset. addanimation (scaleanimation );
// Start the animation
Imageview. setanimation (animationset );
}
}
Private class alphabuttonlistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// Create an animationset object
Animationset = new animationset (true );
// Create an alphaanimation object
Alphaanimation = new alphaanimation (1, 0 );
// Set the animation execution time (unit: milliseconds)
Alphaanimation. setduration (1000 );
// Add the alphaanimation object to the animationset
Animationset. addanimation (alphaanimation );
// Start the animation
Imageview. setanimation (animationset );
}
}
Private class translatebuttonlistener implements onclicklistener {
@ Override
Public void onclick (view v ){
// Create an animationset object
Animationset = new animationset (true );
// Create a translateanimation object
Translateanimation = new translateanimation (
Animation. relative_to_self, 0f,
Animation. relative_to_self, 0.5f,
Animation. relative_to_self, 0f,
Animation. relative_to_self, 1.0f );
// Set the animation execution time (unit: milliseconds)
Translateanimation. setduration (5000 );
// Add the translateanimation object to the animationset
Animationset. addanimation (translateanimation );
// Start the animation
Imageview. setanimation (animationset );
}
}
General Attributes of tween animation:
1. setduration (long durationmills)
Set the animation duration (unit: milliseconds)
2. setfillafter (Boolean fillafter)
If the value of fillafter is true, after the animation is executed, the control remains in the execution end state.
3. setfillbefore (Boolean fillbefore)
If the value of fillbefore is true, after the animation is executed, the control returns to the State before the animation is executed.
4. setstartoffset (long startoffset)
Set the wait time before the animation is executed.
5. setrepeatcount (INT repeatcount)
Set the number of repeated animation executions
The second method of using animations:
1. Create a folder named anim under the res folder.
2. Create an XML file and add the set tag as follows:
<Set xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: interpolator = "@ Android: anim/accelerate_interpolator">
</Set>
Add the rotate, Alpha, scale, or translate label to the label.
3. Use animationutils in the code to load the XML file and generate the animation object.
Alpha XML file compilation method Alpha. xml
<Alpha Android: fromalpha = "0.1"
Android: toalpha = "0.0"
Android: startoffset = "500"
Android: Duration = "500"/>
Code:
Animation animation = animationutils. loadanimation (mainactivity. This, R. anim. Alpha );
Imageview. startanimation (animation );
Rotate. xml
<Rotate Android: fromdegrees = "0"
Android: todegrees = "+ 350"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: Duration = "3000"/>
There are three ways to set the value of Android: strongtx:
1. Android: uplotex = "50". This method uses absolute location.
2. Android: vertex Tex = "50%", which is located relative to the control
3. Android: Using Tex = "50% P", this method locates relative to the control's parent control.
How to compile the XML file of translate
<Translate Android: fromxdelta = "50%"
Android: toxdelta = "100%"
Android: fromydelta = "0%"
Android: toydelta = "100%"
Android: Duration = "2000"/>
Method for compiling scale XML files
<Scale Android: fromxscale = "1.0"
Android: toxscale = "0.0"
Android: fromyscale = "1.0"
Android: toyscale = "0.0"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: Duration = "2000"/>
Animationset
1. animationset is a subclass of animation.
2. An animationset contains a series of animations.
3. Set some common attributes of animation (such as startoffset and duration) for the animationset, which can be integrated into the animation set.
Add multiple animations to the animationset
// Create an animationset object
Animationset = new animationset (true );
// Create a rotateanimation object
Rotateanimation = new rotateanimation (0,360,
Animation. relative_to_parent, 0.5f,
Animation. relative_to_parent, 0.2f); // The X coordinate of the rotation point is 0.5 times the width of the parent control, and Y coordinate is 0.2 times the length of the parent control.
// Create an alphaanimation object
Alphaanimation = new alphaanimation (1, 0 );
// Add the alphaanimation object to the animationset
Animationset. addanimation (alphaanimation );
// Set the animation execution time (unit: milliseconds)
Animationset. setduration (5000 );
// Add the rotateanimation object to the animationset
Animationset. addanimation (rotateanimation );
// Start the animation
Imageview. setanimation (animationset );
You can also define multiple animation effects in the layout file when loading the layout file.
What is interpolator?
Interpolator defines the speed at which an animation changes. In the animations framework, it defines the following types of interpolator:
1. acceleratedecelerateinterpolator
2. accelerateinterpolator: speed changes are slow at the beginning of the animation, and acceleration starts.
3. cycleinterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.
4. decelerateinterpolator: The speed changes slowly at the beginning of the animation, and then starts to slow down.
5. linearinterpolator: the animation changes at an even rate.
Set Android: interpolator =... in XML ....
Set in the Code: (alphaanimation, animationset, etc.) animation. setinterpolator (New decelerateinterpolator ());
How to use frame-by-frame animations:
Create an XML file (such as anim_nv.xml) in res/drawable to define the animation sequence of animations:
<Animation-List>
<Item/>
<Item/>
</Animation-List>
In the Code:
Imageview. setbackgroundresource (R. drawable. anim_nv );
Animationdrawable = (animationdrawable) imageview. getbackground ();
Animationdrawable. Start ();
What is layoutanimationcontroller?
1. layoutanimationcontroller is used to set animation effects for controls in a layout or viewgroup.
2. Each widget has the same animation effect.
3. The animation effects of these controls are displayed at different times.
4. layoutanimationcontroller can be set in the XML file or in the code.
Use layoutanimationcontroller in XML
1. Create a new file named list_anim_layout.xml In the Res/anim Folder:
<Layoutanimation xmlns: Android = http://schemas.android.com/apk/res/android
Android: delay = "0.5"
Android: animationorder = "random"
Android: animation = "@ anim/list_anim"/>
2. Add the following configuration for listview in the layout file:
Android: layoutanimation = "@ anim/list_anim_layout"
Use layoutanimationcontroller in the code
1. Create an animation object:
You can create an animation object by loading an XML file or directly using the animation constructor.
Animation animation = (animation) animationutils. loadanimation (mainactivity. This, R. anim. list_item );
2. Use the following code to create a layoutanimationcontroller object:
Layoutanimationcontroller LAC = new layoutanimationcontroller (animation );
3. Set the display sequence of controls:
LAC. setorder (layoutanimationcontroller. order_normal );
4. Set the layoutanimationcontroller attribute for listview:
Listview. setlayoutanimation (LAC );
What is animationlistener?
1. animationlistener is a listener.
2. the listener will be notified at various stages of animation execution, so as to call the corresponding method.
3. It mainly includes the following three methods:
A) onanimationend (animation)
B) onanimationrepeat (animation)
C) onanimationstart (animation)
Animation. setanimationlistener ()