1. How to Use the AnimationSet
What is AnimationSet?
1. The AnimationSet is a subclass of Animation.
2. An AnimationSet contains a series of Animation
3. Set some common attributes of Animation (startOffset, duration, etc.) for the AnimationSet, which can be integrated into the Animation set.
2. How to Use Interpolator
What is Interpolator?
Interpolator defines the animation change rate. In the Animations framework, it defines several types of Interpolator
AccelerateDecelerateInterpolator: The speed change between the animation start and end is slow, and acceleration is enabled in the middle.
AccelerateInterpolator: speed changes are slow at the beginning of the animation and acceleration starts.
CycleInterpolator: specifies the number of times the animation is played cyclically, and the speed changes along the sine curve.
DecelerateInterpolator: speed changes are slow at the beginning of the animation, and then start to slow down
LinearInterpolator: the animation changes at a uniform speed
This is to achieve the effects of two animations at the same time:
MainActivity. java
Package com. yx. animations03;
Import com. yx. animations03.R;
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. AccelerateDecelerateInterpolator;
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 button = null;
Private ImageView imageView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button = (Button) findViewById (R. id. buttonId );
Button. setOnClickListener (new buttonListener ());
ImageView = (ImageView) findViewById (R. id. imageViewId );
}
Class buttonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
/* // Multiple animation effects, method 1
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. flishes );
ImageView. startAnimation (animation );
*/
// Method 2
AnimationSet animationSet = new AnimationSet (true); // true indicates sharing an Interpolator.
AnimationSet. setInterpolator (new AccelerateDecelerateInterpolator ());
ScaleAnimation scaleAnimation = new ScaleAnimation (1, 0.1f, 1, 0.1f,
Animation. RELATIVE_TO_SELF, 0.5f,
Animation. RELATIVE_TO_SELF, 0.5f
);
RotateAnimation rotateAnimation = new RotateAnimation (0,360,
Animation. RELATIVE_TO_PARENT, 1f,
Animation. RELATIVE_TO_PARENT, 0f
);
AnimationSet. addAnimation (rotateAnimation );
AnimationSet. addAnimation (scaleAnimation );
AnimationSet. setDuration (2000 );
AnimationSet. setStartOffset (500 );
ImageView. startAnimation (animationSet );
}
}
}
The flishes. xml file is in the anim created under res.
Android: interpolator = "@ android: anim/accelerate_interpolator"
Android: Required interpolator = "true"
>
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"
>
Android: toAlpha = "0.0"
Android: startOffset = "500"
Android: duration = "500">
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/buttonId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: text = "test 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"/>