Android Property Animation

Source: Internet
Author: User

Android Property Animation
Property Animation introduction:

Born in 3.0, a series of operations are performed using the attributes of the View. For example, the setAbc attribute of a View can be set theoretically.

It not only changes the painting of the View, but also the attributes of the View. Tween Animation only changes the painting of the View.

ValueAnimator is the base class of an animation. It has a subclass of ObjectAnimator. Interpolator and TypeEvaluator are required to calculate the attribute value.

Interpolator time plug-in, Animation speed, see the previous Tween Animation

The factory method of TypeEvaluator Animator can create objects with properties of any value type. TypeEvaluator is used to calculate the attribute value.

Some View attributes are added after 3.0:

1) translationX and translationY: These two attributes control the position of the View,
Their values are set by the layout container and are an offset relative to the coordinate origin (top left corner 0.

2) rotation, rotationX, and rotationY: controls the rotation of a View around the axis points (TX and Ty. It is not consistent with RotateAnimation in Tween Animation.

RotateAnimation rotation, expressed as plane rotation

The rotationX and Y rotate in a three-dimensional way. By default, It is a horizontal line of x y based on the center point of the View.
3) scaleX and scaleY: controls the scaling of View based on transport Tx and transport ty.
4) rotate Tx and rotate TY: rotate the vertices and zoom datum points. By default, they are the center points of the View.
5) x and y: Describes the final position of the view in its parent container, which is the sum of the left Mark and offset (translationX, translationY) in the upper left corner.
6) aplha: transparency. 1 is completely opaque, and 0 is completely transparent.

These attributes are similar to the Animation attribute values of Tween Animation.


ObjectAnimator object Animation

This animation can only represent one action attribute at a time.

ObjectAnimator xml implements xml definition Animation

Res/animator/scale_object_animator.xml

 
 
Code loading 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); // you can specify scaleAnimator as the animation target. setDuration (1000); scaleAnimator. setRepeatCount (50); scaleAnimator. start ();
Animation set

Composed of ObjectAnimator and ValueAnimator. The corresponding xml format is similar

Xml definition Animation

Res/animator/set_rotate_scale.xml

 
     
          
           
       
      
          
           
           
           
       
  
 
Code loading 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 (1000); animatorSet. setInterpolator (new BounceInterpolator (); // you can specify the anmatorset for the spring plug-in at the end. start ();
PropertyValuesHolder
// Use PropertyValuesHolder to construct an Animator and combine it into a set-like effect: 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

Obtain ViewPropertyAnimator through view. animate ()

Imageview. setBackground (getResources (). getDrawable (R. drawable. a11); ViewPropertyAnimator animate = imageview. animate (); // This object does not have the setRepeat method. // you can set the animation attributes to combine them into an animation similar to the set effect. alpha (0); animate. rotationX (50); animate. translationXBy (500); animate. scaleX (1.5f); animate. scaleY (1.5f); animate. setInterpolator (new BounceInterpolator (); animate. setDuration (2000); animate. start ();

ValueAnimator

The ValueAnimator code and xml settings do not contain setPropertyName. Because it is not an operation object, it only performs an action based on the value.
You need to add a listener to handle changes to the listener value.

Xml definition Animation

 
Code loading animation xml
ValueAnimator valueAnimator = (ValueAnimator) iterator. loadAnimator (this, R. animator. animator); valueAnimator. setTarget (TV _num); valueAnimator. setEvaluator (new TypeEvaluator
 
  
() {@ Override public Integer evaluate (float fraction, Integer startValue, Integer endValue) {System. out. println ("percentage, 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) {// return the evaluate value of the current frame of the first animation in onAnimationUpdate. out. println ("animation. getAnimatedValue () = "+ animation. getAnimatedValue (); TV _num.setText (animation. getAnimatedValue () + "") ;}}); // valueAnimator. setInterpolator (new LinearInterpolator (); valueAnimator. start ();
 

Animator listener

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 () {// empty implement the 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, this 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 ();


Use attribute animation for background color

For details, see bouncingbils. java under ApiDemo.

Private static final int RED = 0xffFF8080; private static final int BLUE = 0xff80ff; private static final int CYAN = 0xff80ffff; private static final int GREEN = 0xff80ff80; {// animation color change 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 (3000); colorAnim. 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.