The previous two blogs, which introduced a objectanimator function with a certain property of a control. But our Valueanimator it does not function with any one property itself, nor does it provide any kind of animation itself. It's simply a numerical generator that produces the various values you want. In fact, in the Android property animation, how to produce each step of the specific implementation of the animation effect, are calculated by Valueanimator.
For example, we are now going to implement a displacement animation from 0~100, Valueanimator will produce a 0~1 time factor based on the total duration of the animation, with such a time factor. Depending on your startvalue and Endvalue, the corresponding values can be generated by changing the corresponding changes. At the same time, through the use of the interpolator, we can further control each time factor it produces a change in the rate of value. If we use a linear interpolator, then it generates a linear change when the value is generated, and the increment is the same as long as the time is the same. If we use an accelerometer with an acceleration, then its increment will show a two-time curve. The growth rate will grow faster. Since our valueanimator does not respond to any one animation, nor does it control any one of the properties, it is not objectanimator used so widely. Let's take a look at how to use Valueanimator in your program.
Here is a timer animated Demo,activity_main.xml:
1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6tools:context= "Com.example.administrator.valueanimatordemo.MainActivity" >7 8<Button9Android:id= "@+id/btn_click"TenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content" AAndroid:layout_centerinparent= "true" -android:text= "click Me"/> - the</RelativeLayout>
Then is the implementation of Java code, Mainactivity.java:
1 PackageCom.example.administrator.valueanimatordemo;2 3 ImportAndroid.animation.TypeEvaluator;4 ImportAndroid.animation.ValueAnimator;5 ImportAndroid.graphics.PointF;6 ImportAndroid.os.Bundle;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.view.View;9 ImportAndroid.widget.Button;Ten One Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener { A - PrivateButton Btn_click; - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + Initview (); - } + A Private voidInitview () { atBtn_click =(Button) Findviewbyid (R.id.btn_click); -Btn_click.setonclicklistener ( This); - } - - @Override - Public voidOnClick (View v) { in Switch(V.getid ()) { - CaseR.id.btn_click: to //StartAnimator1 ();//Timer Implementation + - //In addition to the Valueanimator.ofint () in the StartAnimator1 () method, we also have the //offloat (), Foobject () methods of these generators; * //we can implement a custom value generator by Foobject () (see StartAnimator2 () method) $ StartAnimator2 ();Panax Notoginseng Break; - } the } + A Private voidStartAnimator1 () { the FinalValueanimator animator = valueanimator.ofint (0, 100); +Animator.setduration (5000); -Animator.addupdatelistener (NewValueanimator.animatorupdatelistener () { $ @Override $ Public voidonanimationupdate (valueanimator animation) { - /** - * With such a listener event, we can get the * The value produced at each step of the valueanimator. - *Wuyi * Gets the value produced by each time factor by calling Getanimatedvalue (). the * */ -Integer value =(Integer) animation.getanimatedvalue (); WuBtn_click.settext (Value + "")); - } About }); $ Animator.start (); - } - - Private voidStartAnimator2 () { AValueanimator animator = Valueanimator.ofobject (NewTypeevaluator<pointf>() { + @Override the PublicPointF Evaluate (floatFraction, PointF startvalue, PointF endvalue) { - return NULL; $ } the }); the } the}
In the StartAnimator2 () method, since we are generating our own custom values, we need to create a new typeevaluator and specify the type of our custom values by generics (PointF in the demo). By rewriting a method like evaluate, we can return a variety of values, and its first parameter, fraction, is the time factor mentioned earlier, which is a numeric value that changes from 0~1 to the other. There are also startvalue parameters and Endvalue parameters, which are combined to produce all of the values we want to generate by means of various calculations. Not only does this produce a common data structure, but it also allows you to define more complex data structures through generics.
For example, the generic type used here is pointf, and by rewriting the Evaluate method, we add a variety of calculations to our point. We can modify the various coordinates of point to complete the subsequent processing. As a result, Valueanimator is very powerful and flexible. It can achieve many effects that are not intended. Not only can it be used in animations, it can be used in many places on Android.
Summarize:
1. Common Properties:
Translationx\translationy Horizontal and vertical direction offset
Rotation, rotationx\rotationy rotation refers to the 3D Flip, rotationx\rotationy refers to the horizontal and vertical direction of a rotation animation
Scalex\scaley x-axis direction, and y-axis scaling of an animation
X\y will move to a specific point
Alpha Transparency Animation
2. Common methods and classes
Valueanimator Numerical Generator
Objectanimator is a subclass of Valueanimator that encapsulates valueanimator and makes it easier to use property animations
Animatorupdatelistener
Animatorlisteneradapter, these 2 are for monitoring events.
Propertyvaluesholder
Animatorset These 2 are used to control the animation of the collection of a display effect, order
Typeevaluators Value Calculator
Interpolators interpolator for controlling the variation of specific numerical values
An introduction to the Valueanimator of Android properties animation