Android Property Animation

Source: Internet
Author: User

Property Animation Introduction:

Born in 3.0, it takes advantage of the properties owned by the view to perform a series of operations. For example, if a view has a SETABC attribute, it can be set in theory.

It not only changes the view's drawing, but also changes the view's properties, while tween Animation only changes the view's drawing.

Valueanimator is the base class for animations, and it has a subclass of Objectanimator. Interpolator and Typeevaluator are required to calculate the property values.

Interpolator time Insert, motion rate of animation, see previous article Tween Animation

Typeevaluator Animator's factory method of the method can create an object that has a property of any value type. Typeevaluator is used to calculate the value of a property.

Some of the properties of the view have been added since 3.0:

1) Translationx and Translationy: These two properties control where the view is located, their values are set by the layout container, and are an offset from the origin of the coordinates (the upper-left corner of the 0,0).

2) rotation, RotationX and RotationY: Controls the rotation of the view around the pivot point (Pivotx and Pivoty). Its performance is inconsistent with the rotateanimation in tween animation.

The rotation of a rotateanimation , expressed as a plane of rotation

and RotationX, Y rotation, is a three-dimensional rotation, the default is to view the center point, do x Y horizontal line, to flip the horizontal line

3) ScaleX and ScaleY: Controls the zoom of the view based on Pivotx and Pivoty.

4) Pivotx and Pivoty: The pivot point of the rotation and the base point of the Zoom, which by default is the center point of the view.

5) x and Y: Describes the final position of view in its parent container, the Zoope of the upper-left corner and the offset (translationx,translationy).

6) Aplha: Transparency, 1 is completely opaque, 0 is completely transparent.

These properties are similar to the animated property values of tween animation

Objectanimator Object Animations

The animation can only represent one action property at a time.

XML implementation of ObjectanimatorXML definition Animations

Res/animator/scale_object_animator.xml

<?xml version= "1.0" encoding= "Utf-8"? ><objectanimator xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:duration=" "    android:propertyname=" ScaleX "    android:repeatcount=" 1 "    Android:repeatmode= "Reverse"    android:valuefrom= "1.0"    android:valueto= "2.0" ></objectanimator >
Code load Animation XML

Imageview_scale.setbackground (Getresources (). getdrawable (R.DRAWABLE.A11)); Objectanimator ScaleAnimator = ( Objectanimator) Animatorinflater.loadanimator (this, r.animator.scale_object_animator); ScaleAnimator.setTarget ( Imageview_scale);//Animate target object Scaleanimator.setduration (+); Scaleanimator.setrepeatcount (50); Scaleanimator.start ();
Animatorset Moving Album

Composed of Objectanimator and Valueanimator, the corresponding XML is written in the same notation as <se> <objectanimator/> <animator/> </set>

XML definition Animations

res/animator/Set_rotate_scale.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <set> <objectanimator android:propertyname= "RotationX" android:repeatcount= "        "Android:repeatmode=" reverse "android:valuefrom=" 0 "android:valueto="/> <objectanimator android:propertyname= "RotationY" android:repeatcount= "Android:repe"  Atmode= "reverse" android:valuefrom= "0" android:valueto= "android:valuetype=" Floattype " /> </set> <set> <objectanimator android:propertyname= "ScaleX" Androi d:repeatcount= "android:repeatmode=" reverse "android:valuefrom=" 1.0 "android:valueto=" 2.0 "> </objectAnimator> <objectanimator android:propertyname=" ScaleY "and Roid:repeatcount= "an"Droid:repeatmode= "Reverse" android:valuefrom= "1.0" android:valueto= "2.0" > </objectanim Ator> </set></set>
Code load Animation XML
Imageview_rotate.setbackground (Getresources (). getdrawable (R.DRAWABLE.A11)); Animatorset Animatorset = (animatorset) animatorinflater.loadanimator (this, r.animator.set_rotate_scale); Animatorset.settarget (imageview_rotate); animatorset.setduration (+); Animatorset.setinterpolator (new Bounceinterpolator ());//The Bounce Plug Animatorset.start () when setting end;
Propertyvaluesholder
Use the Propertyvaluesholder construct Animator   Group to synthesize effects like set propertyvaluesholder PVHX = Propertyvaluesholder.offloat (" ScaleX ", 0f,2.5f); Propertyvaluesholder pvhy = propertyvaluesholder.offloat ("ScaleY", 0f,3f);     Objectanimator animator = Objectanimator.ofpropertyvaluesholder (ImageView, pvhx,pvhy); Animator.setduration (2000); Animator.start ();
Viewpropertyanimator

