Property Animation)

Source: Internet
Author: User

Property Animation)

 

As mentioned in the previous article, property animation has been introduced in Android 3.0 and later versions. Property animation can easily do a lot of things that cannot be done by View animation. Today we will learn about property animation.

As mentioned above, the View animation only changes the rendering effect of the View, while the attribute animation actually changes an attribute. The effect is as follows.

Compared with the detailed explanation of Android animation, we can see the obvious difference, so how to use the property animation? Don't panic, then the code will be available.

 

1, ObjectAnimator

Attribute animation is not complicated to use. It can be said that it is much simpler than View animation.

 

// MButton: Object // TranslationY: Property // 300 offset ObjectAnimator. ofFloat (mButton, TranslationY, 300). setDuration (2000). start ();

In one sentence, the translation animation is implemented, but the attribute animation can do more than that. Let's Modify an attribute to see it.

 

 

// MButton: Object // TranslationY: Property // 300 offset ObjectAnimator. ofFloat (mButton, RotationY, 360). setDuration (2000). start ();

 

Many people may wonder what the attribute name is. In fact, when using ObjectAnimator, you must specify an object and an object attribute, that is, you must have the get & set function, if not, you cannot use ObjectAnimator, but you can use another ValueAnimator.

 

2, ValueAnimator

Let's take a look at the example. How can we use ValueAnimator to complete the above effect.

 

 ValueAnimator valueAnimator = ValueAnimator.ofFloat(360); valueAnimator.setDuration(2000);
Familiar with it .. However, this method cannot be used because no objects and attributes are specified. To achieve the animation effect, you must implement the ValueAnimator. onUpdateListener interface.

 

 

ValueAnimator valueAnimator = ValueAnimator.ofFloat(360);valueAnimator.setDuration(2000);valueAnimator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mButton.setRotationY((Float) animation.getAnimatedValue());}});
You can use animation. getAnimatedValue () to obtain the current attribute value calculated based on the time and offset. After obtaining the current attribute value, we naturally want to do what we want. By referring to ObjectAnimator, we inherit ValueAnimator.

 

 

3, AnimatorSet

Of course, our property animation has the set function, and its usage is very interesting.

 

AnimatorSet animatorSet = new AnimatorSet();ValueAnimator valueAnimator = ValueAnimator.ofFloat(360);valueAnimator.setDuration(2000);valueAnimator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mImageView.setRotationY((Float) animation.getAnimatedValue());}});animatorSet.play(valueAnimator).with(ObjectAnimator.ofFloat(v, RotationY, 360).setDuration(2000));animatorSet.start();
AnimatorSet. play is used to add an animation. with is playing at the same time. before. after, I believe everyone knows what it is, but it can only play animations of different objects at the same time. What should we do if we want the same object to change its attributes at the same time.

 

 

4, ViewPropertyAnimator

Call RotationX and RotationY at the same time, haha is not very pleasant.

 

PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat(RotationX, 360f);PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat(RotationY, 360f);ObjectAnimator.ofPropertyValuesHolder(v, p1, p2).setDuration(2000).start();

 

There are still interesting things about property animation. We will stay in the next issue!

 

 

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.