Android Note (65) animation in Android--attribute animation (propertyanimation)

Source: Internet
Author: User

Motion tweens can only define the start and end of two frames in the "transparency", "rotation", "tilt", "displacement" 4 aspects of the change, frame-wise animation can only play multiple pictures, not to meet our daily complex animation needs, so Google started in 3.0, launched a property animation Animation

Property animations are no longer designed for view, and are not only limited to moving, zooming, fading, but also not a visual animation. It is actually a constant mechanism for manipulating values and assigning values to the specified properties of a specified object, which can be any property of any object.

Valueanimator

Valueanimator is the most important class in the entire property animation mechanism, responsible for calculating the animation between the starting value and the ending value, we just need to give the starting value and the end value and run time to Valueanimator, Valueanimator automatically calculates the excess value from the start value to the ending value. In addition Valueanimator is also responsible for managing the playback of the animation at this time, play mode, and set up listeners, etc.

For example, we need to animate the value from 0 to 5 and over 0 in 2 seconds, so we can write:

 PackageCn.lixyz.animator;ImportAndroid.animation.ValueAnimator;ImportAndroid.animation.ValueAnimator.AnimatorUpdateListener;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateImageView IV; PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {IV=(ImageView) Findviewbyid (r.id.image); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt:valueanimator va=valueanimator.offloat (0f, 5f, 0f); Va.setduration (1000); Va.addupdatelistener (NewAnimatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {floati = (float) Animation.getanimatedvalue (); LOG.D ("Tttt", "Run Time:" +i);            }            });            Va.start ();  Break; }    }}

In the above code through the Addupdatelistener () method to add an animation listener, in the process of animation will continue to callback the Onanimationupdate method, in this method to print the current value, you will find that the system automatically calculates the value of the intermediate over process

Objectanimator

Valueanimator is a calculation of excessive values, and objectanimator can animate arbitrary object properties directly

For example, we want to change a picture from transparent to normal in 3 seconds to transparent.

 PackageCn.lixyz.animator;ImportAndroid.animation.ObjectAnimator;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateImageView IV; PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {IV=(ImageView) Findviewbyid (r.id.image); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt:objectanimator OA= Objectanimator.offloat (iv, "Alpha", 0f, 1f, 0f); Oa.setduration (3000);            Oa.start ();  Break; }    }}

offloat parameter Description:

Target: Passing in the object we want to manipulate

PropertyName: Passing in object properties to be manipulated

Values: The animation value you want to implement (not fixed length)

  

commonly used animation properties:

rotation--rotation

Translationx--x Axis Direction displacement

Translationy--y Axis Direction displacement

Scalex--x Axis Direction Scaling

Scaley--y Axis Direction Scaling

Animatorset

By name you can see that this class is used to implement animation collections, and this class can help us to combine multiple animations together for playback.

Animatorset provides a play () method if we pass a animator object (Valueanimator or Objectanimator) to this method An instance of Animatorset.builder will be returned, and the following four methods are included in the Animatorset.builder:

After (Animator anim) inserts an existing animation into the incoming animation after it executes

After (long delay) The existing animation is delayed by a specified millisecond after execution

Before (Animator Anim) executes an existing animation before inserting it into an incoming animation

With (Animator Anim) performs both existing and incoming animations

For example, we want to make the picture rotate while moving.

 PackageCn.lixyz.animator;ImportAndroid.animation.AnimatorSet;ImportAndroid.animation.ObjectAnimator;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateImageView IV; PrivateButton BT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {IV=(ImageView) Findviewbyid (r.id.image); BT=(Button) Findviewbyid (R.ID.BT); Bt.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.bt:objectanimator Traslationanimator= Objectanimator.offloat (iv, "Translationx",-600f, 0f); Objectanimator Rotationanimator= Objectanimator.offloat (iv, "rotation", 0f, 360f); Animatorset as=NewAnimatorset ();            As.play (Rotationanimator). with (Traslationanimator); As.setduration (3000);            As.start ();  Break; }    }}

Android Note (65) animation in Android--attribute animation (propertyanimation)

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.