Get Viewpropertyanimator with View.animate ()

Imageview.setbackground (Getresources (). getdrawable (R.DRAWABLE.A11)); Viewpropertyanimator animate = Imageview.animate ();//The object does not have a Setrepeat method//By some animated properties to set the effect of combining similar set Animate.alpha (0); Animate.rotationx (Animate.translationxby); Animate.scalex (1.5f); Animate.scaley (1.5f); Animate.setinterpolator (New Bounceinterpolator ()); animate.setduration (+); Animate.start ();

Valueanimator

There is no setpropertyname in Valueanimator code and XML settings because it is not an action object, just some action based on value
Need to add listener, listen to change the value of the corresponding processing

XML definition Animations

<?xml version= "1.0" encoding= "Utf-8"? ><animator xmlns:android= "http://schemas.android.com/apk/res/ Android "     android:interpolator=" @android: Anim/accelerate_interpolator "    android:duration=" 10000 "    android:startoffset= "    android:repeatcount=" Infinite "    android:repeatmode=" restart "    android: valuefrom= "1"    android:valueto= "android:valuetype=" "    inttype" ></animator>
Code Load Animation XML
Valueanimator valueanimator = (valueanimator) animatorinflater.loadanimator (this, r.animator.animator); Valueanimator.settarget (Tv_num); Valueanimator.setevaluator (new typeevaluator<integer> () {@Override public Integer evaluate (float fraction, integer startvalue, integer endvalue) {System.out.println ("percent, fraction:" + fraction);    SYSTEM.OUT.PRINTLN ("Result value:" + (int) ((Startvalue + fraction * (endvalue-startvalue))/10 * 10)); return (int) ((Startvalue + fraction * (endvalue-startvalue))/10 * 10);}}); Valueanimator.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (valueanimator Animation) {//in Onanimationupdate This value returns the evaluate value of the current frame of the first animation System.out.println ("animation.getanimatedvalue () = =" + Animation.getanimatedvalue ()); Tv_num.settext (Animation.getanimatedvalue () + "");}); /valueanimator.setinterpolator (New Linearinterpolator ()); Valueanimator.start ();

Animator's ListenerAnimatorlistener

New Animatorlistener () {@Overridepublic void Onanimationstart (Animator animation) {} @Overridepublic void Onanimationrepeat (Animator animation) {} @Overridepublic void Onanimationend (Animator animation) {} @Overridepublic void Onanimationcancel (Animator animation) {}}

Animatorlisteneradapter
New Animatorlisteneradapter () {//null implements Animatorlistener@overridepublic void Onanimationcancel (Animator animation) { Super.onanimationcancel (animation);} @Overridepublic void Onanimationend (Animator animation) {super.onanimationend (animation);} @Overridepublic void Onanimationrepeat (Animator animation) {super.onanimationrepeat (animation);} @Overridepublic void Onanimationstart (Animator animation) {Super.onanimationstart (animation);} @Overridepublic void Onanimationpause (Animator animation) {Super.onanimationpause (animation);} @Overridepublic void Onanimationresume (Animator animation) {Super.onanimationresume (animation);}}
Animatorupdatelistener

New Animatorupdatelistener () {@Overridepublic void onanimationupdate (Valueanimator animation) {//in Onanimationupdate The value returns the evaluate value of the current frame of the first animation System.out.println ("animation.getanimatedvalue () = =" + Animation.getanimatedvalue ());}}

Some operation functions: Animator.pause (); Animator.resume ();Animator.reverse ();Animator.end ();Animator.cancel ();

Animator.start (); animator.isstarted (); animator.ispaused (); animator.isrunning ();


Animating a background color with a property animation

See Apidemo to the Bouncingballs.java

private static final int RED = 0xffff8080;private static final int BLUE = 0xff8080ff;private static final int CYAN = 0XFF8 0ffff;private static final int GREEN = 0XFF80FF80; {//Animate color  Objectanimator Coloranim = Objectanimator.ofint (This, "BackgroundColor", CYAN, BLUE, RED); Coloranim.settarget (Ll_ animation); Coloranim.setevaluator (new Argbevaluator ()); Coloranim.setrepeatcount (Valueanimator.infinite); Coloranim.setrepeatmode (valueanimator.reverse); coloranim.setduration; Coloranim.start ();}



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.