Android animations animation, androidanimations
Android provides two mechanisms for creating simple animations: tweedned animation (gradient animation) and frame-by-frame animation (frame-by-frame animation, khan !!!) . Here we mainly introduce four animation formats in tweedned animation: alpha (Fade-in and fade-out effect), scale (scale), rotate (rotate), and translate (PAN ).
So how can this effect be achieved? Let's look at the following: (this is implemented inside the code. Although it is easy to see, it is not conducive to maintenance and reuse. Here we will introduce a method for implementing it externally)
Internal implementation:
1. Create an AnimationSet object
2. Create an Animation object that you want to implement, such as AlphaAnimations.
3. Set Some attributes for the animation
4. Add the alpha object to the AnimationSet.
5. Execute the animation set for the image.
Private class alphaButtonListener implements OnClickListener {@ Override public void onClick (View v) {// create an AnimationSet object AnimationSet animationSet = new AnimationSet (true); // create an AlphaAnimation object, set the transparency to 1 to 0 AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0); // set the duration of the entire animation alphaAnimation. setDuration (10000); // Add the Animation object to the animationSet object. addAnimation (alphaAnimation); // start to execute AnimationSet imagView on the image. startAnimation (animationSet );}}
======================================
This section describes how to set some common animation attributes.
- SetDuration(Long durationMillis): Used to set the animation duration, in milliseconds.
- SetFillAfter: If it is set to true, the animation will be exclusively applied after the animation ends. That is to say, the animation will stay in the end state.
- SetFillBefore: If it is set to true, apply the animation conversion before the animation starts. (This system is set to true by default) (I don't quite understand this. On the Internet, some say that it is not right to return to the starting position after the animation is set. If you know it, leave a message to tell me)
- SetStartOffset: Set the time after which the animation starts to run after the instruction of starting the animation.
- SetRepeatMode: Set the number of animation repetitions.
======================================
The following is the specific ManiActivity. java code, which gives a detailed explanation of each Animation:
Package com. mecury. animationstest; import android. app. activity; import android. OS. bundle; 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 ImageView imagView; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; @ Override protected void onCreate (Bundle detail) {super. onCreate (savedInstanceState); setContentView (R. layout. activi Ty_main); imagView = (ImageView) findViewById (R. id. imageView); alphaButton = (Button) findViewById (R. id. alphaButton); alphaButton. setOnClickListener (new alphaButtonListener (); scaleButton = (Button) findViewById (R. id. scaleButton); scaleButton. setOnClickListener (new scaleButtonListener (); rotateButton = (Button) findViewById (R. id. rotateButton); rotateButton. setOnClickListener (new rotateButtonList Ener (); translateButton = (Button) findViewById (R. id. translateButton); translateButton. setOnClickListener (new translateButtonListener ();}/** fade in/out effect */private class alphaButtonListener implements OnClickListener {@ Override public void onClick (View v) {// create an AnimationSet object AnimationSet animationSet = new AnimationSet (true); // create an AlphaAnimation object and set the transparency to 1 to 0 AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0); // sets the duration of the entire animation. setDuration (10000); // Add the Animation object to the animationSet object. addAnimation (alphaAnimation); // start to execute AnimationSet imagView on the image. startAnimation (animationSet);}/** scaling effect */private class scaleButtonListener implements OnClickListener {@ Override public void onClick (View v) {AnimationSet animationSet = new AnimationSet (true ); /* here the percentage representation is used, and f indicates the floating point type. * The four parameters represent the scaling ratio. The first two parameters represent the zooming of the X axis (here the percent representation is used: 1f represents the original width, and 0.5f represents half of the original width) the last two represent the scaling of the Y axis * Animation, which has two common attributes: RELATIVE_TO_SELF (dependent on itself) and RELATIVE_TO_PARENT (dependent on the parent control) * The following four axes indicate that the zooming axis depends on its own in the place where x is 0.5f and y is 0.5f */ScaleAnimation scaleAnimation = new ScaleAnimation (1f, 0.1f, 1f, 0.1f, and Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); animationSet. addAnimation (scaleAnimation); animationSet. setDuration (1, 2000); I MagView. startAnimation (animationSet);}/** rotation effect */private class rotateButtonListener implements OnClickListener {@ Override public void onClick (View v) {AnimationSet animationSet = new AnimationSet (true ); /* The first parameter indicates the angle of the image relative to the starting position. * The second parameter indicates the deflection angle of the image when the animation ends. Here, the four parameters after 0 to 360, that is, the origin *, are used to set the axis around which the image is rotated. This means that the horizontal axis of the axis is dependent on the 0f of the parent control, the ordinate is dependent on the parent control-1f * Here we will talk about the coordinate issue. In this animation, the coordinate origin is the upper left corner of the image, the left X axis and the lower Y axis of the origin point represent the positive axis * The coordinate axes of the surrounding points are on the top of the image */RotateAnimation rotateAnimation = new RotateAnimation (0,360, Animation. RELATIVE_TO_PARENT, 0f, Animation. RELATIVE_TO_PARENT,-1f); rotateAnimation. setDuration (2000); animationSet. addAnimation (rotateAnimation); imagView. startAnimation (animationSet);}/** mobile effect */private class translateButtonListener implements OnClickListener {@ Override public void onClick (View v) {AnimationSet animationSet = new AnimationSet (true ); /** the first two parameters determine the starting position of the X axis of the image when the animation starts. * The third four parameters indicate the X axis at which the image will arrive. * The last four parameters indicate the Y axis, which is similar to the preceding code. * indicates: shift from the starting position to the upper right corner */TranslateAnimation translateAnimation = new TranslateAnimation (Animation. RELATIVE_TO_SELF, 0f, Animation. RELATIVE_TO_PARENT, 1f, Animation. RELATIVE_TO_SELF, 0f, Animation. RELATIVE_TO_PARENT,-1f); translateAnimation. setDuration (2000); animationSet. addAnimation (translateAnimation); imagView. startAnimation (animationSet );}}}
The following is my layout file:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = "com. mecury. animationstest. mainActivity "> <Button android: id =" @ + id/translateButton "android: layout_width =" match_parent "android: layout_height =" 40dp "android: layout_alignParentBottom =" true "android: text = "Translate translation"/> <Button android: id = "@ + id/rotateButton" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_above = "@ id/translateButton" android: text = "Rotate rotation"/> <Button android: id = "@ + id/scaleButton" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_above = "@ id/rotateButton" android: text = "Scale scaling"/> <Button android: id = "@ + id/alphaButton" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_above = "@ id/scaleButton" android: text = "Alpha fade-in and fade-out"/> <ImageView android: id = "@ + id/imageView" android: layout_width = "50dp" android: layout_height = "50dp" android: layout_centerInParent = "true" android: src = "@ drawable/china"/> </RelativeLayout>
Implementation in external resources:
1. Create a new file named anim In the res file.
2. Create a. xml file in the file (alpha is used as an example here), for example, alpha. xml
3. Set the corresponding attributes in the file
4. Use AnimationUtils to load the animation settings file
Here are four animation code files in my anim:
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Aplha is a fade-in and fade-out effect startOffser Time offset that pauses for 500 milliseconds before the animation starts --> <set xmlns: android = "http://schemas.android.com/apk/res/android"> <alpha android: fromAlpha = "1" android: toAlpha = "0" android: startOffset = "500" android: duration = "2000"/> </set> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Scale is used to achieve scaling privat. It is used to set the zooming around the axis. privatX = "50" uses absolute position = "% 50" and locates relative to the control, locate the position of = "1/2 p" at the control's 50% point relative to the parent control --> <scale android: fromXScale = "1" android: toXScale = "0.1" android: fromYScale = "1" android: toYScale = "0.1" android: shorttx = "50%" android: shortty = "50%" android: startOffset = "500" android: duration = "2000"/> </set> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <rotate android: fromDegrees = "0" android: toDegrees = "+ 360" android: Export Tx = "100% p" android: ty = "0% p" android: startOffset = "500" android: duration = "2000"/> </set> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <translate android: fromXDelta = "0%" android: toXDelta = "100% p" android: fromYDelta = "0%" android: toYDelta = "-100% p" android: interpolator = "@ android: anim/decelerate_interpolator" android: startOffset = "500" android: duration = "30000"/> </set>
Intepolator is used to set the animation execution speed.
Accelerate_Decelerate_Interpolator
Accelerate_Interpolator: The acceleration speed starts slowly at the beginning of the animation.
Cycle_Interpolator: The speed changes the number of times played cyclically along the animation, with a sine curve
Deceletrate_Interpolator: at the beginning of the animation, the speed changes slowly and then suddenly slows down.
Linear_Interpolator: the animation changes at a constant speed.
Use: android: Interpolator = "@ android: anim /....."
MianActivity. java:
Package com. example. animation_01; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity {private ImageView imagView; private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button translateButton; @ Override protected void onCreate (Bundle detail) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imagView = (ImageView) findViewById (R. id. imageView); alphaButton = (Button) findViewById (R. id. alphaButton); alphaButton. setOnClickListener (new alphaButtonListener (); scaleButton = (Button) findViewById (R. id. scaleButton); scaleButton. setOnClickListener (new scaleButtonListener (); rotateButton = (Button) findViewById (R. id. rotateButton); rotateButton. setOnClickListener (new rotateButtonListener (); translateButton = (Button) findViewById (R. id. translateButton); translateButton. setOnClickListener (new listener ();} class alphaButtonListener implements OnClickListener {@ Override public void onClick (View v) {// use AnimaionUtils to load the Animation setting file animation Animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. alpha); imagView. startAnimation (animation);} class scaleButtonListener implements OnClickListener {@ Override public void onClick (View v) {Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. scale); imagView. startAnimation (animation);} class rotateButtonListener implements OnClickListener {@ Override public void onClick (View v) {Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. rotate); imagView. startAnimation (animation);} class translateButtonListener implements OnClickListener {@ Override public void onClick (View v) {Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. translate); imagView. startAnimation (animation );}}}
The layout file is the same as the first layout file.