Android animation details 4. animation creation

Source: Internet
Author: User

Android animation details 4. animation creation
· Use ValueAnimator for animation

By specifying a set of int, float, or color values, ValueAnimator allows you to animation these types of values. You need to call a factory method of ValueAnimator to obtain a ValueAnimator object, such as ofInt (), ofFloat (), or ofObject (). For example:

 

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);animation.setDuration(1000);animation.start();

 

In this Code segment, when the start () method is executed, the animation value between 0 and 1 is calculated in ms.

You can also specify a custom type for animation as follows:

 

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);animation.setDuration(1000);animation.start();

In this Code segment, ValueAnimator starts to calculate the animation value between startPropertyValue and endPropertyValue in MS when the start () method is executed. The calculation logic provided by MyTypeEvaluator is used.

However, in the code snippet above, there is actually no impact on the object, because ValueAnimator does not directly operate on the object or attribute. You should modify the object based on the calculated value. You can define a listener to process important events of ValueAnimator during the animation process, such as updating frames. When the listener is implemented, you can call getAnimatedValue () to obtain the animation value during frame refresh.

 

· Use ObjectAnimator for animation

 

ObjectAnimator is a subclass of ValueAnimator and incorporates the computing power of the time engine and ValueAnimator to animation the attributes of an object. This attribute is specified by name. This makes it easier to animation an object, because you no longer need to implement ValueAnimator. AnimatorUpdateListener, because the attributes to be animated are automatically updated.

Instantiating ObjectAnimator is similar to instantiating ValueAnimator, but you also need to specify the attribute names of objects and objects:

 

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);anim.setDuration(1000);anim.start();

To enable ObjectAnimator to update attributes correctly, follow these steps:

· The property of the object to be animated must have a setter function, like this: set (). Because ObjectAnimator automatically updates attributes during animation, you must use this setter method to operate the target attributes. For example, if the attribute name is foo, you must have a setFoo () method. If this method does not exist, you have three options:

· If you can, add the setter method by yourself ).

· Use another class to encapsulate the class of this object, so you have the power to rewrite it. Add the setter that operates the attributes of the original class to the encapsulation class.

· Use ValueAnimator instead.

· If you specify only one values... parameter in a factory method of ObjectAnimator, this parameter is regarded as the animation end value. Therefore, the object property of your animation must have a getter method for obtaining the start value. The getter method must have the get () Format. For example, if the attribute name is foo, You need to carry a getFoo () method.

· The data types processed by the getter (if needed) and setter methods of the attributes to be animated must be the same as the start and end values specified for ObjectAnimator. For example, if you create the following ObjectAnimator, you must have the targetObject. setPropName (float) method and targetObject. getPropName (float) method:

ObjectAnimator. ofFloat (targetObject, "propName", 1f)

· Unlike the animation objects and attributes, you may need to call the invalidate () method of View to force the screen to repaint itself with new animation data. You should do this in the onAnimationUpdate () callback. For example, if the color attribute of a Drawable object is animated, the screen is updated only when the object is re-painted. All attributes of the View, such as setAlpha () and setTranslationX (), will invalidate the View. Therefore, you do not need to invalidate the View when calling these methods.

· Use AnimatorSet to combine multiple animations

 

In many cases, you may want to play an animation. Its playback time depends on the start time or end time of other animations. The Android system allows you to bind multiple animations to an animation set. Therefore, you can specify whether to play simultaneously or separately or after a delay. You can also nest animation set objects.

The following sample code is taken from the bouncingbils example (slightly changed to the lower part ). It plays the following animation objects in this way:

1. Play bounceAnim.

2. Play squashAnim1, squashAnim2, stretchAnim1, and stretchanim2.

3. Play bounceBackAnim.

4. Play fadeAnim.

AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();
 

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.