Simple use of three types of animations and three types of animations

Source: Internet
Author: User

Simple use of three types of animations and three types of animations
Preface

Android animations can be divided into three types: Frame Animation, View animation, and attribute animation. Our common animations include translation, scaling, rotation, and transparency conversion. We can also customize some complex animations to meet our needs. Today, we will briefly introduce the simple use of Frame Animation, View animation, and attribute animation.

Frame Animation

The so-called Frame Animation is a pre-set picture for playing a frame at a time, just like playing an old-fashioned movie. Android provides an AnimationDrawable class to use Frame Animation. First, we need to use XML to define an AnimationDrawable.
The file path is res/drawable/xxx. xml (if the drawable directory does not exist, you can create it manually)

 
         
     
      
    
   
  
 

Oneshot indicates whether the animation is played only once. Set the above drawable to the background of the target object.

Play the animation code:

AnimationDrawable background = (AnimationDrawable) test.getBackground();background.start();

Note that if the image size is large, OOM may be generated.

View animation

View Animation has four common effects: translation, contraction, rotation, and transparency change, which correspond to four sub-classes of Animation: TanslateAnimation, ScaleAnimation, RotateAnimation, and AlphaAnimation. These four types of animations can be implemented not only by using XML files, but also by code.

Related attributes

XML definition

File Path res/anim/xxx. xml (if the anim directory does not exist, you can create it manually)

 
     
     
      
       
            
    
   
  
 

This is a combination of animation sets. You can also name a single animation.

     

Animation usage:
Animation animation = AnimationUtils. loadAnimation (this, R. anim. test );
Animation. setAnimationListener (new Animation. AnimationListener (){
@ Override
Public void onAnimationStart (Animation animation ){
}

@ Override
Public void onAnimationEnd (Animation animation ){
}

@ Override
Public void onAnimationRepeat (Animation animation ){
}
});
View. startAnimation (animation );
You can add an animation listener to perform the required operations.

Code definition

Pan Animation

/** * Constructor to use when building a TranslateAnimation from code *  * @param fromXType Specifies how fromXValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param fromXValue Change in X coordinate to apply at the start of the *        animation. This value can either be an absolute number if fromXType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toXType Specifies how toXValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param toXValue Change in X coordinate to apply at the end of the *        animation. This value can either be an absolute number if toXType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param fromYType Specifies how fromYValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param fromYValue Change in Y coordinate to apply at the start of the *        animation. This value can either be an absolute number if fromYType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toYType Specifies how toYValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param toYValue Change in Y coordinate to apply at the end of the *        animation. This value can either be an absolute number if toYType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. */public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,        int fromYType, float fromYValue, int toYType, float toYValue)

Scale-down Animation

 /** * Constructor to use when building a ScaleAnimation from code *  * @param fromX Horizontal scaling factor to apply at the start of the *        animation * @param toX Horizontal scaling factor to apply at the end of the animation * @param fromY Vertical scaling factor to apply at the start of the *        animation * @param toY Vertical scaling factor to apply at the end of the animation * @param pivotX The X coordinate of the point about which the object is *        being scaled, specified as an absolute number where 0 is the left *        edge. (This point remains fixed while the object changes size.) * @param pivotY The Y coordinate of the point about which the object is *        being scaled, specified as an absolute number where 0 is the top *        edge. (This point remains fixed while the object changes size.) * @param pivotXType Specifies how pivotXValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param pivotXValue The X coordinate of the point about which the object *        is being scaled, specified as an absolute number where 0 is the *        left edge. (This point remains fixed while the object changes *        size.) This value can either be an absolute number if pivotXType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param pivotYType Specifies how pivotYValue should be interpreted. One of *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or *        Animation.RELATIVE_TO_PARENT. * @param pivotYValue The Y coordinate of the point about which the object *        is being scaled, specified as an absolute number where 0 is the *        top edge. (This point remains fixed while the object changes *        size.) This value can either be an absolute number if pivotYType *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. */public ScaleAnimation(float fromX, float toX, float fromY, float toY)public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY)public ScaleAnimation(float fromX, float toX, float fromY, float toY,        int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

Rotating Animation

public RotateAnimation(float fromDegrees, float toDegrees)public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,        int pivotYType, float pivotYValue)

Transparency Animation

public AlphaAnimation(float fromAlpha, float toAlpha)

The above are the constructors of the four types of animations. Relevant parameter meanings are included in the comments. You can choose the appropriate constructors to define the relevant animations. This is also very simple to use.

AlphaAnimation animation = new AlphaAnimation(0,1);animation.setDuration(1000);view.setAnimation(animation);

AnimationSet set = new AnimationSet () can combine multiple animations and add them to the set for execution.

AnimationSet set = new AnimationSet(true);xxxAnimation xxx = new xxxAnimation();yyyAnimation yyy = new yyyAnimation();zzzAnimation zzz = new zzzAnimation();set.addAnimation(xxx);set.addAnimation(yyy);set.addAnimation(zzz);set.setDuration(1000);view.setAnimation(set);

The animation time can be set separately or together for each animation. The true value in the set constructor indicates whether the interpolation tool is shared, that is, whether the animation change rate is the same, the default interpolation tool is accelerate_decelerate_interpolator, which is described in the following section.

Property Animation

ObjectAnimator is a subclass of ValueAnimator. ValueAnimator only provides a variation process and value. It is not used for animation of a specific control, but sometimes it can be used to perform corresponding operations.

ValueAnimator animator = ValueAnimator.ofInt(1, 4);animator.setInterpolator(new LinearInterpolator());animator.setDuration(500);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (int) animation.getAnimatedValue(); switch (value){ case 1: showAnimation(mRightIcon); break; case 2: showAnimation(mIcon); showAnimation(mAdChoice); showAnimation(mAdIcon); break; case 3: showAnimation(mLeftIcon); break; case 4: showAnimation(mSecondIcon); break; default: break; } }});animator.start();

ValueAnimator.ofFloat(float… values)

 /** * Constructs and returns a ValueAnimator that animates between float values. A single * value implies that that value is the one being animated to. However, this is not typically * useful in a ValueAnimator object because there is no way for the object to determine the * starting value for the animation (unlike ObjectAnimator, which can derive that value * from the target object and property being animated). Therefore, there should typically * be two or more values. * * @param values A set of values that the animation will animate between over time. * @return A ValueAnimator object that is set up to animate between the given values. */

In the ObjectAnimator constructor, you can add the animation execution control.

public static ObjectAnimator ofFloat(Object target, String propertyName, float… values)

public static ObjectAnimator ofInt(Object target, String propertyName, int… values)

Here we will only introduce these two types of structures. Others will be introduced later.

 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", -200f, 20.0f, 0f); animator.setDuration(1000); animator.setInterpolator(new LinearInterpolator()); animator.start();

AnimatorSet attribute animation set

AnimatorSet set = new AnimatorSet (); set. playTogether (anim1, anim2); // execute set together. play (anim3 ). after (anim4); // anim3 runs set after anim4 is executed. setduration( 1000); set. addListener (new AnimatorListenerAdapter () {}); set. setInterpolator (new DecelerateInterpolator (); set. start ();

The simple use of the three types of animations is described here. We will introduce the animations in more detail later.

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.