Property animation for Android (2), why? /A> android platform was released
Previous Article (link: http://www.cnblogs.com/jerehedu/p/4458928.html), we have a simple understanding of the property animation, and the actual use of ObjectAnimator, AnimatorSet to complete a simple example, this continue to the remaining content for learning.
First, let's take a look at how animation is calculated? The following figure is displayed in the official document:
The figure describes the main types of interaction in the animation process. The ValueAnimator object can record the animation time track. ValueAnimator contains a TimeInterpo; ator, which is used to describe the time interpolation of an animation and a TypeEvaluator. This attribute is used to specify how the animation property value is calculated.
After you create a ValueAnimator and start the animation, ValueAnimator calculates a score ranging from 0 to 1 during the animation. This score indicates the percentage of the animation. After ValueAnimator calculates a running score, it calls the currently set TimeInterpolator to calculate the interpolation score. After the interpolation score is calculated, ValueAnimator calls TypeEvaluator to calculate the attribute value based on the interpolation score.
ValueAnmiator is the parent class of ObjectAnimator we used in the previous chapter. It should be noted that ValueAnimator does not directly operate an object or attribute, which means that if we want to use ValueAnimator to complete the animation, you must add a listener for ValueAnimator:
ValueAnimator animator = ValueAnimator. ofFloat (0.0f, 360f); animator. addUpdateListener (new ValueAnimator. animatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {// The current animation progress float curValue = (Float) animation. getAnimatedValue (); imageView. setRotationX (curValue) ;}}); animator. setDuration (4000); animator. setInterpolator (new DecelerateInterpolator (); animator. start ();
:
When the ofInt and ofFloat values of ValueAnimator cannot meet the requirements, we can use the ofObject method. In this case, we need to inherit the TypeEvaluaot <T> customization.
In addition to the AnimatorUpdateListener, the property animation framework provides the Animator. AnimatorListener listener for us:
Android provides the default implementation of this listener, Which is AnimatorListenerAdapter. We only need to inherit this class. The reference code is as follows:
animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { PropertyValuesHolder holderX =PropertyValuesHolder.ofFloat("translationX",0f,260f); PropertyValuesHolder holderY=PropertyValuesHolder.ofFloat("translationY",0f,260f); ObjectAnimator.ofPropertyValuesHolder(imageView,holderX,holderY) .setDuration(4000) .start(); } });
Common attributes in property animation include:
TranslationX and TranslatioinY: coordinates on the left and top of the layout container
Rotation, rotationX, and rotationY: Rotation attributes
ScalX and scalY: Scaling attribute
TX and Ty: Set the center point for rotation and scaling.
X and y: Coordinates
Alpha: Transparency
Like view animation, in addition to using java code to complete animation, we can also use xml documents to compile attribute animation. Note that the directory of the xml file is no longer anim, but to create an animator folder under res for storage.
The Xml Code is as follows:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <set android:ordering="together"> <objectAnimator android:propertyName="translationX" android:valueFrom="0" android:valueTo="260" android:duration="2000" android:valueType="floatType"/> <objectAnimator android:propertyName="translationY" android:valueFrom="0" android:valueTo="260" android:valueType="floatType" android:duration="2000"/> </set> <objectAnimator android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:duration="2000"/></set>
Use in java:
AnimatorSet animatorSet = (AnimatorSet)AnimatorInflater.loadAnimator(this,R.animator.myanimator); animatorSet.setTarget(imageView); animatorSet.start();
For more details, click Download source code!