Simple use of view animation in Android

1. Transparency animation

AlphaAnimation aa = new AlphaAnimation(0, 1);//The first parameter is the transparency at the beginning, and the second is the transparency at the end
                                                                //0 is fully transparent, 1 is opaque
                aa.setDuration(1000); //Set the animation duration
                alpha.startAnimation(aa);


2. Rotation animation

The parameters of the rotation animation are slightly more, but in general it is not difficult to understand

 

/* The first and second parameters determine the angle of rotation
                * Other parameters are used to determine the center of rotation
                *There are two rotation methods: RotateAnimation.RELATIVE_TO_SELF and RotateAnimation.RELATIVE_TO_PARENT
                * Respectively means centering on itself or centering on the parent layout
                * The two floating point numbers represent the points on the X and Y axes respectively *
                * */
                RotateAnimation ra = new RotateAnimation(0, 360,
                        RotateAnimation.RELATIVE_TO_SELF,1f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                ra.setDuration(1000);
                rotate.startAnimation (ra);

 

The rotation center of the parameter in the sample code is shown as the red dot in the following figure:

 

 

3. Displacement animation

 
 
  /*The 4 parameters are: the x-axis variable at the beginning, the x-axis variable at the end, the y-axis variable at the beginning, and the y-axis variable at the end*/
                TranslateAnimation ta = new TranslateAnimation(100, 200,
                        0, 300);
                ta.setDuration(1000);
                translate.startAnimation (ta);

4. Zoom animation


/* The 4 parameters are: the multiple at the beginning of the width, the multiple at the end of the width, the multiple at the beginning of the height, the multiple at the end of the height */
                ScaleAnimation sa = new ScaleAnimation (0, 2, 0, 2);
                sa.setDuration (1000);
                scale.startAnimation (in);

5. Animation collection

AnimationSet as = new AnimationSet(true);
                TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 300);
                translateAnimation.setDuration(1000);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
                alphaAnimation.setDuration(1000);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2);
                scaleAnimation.setDuration(1000);
                as.addAnimation (alphaAnimation);
                as.addAnimation(scaleAnimation);
                as.addAnimation(translateAnimation);
                set.startAnimation(as);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325567452&siteId=291194637