Chapter 7 In-depth analysis of Android animation
Android animations are divided into three types: View animation, frame animation, and attribute animation. Frame animation belongs to View animation.
7.1 View animation
The object of View animation is View, and there are four animation effects: Translate, Scale, Rotate, and Transparency.
7.1.1 Types of View Animation
The save path of View animation: res/anim/. The XML format syntax is as follows:
<?xml version="1.0" encoding="utf-8" ?> <set xmlns:andro android:interpolator="@anim/interpolator_resource" android:shareInterpolator="true|false"> <alpha android:fromAlpha="float"<!-- Transparency start value--> android:toAlpha="float"/><!-- Transparency end value--> <scale android:fromXScale="float"<!--Scaling the starting value horizontally --> android:toXScale="float"<!--Horizontal scaling end value --> android:fromYScale="float"<!--Scaling the starting value vertically --> android:toYScale="float"<!--Scaling the end value in vertical direction --> android:pivotX="float"<!--Scaling axis pointxcoordinate --> android:pivotY="float"/><!--Scaling axis pointycoordinate --> <translate android:fromXDelta="float"<!--xThe starting position--> android:fromYDelta="float"<!--yThe starting position--> android:toXDelta="float"<!--xEnd position--> android:toYDelta="float"/><!--yEnd position--> <rotate android:fromDegrees="float"<!--Starting angle --> android:toDegrees="float"<!-- End angle--> android:pivotX="float"<!-- Rotating axis pointxcoordinate --> android:pivotY="float"/><!-- Rotating axis pointycoordinate--> <set> ... </set> </set>
The <set> tag represents an animation collection, corresponding to the AnimationSet class, and other animation collections can be nested inside.
android:interpolator interpolator specifies how fast the animation has run, and defaults to the acceleration and deceleration interpolator.
android:shareInterpolator Whether the animation in the collection shares the same interpoler as the collection.
android:duration animation duration
android:fillAfter Whether the View stays at the end position after the animation is over.
The center axis points of <scale> and <rotate> are by default the center point of the View (it seems to be the upper left corner to be verified).
Load the animation defined in xml in the code:
Animation animation = (this, .view1); (animation);
Use the setAnimationListener method of Animation to add monitoring to View animation.
7.1.2 Custom View Animation
Principle: Inherit the abstract class Animation, then rewrite its initialize and applyTransformation methods, initialize in initialize, and perform corresponding matrix transformation in applyTransformation. Camera is usually used to simplify the process of matrix transformation. The process of customizing View animation is mainly the process of matrix transformation. For example, refer to the Rotate3dAnimation effect in APIDEMO.
7.1.3 Frame animation
Frame animation is actually playing a set of predefined pictures sequentially. Use frame animation via AnimationDrawable. The XML format syntax is as follows:
<?xml version="1.0" encoding="utf-8" ?> <animation-list xmlns:andro android:oneshot="true|false"> <item android:drawable="@mipmap/ic_launcher" android:duration="500"/> <item android:drawable="@mipmap/ic_launcher" android:duration="500"/> <item android:drawable="@mipmap/ic_launcher" android:duration="500"/> </animation-list>
When using it, just use it as the background of the View and play it through Drawable.
(.view2); AnimationDrawable drawable=(AnimationDrawable)(); ();
7.2 Special usage scenarios for View animation
7.2.1 LayoutAnimation
It acts on the ViewGroup and assigns an animation to the ViewGroup so that it will have this animation effect when its child elements appear.
step:
(1) Define LayoutAnimation
<?xml version="1.0" encoding="utf-8" ?> <layoutAnimation xmlns:andro android:delay="0.5" android:animationOrder="normal" android:animation="@anim/anim_item"/>
android:delay The time delay for the child element to start animation. For example, if the child element sets the duration to 200ms, then 0.5 means that each child element needs to be delayed by 100ms to start playing the animation. That is, the first one starts playing after 100ms, the second one starts playing after 200ms, the third one starts playing after 300ms, and so on.
android:animationOrder child elements animation order, normal (sequential display), reverse (sequential display), random (random).
(2) Specify animation for child elements
<?xml version="1.0" encoding="utf-8" ?> <set xmlns:andro android:duration="300" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" /> <translate android:fromXDelta="500" android:toYDelta="0" /> </set>
(3) Specify the android:layoutAnimation property for ViewGroup
android:layoutAnimation="@anim/anim_layout"
Or use the LayoutAnimationController to specify an animation for the ViewGroup.
Animation animation = (this, .anim_item); LayoutAnimationController controller=new LayoutAnimationController(animation); (0.5f); (LayoutAnimationController.ORDER_NORMAL); (controller);
7.2.2 Activity switching effect
Use the overridePendingTransition(int enterAnim, int exitAnim) method, note that this method must be called after startActivity or finish before it can take effect.
enterAnim - Animation when Activity is opened
exitAnim - Animation when Activity exits
Start Activity:
Intent intent =new Intent(this,); startActivity(intent); overridePendingTransition(.enter_anim,.exit_anim);
Exit Activity:
@Override public void finish() { (); overridePendingTransition(.enter_anim,.exit_anim); }
7.3 Attribute animation
The API level must be greater than or equal to 11, and the storage path is res/animator/ directory. Attribute animation can animate any object. Common ones include ValueAnimator and ObjectAnimator.
7.3.1 Using attribute animation
The default time interval of attribute animation is 300ms and the default frame rate is 10ms/frame, which can complete the change of the object from one attribute value to another attribute value within one time interval. If you want attribute animation to be compatible with lower version systems, you need to use the open source animation library NineOldAndroids.
Give an example of how to use attribute animation:
(1) Change the translationY property of an object and let it translate a distance along the Y axis: its height.
(myObject,"translationY",-());
(2) Change the background color of a View, let its background color achieve a gradient of 0xFFFF8080 to 0xFF8080FF within 3 seconds, and animate infinite loops and add inversion effects.
ValueAnimator colorAnim=(this,"backgroundColor",0xFFFF8080,0xFF8080FF); (3000); (new ArgbEvaluator()); (); (); ();
The XML format syntax of attribute animation is as follows:
<?xml version="1.0" encoding="utf-8" ?> <set xmlns:andro android:ordering="sequentially|together"><!--together:Sub-animation plays simultaneously。sequentially:Automation plays in sequence--> <objectAnimator android:duration="int"<!--Animation duration--> android:propertyName="string"<!--Attribute name--> android:repeatCount="int"<!--Number of repetitions--> android:repeatMode="restart|reverse"<!--Repeat mode--> android:startOffset="int"<!--Delay time--> android:valueFrom="float|int|color"<!--Attribute start value--> android:valueTo="float|int|color"<!--Attribute end value--> android:valueType="colorType|intType|floatType|pathType" /><!--Attribute Type--> <animator android:duration="int" android:repeatCount="int" android:repeatMode="restart|reverse" android:startOffset="int" android:valueFrom="float|int|color" android:valueTo="float|int|color" android:valueType="colorType|intType|floatType|pathType" /> </set>
android:repeatCount The number of loops in the animation, default is 0, and -1 is an infinite loop;
android:repeatMode repeat:continuous repeat; reverse: reverse repeat (after the first playback, the second playback is played backwards, and the third playback is repeated so repeatedly).
After defining the attribute animation in XML, it can be used in Java code.
AnimatorSet set=(AnimatorSet) (context,.property_animator); (button); ();
In actual development, it is recommended to use code to implement attribute animation, and do not use xml.
7.3.2 Understanding interpolation and valuators
Interpolation: calculates the percentage of the current property value changing based on the percentage of time elapsed;
Evaluator: Calculate the changed attribute value based on the percentage of the current attribute.
7.3.3 Attribute animation monitor
AnimatorListener monitors the start, end, cancel, and repeat play of the animation;
AnimatorUpdateListener listens to the entire animation process.
7.3.4 Animate any attributes
To animate the object's attribute abc, if you want the animation to take effect, you need to meet the following two conditions:
(1) object must provide the setAbc method. If the initial value is not passed during the animation, then the getAbc method must be provided, otherwise the program will directly crash;
(2) The changes made by object's setAbc to the attribute abc must be reflected through some method, such as bringing changes to the UI.
To add animation to an original object, see Method 2 on page p285;
7.3.5 How attribute animation works
Attribute animation requires the object that the animation acts as a set method for the attribute. Attribute animation uses the set method multiple times based on the initial and final values of the attribute you pass, as well as the animation effect. The value passed to the set method is different every time. To be precise, over time, the passed value becomes closer and closer to the final value. If the initial value is not passed during the animation, then a get method must be provided because the system needs to obtain the initial value of the attribute.
7.4 Notes on using animation
Note that View animation is animating the View image, and does not really change the state of the View. Therefore, sometimes the View cannot be hidden after the animation is completed, that is, setVisibility() is invalid. At this time, just call () to clear the View animation to solve this problem.
After moving (panning), on systems before Android 3.0, the new position cannot trigger the click event, whether it is View animation or attribute animation, and the old position can still trigger the click event. Although the View no longer exists visually, after moving the View back to its original position, the click event in the original position continues to take effect. Starting from 3.0, the click event trigger position of the property animation is the position after moving, but the View animation is still in its original position.
I will introduce so much to you about Android Development Art Exploration Learning Notes (7) and will continue to update the knowledge related to Android Art Exploration in the future. Please continue to pay attention to this site, thank you.