Android uses property animation to generate an animation between substitutes

Source: Internet
Author: User

Android uses property animation to generate an animation between substitutes

This document describes how to use property animation in Android (I ).

Before Android, there were two types of animations: frame Animation and tween animation, if you are not familiar with these two types of animations, you can refer to my previous articles about android frame Animation and android tween animation. frame Animation is a frame-by-frame animation, multiple images are put together for continuous playback to achieve an animation effect similar to a GIF image. a tween animation is a complementary animation that mainly translates, scales, rotates, and changes the transparency of the image.

The tween Animation is called View Animation. The first sentence is very clear. You can use View Animation System to implement the tween Animation on The View. That is to say, the tween animation can only work on the view, but what if we want to change the color of a custom View? Sorry, tween cannot.

Android3.0 introduces a new animation implementation method, namely attribute animation and attribute animation. As the name suggests, it is applied to the View attributes. We can make the View attributes achieve the animation effect.

Speaking of this, we have to introduce the ObjectAnimator class, which is also the most used in actual development and tracing. We found that ObjectAnimator inherits from ValueAnimator and ValueAnimator can help us implement some simple numerical changes, however, this stuff has not been used so far.

Next, let's take a look at how to use property animation to achieve the tween animation effect:

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, alpha, 1f,                    0.5f);            animator.setDuration(5000);            animator.start();

We use the ofFloat method to obtain an ObjectAnimator instance. First, let's look at the source code of this method:

    public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {        ObjectAnimator anim = new ObjectAnimator(target, propertyName);        anim.setFloatValues(values);        return anim;    }

From the source code, we can see that this method receives N parameters. The first is the object we want to set the animation, and the second parameter sets the animation for which attribute, the following two parameters indicate that the control is not transparent to the translucent layer. The following parameters can be transferred to N multiple parameters, for example

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, alpha, 1f,                    0.5f,1f,0f);

It indicates that the control is from transparent to translucent, to opaque, and finally to fully transparent. With this example, I want to achieve the effects of other tween animations.

Rotation:

ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, rotation,                    0f, 360f);            animator2.setDuration(5000);            animator2.start();

Translation:

float curTranslationX = tv.getTranslationX();            ObjectAnimator animator = ObjectAnimator.ofFloat(tv,                    translationX, curTranslationX, -1000f, curTranslationX);            animator.setDuration(3000);            animator.start();

What if you want to achieve the combined animation effect?

ObjectAnimator moveIn = ObjectAnimator.ofFloat(tv, translationX,                    -500f, 0f);            ObjectAnimator rotate = ObjectAnimator.ofFloat(tv, rotation, 0f,                    360f);            ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(tv, alpha, 1f,                    0f, 1f);            AnimatorSet animSet = new AnimatorSet();            animSet.play(rotate).with(fadeInOut).after(moveIn);            animSet.setDuration(5000);            animSet.start();

Haha, it's easy.
Note that:

// After (Animator anim) an existing animation is executed after the input animation // after (long delay) an existing animation is executed after a specified delay in milliseconds // before (Animator anim) the existing animation is executed before the input animation // with (Animator anim). The existing animation and the imported animation are executed simultaneously.

Of course, attribute animation is the same as tween animation, that is, it can be implemented using code or xml. Let's look at how to implement attribute animation through xml files:


      
       
       
           
            
            
                
                 
                 
                 
             
        
   
  

Android: the value of ordering is together, which indicates that the changes of each dimension occur simultaneously. The default value is together, and sequentially, which indicates that the animation occurs sequentially.

This is basically the case.

 

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.