Android Properties Animation Property Animation series one of the Valueanimator

Source: Internet
Author: User

Android Animation category

Many apps on the market use animated effects, which can improve the user's experience. So what are the mechanisms of the Android system animation?
1. Frames-per-frame animation (Frame-by-frame animation). Frames-by-frame animations work very simply by splitting a complete animation into a single picture and then making them coherent to play, similar to how the animation works.
2. The motion Tween (tweened animation) is a series of animations that can be performed on the view, including fade, zoom, pan, and rotate four.
3. Attribute Animation Property Animation can achieve a more animated effect by changing the properties of the object.

Property animations vs. Tween animations

1. The tweened animation changes the position and the actual effect of the view, and does not change its properties. For example, if a button is moved with a motion tween, clicking the button again will not respond to the Click event but the property animation can do this.
2. The Tweened animation object is limited to view, and the property animation object is not limited to view, but works on all objects. For example, a property animation can change the color value and the motion tween cannot do this.

Valueanimator

Valueanimator is an important class in property animation that uses a mechanism of time loops to calculate the animation transitions between values and values, and we only need to provide the initial and end values to Valueanimator, and tell it how long the animation needs to run, Then Valueanimator will automatically help us with the effect of smoothing the transition from the initial value to the end value. Let's look at its usage in the example. The use of Valueanimator is simple:

Private void startanimation() {Valueanimator animator = Valueanimator.ofint (0, -,0);//In order to see the changes in animation values, this adds an animated update listener event to print the current value of the animationAnimator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public void onanimationupdate(Valueanimator animation) {int value= (int) Animation.getanimatedvalue (); LOG.E ("TAG","The value is"+value);        }        }); Animator.setduration ( +);//Animation timeAnimator.start ();//Start animation}

Valueanimator ofint (int ... values), parameters can have one or more. However, it is generally more than two, if it is two parameters: the value changes from the first parameter over time to the second parameter. If it is more than three arguments, the values change over time. Look at the above print results as follows:

Valueanimator There are other methods, such as the need for a floating-point data animation changes can be used as follows:

ValueAnimator animator = ValueAnimator.ofFloat(0f,10.5f,5.0f,0f);        animator.setDuration(500);//动画时间        animator.start();//启动动画

Animations that change color values

ValueAnimator animator = ValueAnimator.ofArgb(0x00ffff,0x00ffee);        animator.setDuration(500);//动画时间        animator.start();//启动动画

Change the animation of the generic value valueanimator animator = Valueanimator.ofobject (). Here's what you'll get.

Valueanimator use

You may be surprised that the examples above do not see where it is used. Don't worry, let's give you an example to prove its usefulness. Draw a line dynamically on the phone screen.

 PackageCom.xjp.animations;ImportAndroid.animation.TypeEvaluator;ImportAndroid.animation.ValueAnimator;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.util.AttributeSet;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public  class mainactivity extends actionbaractivity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        ViewGroup parent = (viewgroup) Findviewbyid (r.id.parent); Parent.addview (NewSingleline ( This)); }Private  class singleline extends View {        PrivatePaint Mpaint;Private floatx =0;Private floaty = Max; Public Singleline(Context context) {Super(context); Mpaint =NewPaint (Paint.anti_alias_flag);        Mpaint.setcolor (color.red); } Public Singleline(context context, AttributeSet attrs) {Super(context, attrs); Mpaint =NewPaint (Paint.anti_alias_flag);        Mpaint.setcolor (color.red); } Public Singleline(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }@Override        protected void OnDraw(Canvas canvas) {if(0= = x) {startanimation (); } canvas.drawline (0, y, x, x + y, mpaint); }Private void startanimation() {Valueanimator animator = Valueanimator.ofobject (NewSinglelineevaluator (),0, -); Animator.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override                 Public void onanimationupdate(Valueanimator animation) {floati = (float) Animation.getanimatedvalue (); x = i;//Refresh the UI constantlyInvalidate ();            }            }); Animator.setduration ( -);        Animator.start (); }    }Private  class singlelineevaluator implements typeevaluator {        @Override         PublicObjectEvaluate(floatFraction, Object Startvalue, Object Endvalue) {returnFraction * ((number) endvalue). Floatvalue ()-((number) startvalue). Floatvalue ()); }    }}

We used valueanimator animator = Valueanimator.ofobject (Typeevaluator evaluator, Object ... values), the first parameter of the method is the animation over algorithm class, The arguments that follow are animated over values.
Here the main explanation typeevaluator type: Ofobject () method and Ofint (), offloat () different, need to implement typeevaluator themselves, and the other two methods within the system to help implement the method, respectively, is Intevaluator and Floatevaluator types.
Take a look at the Floatevaluator implementation as follows:

publicclass FloatEvaluator implements TypeEvaluator {    publicevaluate(float fraction, Object startValue, Object endValue) {        float startFloat = ((Number) startValue).floatValue();        return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);    }}

As you can see, Floatevaluator implements the Typeevaluator interface and then overrides the Evaluate () method. Evaluate () method passed three parameters, the first parameter fraction is very important, this parameter is used to indicate the completion of the animation (the value of fraction varies between 0-1), we should calculate the current animation value should be based on it, The second third parameter represents the initial and ending values of the animation, respectively. Then the logic of the above code is clearer, with the end value minus the initial value, calculate the difference between them, and then multiplied by the coefficient of fraction, plus the initial value, then the current animation value.

Well, let's take a look at the typeevaluator we've achieved.

privateclass SingleLineEvaluator implements TypeEvaluator {        @Override        publicevaluate(float fraction, Object startValue, Object endValue) {            return fraction * (((Number) endValue).floatValue() - ((Number) startValue).floatValue());        }    }

The interface method evaluate implements the algorithm of linear straight line. Y=n*x;

System Typeevaluator

There are several typeevaluator in the system, which can be used directly in the code.

Argbevaluator: This evaluator can be used to perform interpolation between types of integer values that represent ARGB colors.
Floatevaluator: This evaluator can be used to perform interpolation between floating-point values.
Intevaluator: This evaluator can be used to perform interpolation between type int values.
Rectevaluator: This evaluator can be used to perform interpolation between types of rectangular values.

Next section Learn Objectanimator

Android Properties Animation Property Animation series one of the Valueanimator

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.