Android development art Exploration Study Notes (7), android art Exploration
Chapter 7 in-depth analysis of Android Animation
Android animations are divided into three types: View animation, frame animation, and attribute animation. Frame Animation is a View animation.
7.1 View animation
A View animation has four animation effects: translation, Scale, rotation, and Alpha ).
7.1.1 View animation types
| Name |
Tag |
Subclass |
Effect |
| Translation |
<Translate> |
TranslateAnimation |
Move View |
| Zoom |
<Scale> |
ScaleAnimation |
Zoom in or out View |
| Rotate |
<Rotate> |
RotateAnimation |
Rotate View |
| Transparency |
<Alpha> |
AlphaAnimation |
Change View transparency |
Saved path of the View animation: res/anim/filename. xml. The XML format syntax is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: interpolator = "@ anim/interpolator_resource" android: Using interpolator = "true | false"> <alpha android: fromAlpha = "float" <! -- Transparency start value --> android: toAlpha = "float"/> <! -- Transparency end value --> <scale android: fromXScale = "float" <! -- Start value of horizontal scaling --> android: toXScale = "float" <! -- End value of horizontal scaling --> android: fromYScale = "float" <! -- Vertical Scaling start value --> android: toYScale = "float" <! -- End value of vertical scaling --> android: Export Tx = "float" <! -- Zoom axis x coordinate --> android: Ty = "float"/> <! -- Zoom axis y coordinate --> <translate android: fromXDelta = "float" <! -- Start position of x --> android: fromYDelta = "float" <! -- Y start position --> android: toXDelta = "float" <! -- End position of x --> android: toYDelta = "float"/> <! -- Y end position --> <rotate android: fromDegrees = "float" <! -- Starting angle --> android: toDegrees = "float" <! -- End angle --> android: Export Tx = "float" <! -- Axis of rotation x coordinate --> android: Ty = "float"/> <! -- Y coordinate of the rotation axis --> <set>... </set>
<Set> A tag indicates an animation set, which corresponds to the AnimationSet class. Other animation sets can be nested inside.
Android: The interpolator interpolation device specifies the speed at which the animation is running. The default speed is the acceleration and deceleration interpolation device.
Android: whether the animation in The Notebook interpolator set shares the same interpolation with the set.
Android: duration animation duration
Android: whether the View stays at the end position after the fillAfter animation ends.
<Scale> and <rotate> the central point of the View by default (the top left corner appears to be verified ).
Load the animation defined in xml in the Code:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view1); button.startAnimation(animation);
You can use the setAnimationListener method of Animation to add a listener to the View Animation.
7.1.2 custom View animation
Principle: Inherit the abstract class "Animation", rewrite its initialize and applyTransformation methods, initialize them in initialize, and perform corresponding matrix transformation in applyTransformation. Camera is usually used to simplify the matrix transformation process. The process of custom View animation is mainly the process of matrix transformation. For more information, see Rotate3dAnimation in APIDEMO.
7.1.3 Frame Animation
Frame Animation is actually playing a set of pre-defined images in sequence. Use AnimationDrawable to use frame animation. The XML format syntax is as follows:
<?xml version="1.0" encoding="utf-8" ?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" 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>
It is used as the background of the View and played through Drawable.
button.setBackgroundResource(R.drawable.view2);AnimationDrawable drawable=(AnimationDrawable)button.getBackground();drawable.start();
7.2 Special Use Cases of View animation
7.2.1 LayoutAnimation
Apply to ViewGroup and specify an animation for ViewGroup. In this way, when its child element appears, it will have this animation effect.
Steps:
(1) define LayoutAnimation
<?xml version="1.0" encoding="utf-8" ?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/anim_item"/>
Android: the time delay when the delay sub-element starts the animation. For example, if the sub-element is set to 200 ms, 0.5 means that each sub-element needs to be delayed for ms to start playing the animation, that is, the first player starts playing after ms, the second player starts playing after ms, the third player starts playing after ms, and so on.
Android: animation sequence of the animationOrder sub-elements, normal (sequential display), reverse (reverse display), and random (random ).
(2) specify an animation for the child element
<?xml version="1.0" encoding="utf-8" ?><set xmlns:android="http://schemas.android.com/apk/res/android" 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 attribute for ViewGroup
android:layoutAnimation="@anim/anim_layout"
You can also use LayoutAnimationController to specify an animation for the ViewGroup.
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item); LayoutAnimationController controller=new LayoutAnimationController(animation); controller.setDelay(0.5f); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); linearlayout.setLayoutAnimation(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 to take effect.
EnterAnim -- animation when Activity is opened
ExitAnim -- animation when Activity exits
Start Activity:
Intent intent =new Intent(this,TestActivity.class); startActivity(intent); overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
Exit Activity:
@Override public void finish() { super.finish(); overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim); }
7.3 property Animation
The API level must be greater than or equal to 11. The storage path is res/animator/. Attribute animation can be used to animation any object. Common examples include ValueAnimator and ObjectAnimator.
7.3.1 use property Animation
The default time interval for property animation is 300 ms, and the default frame rate is 10 ms/frame. This allows you to change an object from one property value to another within one time interval. If you want attribute animation to be compatible with earlier versions, you need to use the NineOldAndroids open-source animation library.
Examples show how to use property Animation:
(1) Change the translationY attribute of an object so that it can pan up a distance along the Y axis: its height.
ObjectAnimator.ofFloat(myObject,"translationY",-myObject.getHeight());
(2) Change the background color of a View so that the gradient from 0xFFFF8080 to 0xFF8080FF can be used within 3 seconds. The animation loops infinitely and is reversed.
ValueAnimator colorAnim=ObjectAnimator.ofInt(this,"backgroundColor",0xFFFF8080,0xFF8080FF); colorAnim.setDuration(3000); colorAnim.setEvaluator(new ArgbEvaluator()); colorAnim.setRepeatCount(ValueAnimator.INFINITE); colorAnim.setRepeatMode(ValueAnimator.REVERSE); colorAnim.start();
The XML format Syntax of property animation is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: ordering = "sequentially | together"> <! -- Together: the sub-animation is played simultaneously. Sequentially: Automated playback in sequence --> <objectAnimator android: duration = "int" <! -- Animation duration --> android: propertyName = "string" <! -- Property name --> android: repeatCount = "int" <! -- Repeated times --> android: repeatMode = "restart | reverse" <! -- Duplicate 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: number of cycles of the repeatCount animation. The default value is 0.-1 indicates an infinite loop;
Android: repeatMode repeat: continuous repetition; reverse: reverse repetition (after the first playback, the second playback is reversed, and the third playback is repeated ).
Attribute animation can be used in java code after being defined in XML.
AnimatorSet set=(AnimatorSet) AnimatorInflater.loadAnimator(context,R.anim.property_animator);set.setTarget(button);set.start();
In actual development, we recommend that you use code to implement property animation. Do not use xml.
7.3.2 understand interpolation and Estimator
Interpolation tool: calculates the percentage of changes in the current attribute value based on the percentage of time elapsed;
Estimator: calculates the changed attribute value based on the percentage of the current attribute.
7.3.3 property animation listener
AnimatorListener listens to the start, end, cancel, and replay of an animation;
AnimatorUpdateListener listens to the entire animation process.
7.3.4 animation of any attribute
To make an animation of the object attribute abc, the following two conditions must be met to make the animation take effect:
(1) The setAbc method must be provided for the object. If the initial value is not transmitted during the animation, The getAbc method must be provided; otherwise, the program will directly crash the object;
(2) The changes made by setAbc of the object to the attribute abc must be reflected in some way, for example, the changes to the UI.
For how to add an animation to an original object, see method 2 on the p285 page;
7.3.5 how property animation works
Property animation requires that the animation objects provide the set Method of the property. The property animation removes the set Method multiple times based on the initial value and final value of the property you pass and the animation effect. The values passed to the set method are different each time. Specifically, over time, the passed values become closer and closer to the final values. If the initial value is not transmitted during the animation, The get method is also provided because the system needs to obtain the initial value of the attribute.
7.4 precautions for using animations
Note: View animation is an animation of the View image, which does not really change the View State. Therefore, sometimes the View cannot be hidden after the animation is completed, that is, setVisibility (View. GONE) is invalid. In this case, you only need to call view. clearAnimation () clears the View animation to solve this problem.
After a View is moved (translated), a click event cannot be triggered in a new position on a system earlier than Android, whether it is a View animation or a property animation. At the same time, the click event can still be triggered at the old location. Although the View does not exist visually, after the View is moved back to the original position, the Click Event of the original position continues to take effect. Starting from 3.0, the trigger bit of the Click Event of the property animation is set to the moving position, but the View animation is still in the original position.