Property animation and android property Animation

Source: Internet
Author: User

Property animation and android property Animation
Property Animation

Property Animation: One of the many animations provided by Android, from a certain perspective, attribute animation is actually a Tween animation of the enhanced version.

Attribute animation mainly consists of two aspects:

1. Calculate the attribute values of each frame.
2. Set relevant attribute values for the specified object.

Differences:

1. The Tween animation can only manipulate the transparency (alpha), position (translate) of various components ),

The rotate and scale attributes are converted accordingly, but the Property animation can change any attribute value.

2. The Tween animation can only function on the UI component, but there is no restriction on the Property animation. You can operate on any object.

Property animation API:

1. ValueAnimator: The main time engine of attribute animation. It is used to calculate the attribute values of each frame, that is, the first part of the attribute animation. As ValueAnimator is only responsible for the first part, the second part assigns the attribute value to the component and needs to be done manually.

2. ObjectAnimator: a subclass of ValueAnimator. You can directly specify the component object when using it, making it easier to use. However, in some scenarios, ObjectAnimator has some restrictions. You may need to consider using ValueAnimator.

3. AnimatorSet: a subclass of Animator. It can be used to combine multiple Animator objects and specify whether to play in sequence or simultaneously.

In ValueAnimator, a computing tool class Evaluator (calculator) is also used to control how property animation calculates property values. Android provides centralized Evaluator:

1. IntEvaluator: used to calculate the int type attribute value calculator.

2. FloatEvaluator: used to calculate the float type attribute value calculator.

3. ArgbEvaluator: used to calculate the hexadecimal rgb color value.

4. TypeEvaluator: calculator interface for custom calculator implementation.

To create an animation using ValueAnimator, follow these four steps::

1. call the ofInt (), ofFloat (), and ofObject () Static Methods of ValueAnimator to create a ValueAnimator instance. Obviously, the instances created by these methods are different, the calculators used are also different. 2. Call the setDuration () method of ValueAnimator to set relevant parameters for the property animation, such as the animation duration and number of repetitions. 3. Call the start () method to start the corresponding animation. 4. register the AnimatorUpdateListener listener for ValueAnimator. In the listener, you can obtain the value of the value calculated by ValueAnimator in real time. You only need to assign a value to the corresponding object in the listener. For example:
ValueAnimator animation = ValueAnimator. ofFloat (1f, 0f); animation. setDuration (3000); animation. setRepeatCount (1); animation. start (); animator. addUpdateListener (new ValueAnimator. animatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {/*** you can use getAnimatedValue () to obtain the attribute values of the currently calculated frames, and assign the value to the specified object. */}});
If you want to use the property value calculator defined by TypeEvaluator, change the first step:
    ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(),1f, 0f);

2. Using ObjectAnimator to create an animation is similar to ValueAnimator, but ObjectAnimator has already assigned the calculation result to an object. We provide a control object when using it,

You do not need to register the AnimatorUpdateListener listener (no step 4 above ).

For example:
    ObjectAnimator animation = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);    animation.setDuration(1000);    animation.setRepeatCount(1);    animation.start();
Note: 1. There must be a changed attribute value on the ObjectAnimator object. For example, in the preceding example, imageView must have the alpha attribute value. 2. If the first sentence is
    ObjectAnimator animation = ObjectAnimator.ofFloat(imageView, "alpha", 1f);
