Animation Control for Android
Overview:
Android animation effects include: movement, gradient transparency, rotation, and scaling.
There are two ways to implement animation: dynamic implementation in java code and static implementation in xml.
Demo
Dynamic implementation:
/* Opacity gradient of the animation */AlphaAnimation alphaAnimation = new AlphaAnimation (1f, 0); // transparency from 1 to 0 alphaAnimation. setDuration (1000); // the time for completing the gradient alphaAnimation. setStartOffset (200); // response time mImageViewAnim. startAnimation (alphaAnimation); // load animation with an ImageView, make sure that the Image is placed in src or the movement of the background/* animation loaded */TranslateAnimation translateAnimation = // the movement from one coordinate to another. The parameters are in sequence: Start Point Horizontal and vertical coordinates, new TranslateAnimation (-mImageViewAnim. getMeasuredWidth (), 0, 0); translateAnimation. setDuration (1000); translateAnimation. setStartOffset (200); mImageViewAnim. startAnimation (translateAnimation);/* animation rotation */RotateAnimation rotateAnimation = new RotateAnimation (0,360); // rotateAnimation is rotated along the upper left corner by default. setDuration (1000); rotateAnimation. setStartOffset (200); mImageViewAnim. startAnimation (rotateAnimation);/* animation scaling */ScaleAnimation scaleAnimation = new ScaleAnimation (1f, 2f, 1f, 2f); // horizontal and vertical scaleAnimation. setDuration (1000); scaleAnimation. setStartOffset (200); mImageViewAnim. startAnimation (scaleAnimation );
To merge and load an animation, you need an AnimationSet to load all animations:
/*** Declare an AnimationSet, which is a set for storing animation. * false: Use your own interpolator for each animation loaded by this set. * true: all animation functions are an interpolator */AnimationSet animationSet = new AnimationSet (false); AlphaAnimation alphaAnimation = new AlphaAnimation (1f, 0); new TranslateAnimation (0, 0. getMeasuredWidth (), mImageViewAnim. getMeasuredHeight (); RotateAnimation rotateAnimation = new RotateAnimation (0,360, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimation. RELATIVE_TO_SELF, 0.5f); // RELATIVE_TO_SELF indicates that the position is relative to its own ScaleAnimation scaleAnimation = new ScaleAnimation (1f, 2f, 1f, 2f); alphaAnimation. setDuration (1000); translateAnimation. setDuration (1000); rotateAnimation. setDuration (1000); scaleAnimation. setDuration (1000); // Add animations to the animation set. addAnimation (alphaAnimation); animationSet. addAnimation (translateAnimation); animationSet. addAnimation (rotateAnimation); animationSet. addAnimation (scaleAnimation); mImageViewAnim. startAnimation (animationSet );
The running effect is the effect of combining all animations.
For static animation loading, you need to create a new folder anim under the res directory and create a resource file in it. My name is rotation. xml.
Call this resource in java code:
AnimationUtils utils = new AnimationUtils(); Animation animation = utils.loadAnimation(getApplicationContext(),R.anim.rotation); mImageViewAnim.startAnimation(animation);
The running effect is the combination of several animations in several types of xml.