The second method of using Animations
1. Create an anim folder under the res folder.
2. Create an xml file and add the set tag first. Modify the tag as follows:
Android: interpolator = "@ android: anim/accelerate_interpolator">
3. Add the rotate, alpha, scale, or translate label to the label.
4. Use AnimationUtils in the code to load the xml file and generate the Animation object.
Below is the code
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. AnimationUtils;
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 ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. scale );
ImageView. startAnimation (animation );
}
}
Class rotateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. rotate );
ImageView. startAnimation (animation );
}
}
// Fade in and out
Class alphaButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// Use AnimationUtils to load the animation settings file
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. alpha );
ImageView. startAnimation (animation );
}
}
Class translateButtonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. translate );
ImageView. startAnimation (animation );
}
}
}
The following are four xml
Alpha. xml
Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toAlpha = "0.0"
Android: startOffset = "500"
Android: duration = "500">
Rotate. xml
Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toDegrees = "+ 350"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration = "3000">
Scale. xml
Android: interpolator = "@ android: anim/accelerate_interpolator">
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"
>
Translate. xml
Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toXDelta = "100%"
Android: fromYDelta = "0%"
Android: toYDelta = "100%"
Android: duration= "2000"
/>
Note the xml file placement location