Animations provides a series of animation effects that can be applied to most controls.
Animations can be divided into two categories:
Category 1: Tweened Animations
This type of Animations provides rotation, movement, stretching, and fade-out effects
Class 2: Frame-by-Frame Animations
This class of Animations 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 effect
2. Scale: Scaling Effect
3. Rotate: rotation effect
4. Translate: Moving Effect
Procedure for using Tweened Animations
1. Create an AnimationSet object
2. Create the corresponding 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
General Attributes of Tween Animations
// Set the animation execution time to 1 s.
AnimationSet. setDuration (1000 );
// If it is true, after the animation is executed, the execution ends.
AnimationSet. setFillAfter (true );
// If it is true, after the animation is executed, it will stop at the start of the execution.
AnimationSet. setFillBefore (false );
// The duration (in milliseconds) before execution)
AnimationSet. setStartOffset (1000 );
// Number of executions
AnimationSet. setRepeatCount (2 );
The following is an example Program
Activity_main.xml
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
Android: id = "@ + id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: text = "test the scale animation effect"
/>
Android: id = "@ + id/rotateButton"
Android: layout_above = "@ id/scaleButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "testing the rotate animation effect"
/>
Android: id = "@ + id/alphaButton"
Android: layout_above = "@ id/rotateButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "testing Alpha animation effect"
/>
Android: id = "@ + id/translateButton"
Android: layout_above = "@ id/alphaButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "testing the Translate animation effect"
/>
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignLeft = "@ + id/scaleButton"
Android: layout_alignParentTop = "true"
Android: orientation = "vertical">
Android: id = "@ + id/imageViewId"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margin = "50px"
Android: src = "@ drawable/ic_launcher"/>
MainActivity. java
Package com. yx. animations01;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. AlphaAnimation;
Import android. view. animation. Animation;
Import android. view. animation. AnimationSet;
Import android. view. animation. RotateAnimation;
Import android. view. animation. ScaleAnimation;
Import android. view. animation. TranslateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;
Public class MainActivity extends Activity {
Private Button scaleButton = null;
Private Button rotateButton = null;
Private Button alphaButton = null;
Private Button translateButton = null;
Private ImageView imageView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
ScaleButton = (Button) findViewById (R. id. scaleButton );
ScaleButton. setOnClickListener (new scaleButtonListener ());
RotateButton = (Button) findViewById (R. id. rotateButton );
RotateButton. setOnClickListener (new rotateButtonListener ());
AlphaButton = (Button) findViewById (R. id. alphaButton );
AlphaButton. setOnClickListener (new alphaButtonListener ());
TranslateButton = (Button) findViewById (R. id. translateButton );
TranslateButton. setOnClickListener (new translateButtonListener ());
ImageView = (ImageView) findViewById (R. id. imageViewId );
}
Class scaleButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
/* Parameters
FromX Horizontal scaling factor to apply at the start of the animation
ToX Horizontal scaling factor to apply at the end of the animation
FromY Vertical scaling factor to apply at the start of the animation
ToY Vertical scaling factor to apply at the end of the animation
Invalid txtype Specifies how should txvalue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
When txvalue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size .) this value can either be an absolute number if your txtype is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
Inclutytype Specifies how inclutyvalue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
Specified tyvalue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size .) this value can either be an absolute number if your tytype is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
ScaleAnimation 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 to 1 s.
ScaleAnimation. setDuration (1000 );
// Add alphaAnimation to the animationSet
AnimationSet. addAnimation (scaleAnimation );
// The setAnimation (animationSet) of the imageView executes the animation.
ImageView. setAnimation (animationSet );
}
}
Class rotateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
/**
* Parameters
FromDegrees Rotation offset to apply at the start of the animation.
ToDegrees Rotation offset to apply at the end of the animation.
Invalid txtype Specifies how should txvalue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
When txvalue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. this value can either be an absolute number if your txtype is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
Inclutytype Specifies how inclutyvalue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
Specified tyvalue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. this value can either be an absolute number if your tytype is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
// 1. From which angle 2. To which angle 3. the X axis coordinate type can be set to Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
// 4. 1f indicates that the abscissa 5-6 of a parent control is the same as 3-4
RotateAnimation rotateAnimation = new RotateAnimation (0,360,
Animation. RELATIVE_TO_PARENT, 1f,
Animation. RELATIVE_TO_PARENT, 0f
);
RotateAnimation. setDuration (1000 );
AnimationSet. addAnimation (rotateAnimation );
// The setAnimation (animationSet) of the imageView executes the animation.
ImageView. setAnimation (animationSet );
}
}
// Fade in and out
Class alphaButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
// Create an AlphaAnimation object. The angle brackets indicate from what transparency to what transparency. 1 indicates opacity, and 0 indicates transparency.
AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0 );
// Set the animation execution time to 1 s.
AlphaAnimation. setDuration (1000 );
// Add alphaAnimation to the animationSet
AnimationSet. addAnimation (alphaAnimation );
// The setAnimation (animationSet) of the imageView executes the animation.
ImageView. setAnimation (animationSet );
}
}
Class translateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Create an AnimationSet object
AnimationSet animationSet = new AnimationSet (true );
/*
* Parameters
FromXType Specifies how fromXValue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
FromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
ToXType Specifies how toXValue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
ToXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
FromYType Specifies how fromYValue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
FromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
ToYType Specifies how toYValue shocould be interpreted. One of Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, or Animation. RELATIVE_TO_PARENT.
ToYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
TranslateAnimation 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 to 1 s.
AnimationSet. setDuration (1000 );
AnimationSet. setFillAfter (true );
AnimationSet. setFillBefore (false );
AnimationSet. setStartOffset (1000 );
AnimationSet. setRepeatCount (2 );
// Add alphaAnimation to the animationSet
AnimationSet. addAnimation (translateAnimation );
// The setAnimation (animationSet) of the imageView executes the animation.
ImageView. setAnimation (animationSet );
}
}
}