Animation Control for Android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.