If one parameter is provided, the provided value is considered as the end value of attribute calculation. The getter method must be provided for the alpha attribute. The returned value is used as the initial value of attribute calculation, that is, the animation starts from the current state of the component. 3. if the animation object is a View, In order to display the animation effect, you need to constantly notify the component to re-draw and call the View. the invalidate () method, but the setter method defined by View, such as setAloha (), will automatically call the invalidate () method, no additional calls are required.
/*** Graphics class * is a circle in the instance */public class ShapeHolder {private float x = 0, y = 0; private ShapeDrawable shape; private int color; private RadialGradient gradient; private float alpha = 1f; private Paint paint; public ShapeHolder (ShapeDrawable s) {shape = s;} public float getWidth () {return shape. getShape (). getWidth ();} public void setWidth (float width) {Shape s = shape. getShape (); s. resize (width, s. getWidth ();} public float getHeight () {return shape. getShape (). getHeight ();} public void setHeight (float height) {Shape s = shape. getShape (); s. resize (height, s. getHeight ();} public float getX () {return x;} public void setX (float x) {this. x = x;} public float getY () {return y;} public void setY (float y) {this. y = y;} public int getColor () {return color;} public void setColor (int color) {this. color = color;} public ShapeDrawable getShape () {return shape;} public void setShape (ShapeDrawable shape) {this. shape = shape;} public RadialGradient getGradient () {return gradient;} public void setGradient (RadialGradient gradient) {this. gradient = gradient;} public float getAlpha () {return alpha;} public void setAlpha (float alpha) {this. alpha = alpha;} public Paint getPaint () {return paint;} public void setPaint (Paint paint) {this. paint = paint ;}} public class MainActivity extends Activity {static final float BALL_SIZE = 50F;/*** used to control the animation execution time */static final float FULL_TIME = 1000; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); LinearLayout container = (LinearLayout) findViewById (R. id. root);/*** Add the custom control to the layout */container. addView (new MyAniamtionView (this);}/*** custom View * implements the AnimatorUpdateListener listener class */public class MyAniamtionView extends View implements ValueAnimator. animatorUpdateListener {/*** container for storing Multiple balls */public final ArrayList <ShapeHolder> bils = new ArrayList <ShapeHolder> (); public MyAniamtionView (Context context) {super (context); setBackgroundColor (Color. WHITE) ;}@ Override public boolean onTouchEvent (MotionEvent event) {// Add a small ball ShapeHolder newBall = addBall (event. getX (), event. getY (); float startY = newBall. getY (); float endY = getHeight ()-BALL_SIZE; float h = (float) getHeight (); float eventY = event. getY (); int duration = (int) (FULL_TIME * (h-eventY)/h); // use ValueAnimator to define the animation ValueAnimator fallAnim = ObjectAnimator. ofFloat (newBall, "y", startY, endY); fallAnim. setDuration (duration); fallAnim. setInterpolator (new AccelerateInterpolator (); // use ObjectAnimator to define the animation ObjectAnimator bounceBackAnim = ObjectAnimator. ofFloat (newBall, "y", endY, startY); bounceBackAnim. setDuration (duration); bounceBackAnim. setInterpolator (new DecelerateInterpolator (); fallAnim. addUpdateListener (this); // specifies the animation ObjectAnimator fadeAnim = ObjectAnimator for the transparency of the ball. ofFloat (newBall, "alpha", 1f, 0f); fadeAnim. setDuration (1, 250); fadeAnim. addListener (new AnimatorListenerAdapter () {@ Override public void onAnimationEnd (Animator animation) {bcalls. remove (ObjectAnimator) animation ). getTarget () ;}}); fadeAnim. addUpdateListener (this); // defines an AnimatorSet to sort the three defined animations. AnimatorSet animatorSet = new AnimatorSet (); animatorSet. play (fallAnim ). before (bounceBackAnim); animatorSet. play (bounceBackAnim ). before (fadeAnim); animatorSet. start (); return true ;} /*** Add the ball to the ArrayList of the ball set * set various attribute coordinates ** @ param x * @ param y * @ return */private ShapeHolder addBall (float x, float y) {OvalShape circle = new OvalShape (); circle. resize (BALL_SIZE, BALL_SIZE); ShapeDrawable drawable = new ShapeDrawable (circle); ShapeHolder shapeHolder = new ShapeHolder (drawable); shapeHolder. setX (x-BALL_SIZE/2); shapeHolder. setY (y-BALL_SIZE/2); int red = (int) (Math. random () * 255); int green = (int) (Math. random () * 255); int blue = (int) (Math. random () * 255); int color = 0xff000000 + red <16 | green <8 | blue; Paint paint = drawable. getPaint (); int drakColor = 0xff000000 | red/4 <16 | green/4 <8 | blue/4; RadialGradient gradient = new RadialGradient (37.5f, 12.5f, BALL_SIZE, color, drakColor, Shader. tileMode. CLAMP); paint. setShader (gradient); shapeHolder. setPaint (paint); bils. add (shapeHolder); return shapeHolder;}/*** draw each ball in the set on the screen * @ param canvas */@ Override protected void onDraw (Canvas canvas) {for (ShapeHolder shapeHolder: bils) {canvas. save (); canvas. translate (shapeHolder. getX (), shapeHolder. getY (); shapeHolder. getShape (). draw (canvas); canvas. restore () ;}@ Override public void onAnimationUpdate (ValueAnimator animation) {this. invalidate ();}}}

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.