Property Animation Part II (Attribute animation second section)

Source: Internet
Author: User

How to use Valueanimator

The Valueanimator class is defined by specifying a set of integers, floating-point numbers, or color values, and the duration of the animation. Getting an instance of an Valueanimator object requires calling its factory method: Ofint (), offloat (), or ofobject (), as follows:

ValueAnimator animation = ValueAnimator.ofFloat(01f);animation.setDuration(1000);animation.start();

In this code, when the start () method starts running, Valueanimator begins to calculate the value of the animation between 0f and 1f, and the duration of the animation is 1000 milliseconds.
You can also specify a custom type of animation as follows:

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

In this code, when the start () method starts running, Valueanimator begins to calculate values for Startpropertyvalue and endpropertyvalue based on Mytypeevaluator, and the duration of the animation is 1000 milliseconds.

The above code fragment, for an object, has no practical effect, because the Valueanimator class does not directly affect objects or attributes. All you need to do is change some of the properties of the object based on the calculated value. You can handle logic by listening to Valueanimator interfaces in the animation life cycle, such as frame updates. When implementing an interface, you can call Getanimatedvalue () to get a calculated value for a particular frame.

How to use Objectanimator

Objectanimator is a subclass of Valueanimator, which changes the properties of a pre-specified object directly based on the calculated value. Since Objectanimator has automatically implemented Valueanimator.animatorupdatelistener, it is no longer necessary for you to implement it, which makes adding animations to an object much easier.

Instantiate a objectanimator similar to the above, but you need to specify the name of the object and the property of the object (a string), the value of the property changes during the animation, a simple example is as follows:

ObjectAnimator anim = ObjectAnimator.ofFloat"alpha"01f);anim.setDuration(1000);anim.start();

In order for Objectanimator to update the properties correctly, you must do the following

    • As the animation changes, the object property must have a setter method (camel naming convention). Because the Objectanimator animation automatically updates the property, it must be able to access the property through the setter method. For example, if the property name is Foo, you need to have a Setfoo () method. If this setter method does not exist, you have three options:

      • To add a setter method to the corresponding class

      • Using a wrapper class, the wrapper class defines a setter method that can pass the value to the property you want to change

      • Use the Valueanimator class to implement

    • If you specify only one value at the time of initialization, the value is considered to be the ending value of the animation. Therefore, you must have a getter function in your animated object property to get the starting value of the animation. For example, if the property name is Foo, you need to have a Getfoo () method.

    • In Objectanimator, the getter method of the property (if necessary) and the setter method must be the same as the data type of the start and end values that you specify. In the following example, your targetObject must have Targetobject.setpropname (float) and targetobject.getpropname (float) methods

ObjectAnimator.ofFloat(targetObject"propName"1f);
    • When you change some of the properties of certain objects, you may need to call the view's invalidate () method notification screen to redraw using the updated animated values. This code should be called in the callback function of Onanimationupdate (). For example, if you change the Color property, only the screen redraw method is invoked to animate the effect. All setter methods that already exist in the view class, such as Setalpha () and Settranslationx (), refresh the view inside the method, so you do not need to recall the invalidate () method when you try to change these properties
How to manage multiple animations using Animatorset

In many cases, you want to perform an animation at the beginning or end of another animation, and the Android system supports putting multiple animations into one animatorset at the same time, allowing you to specify that the animations are executed at the same time, sequentially, or after a certain delay. You can also put Animatorset in the Animatorset.

The following code example is from the Bouncing Balls example (it has been changed a little), and Animatorset includes the following animations:

    1. Executive Bounceanim
    2. Simultaneous execution of SQUASHANIM1, squashAnim2, STRETCHANIM1, and STRETCHANIM2
    3. Executive Bouncebackanim
    4. Executive 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",1F0F;Fadeanim. Setduration( -);Animatorset animatorset = new Animatorset ();Animatorset. Play(bouncer). Before(Fadeanim);Animatorset. Start();
How to implement an animated event listener

During the execution of the animation, you can listen for an animated duration with the following event

    • Animator.animatorlistener

      • Onanimationstart ()-Called when the animation starts executing
      • Onanimationend ()-Called when the animation ends
      • Onanimationrepeat ()-Called when the animation repeats
      • Onanimationcancel ()-Called when the animation is canceled, regardless of how the animation ends, and the Onanimationend () method is also called
    • Valueanimator.animatorupdatelistener

      • Onanimationupdate ()-Every frame of the animation is called. Listen to this interface and use the computed value (the Getanimatedvalue () method) to modify the properties of the object that you want to change in the current animation.

      • As with Objectanimator, when you change certain properties of certain objects, you may need to call the view's invalidate () method to tell the screen to redraw with the updated animated values. This code should be called in the callback function of Onanimationupdate (). When you change the Color property, only the screen redraw method is invoked to animate the effect. All setter methods that already exist in the view class, such as Setalpha () and Settranslationx (), refresh the view inside the method, so you do not need to recall the invalidate () method when you try to change these properties

If you do not want to implement all of the Animator.animatorlistener interface methods. Then you can choose to override the empty implementation provided by the Animatorlisteneradapter class.

The following is an example of a bouncing Balls project, in order to invoke the Onanimationend () callback, a new animatorlisteneradapter is created, and the code is as follows

"alpha"10f);fadeAnim.setDuration(250);fadeAnim.addListener(new AnimatorListenerAdapter() {publicvoidonAnimationEnd(Animator animation) {    balls.remove(((ObjectAnimator)animation).getTarget());}
How to realize the animation change of viewgroups

Animation support for property animations for internal member changes in ViewGroup is as good as animation support for view

When layout changes within a viewgroup, you can use the Layouttransition class to animate IT support. When you call the Setvisibility () method on a view inside a viewgroup, the view itself can produce a corresponding animation effect that disappears or appears. When you add or remove a view, there is also a corresponding displacement animation generated. You can specify one of the following animation constants for an object by calling Layouttransition's Setanimator () method:

    • Appearing-animation when a member in a container appears.
    • Change_appearing-animation that occurs when a container is changed due to the addition of a member.
    • Disappearing-animation When a member in a container disappears.
    • Change_disappearing-animation that occurs when a container changes due to the disappearance of a member.

You can define these four types of constants corresponding to the animation effect, or directly use the system default effect

The following layoutanimations example shows how to set a system default animation effect on a layout file, which is very simple to use, just set the Android:animatelayoutchanges property to True, as shown below

<LinearLayout    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:id="@+id/verticalContainer"    android:animateLayoutChanges="true" />

Set this property to True and all controls inside it will have an animated display when views are added or removed.

Property Animation Part II (Attribute animation second section)

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.