From the perspective of the influence scope of the animation effect, there are three types of animations in the view system:
- Window Animation: the animation corresponding to a window. Its function object is surface. A window can be a window corresponding to an activity, or a subwindow such as a dialog box. Of course, it can also be directly through windowmanager. any window added by addview;
- Layout Animation: an animation contained in the viewgroup container object. This animation is defined in the viewgroup object, but it actually affects the child view in the container, the essential process is to set different animations for each sub-view based on the layout animation, so that the whole looks like it acts on the entire container;
- View animation: an animation that acts on a specific view object;
Animation types and design ideas
An animation is represented by an animation class. The animation start time is saved in this class, and different Animation Parameters are returned at different times after the animation starts, the transformation class stores the animation parameter information (including the mmatrix and malpha variables) and applies these parameters to the corresponding animation body (for window animation, the subject is the surface; for Layout animation and view animation, the subject is in view), and then the invalidate-related animation subject will use new parameters to display the new screen the next re-painting, in this way, continuous animation effects are produced like watching a movie during the entire animation interval, but in essence it is still composed of static images, but when there are more than 60 frames per second, the human eyes will basically not feel choppy or discontinuous;
The view system currently supports five basic animations: translate, scale, rotate, twist skew, and transparent Alpha;
In addition to twisting skew, the other four types of animations can be declared with corresponding labels in XML. The Code layer corresponds to several sub-classes of animation, such as translateanimation/scaleanimation/rotateanimation/alphaanimation;
Combination of animations
During our development process, animations may often be combined, for example, first translation and then scaling. This requires a unified model to process these five basic animations, in fact, each basic animation corresponds to a basic mathematical transformation formula, and the matrix operation can easily unify these transformation formulas, you only need to change the value of a column in a row in the Matrix. In Android, the 3x3 matrix is used. (in principle, the 2x3 matrix can meet the storage requirements of these animation parameters, however, two 2x3 matrices cannot be combined. Therefore, a 3x3 matrix is used to save the parameters related to these five animations. The Code layer is the matrix class, this class encapsulates four Animation Parameters except Alpha and related operation APIs. There are three main types:
- Setxxx: this series of methods is used to directly set relevant Animation Parameters. Calling this series of methods will directly clear the previously set parameters;
For example, the Code is M. pretranslate (...); M. setrotate (.); so that the pretranslate before set will be cleared, which is equivalent to the unmiraculous effect of the sentence code;
- Prexxx: this series of methods are used to set an animation first. Each pre method call is based on the previous pre method;
For example, the Code is M. pretranslate (...); M. prescale (.); this indicates that the scaling and translation are performed first;
- Postxxx: this series of methods are used to set and then perform an animation. Each POST method call is also based on the previous post method, and the post method is always after the pre method and has nothing to do with the code sequence;
For example, the Code is M. posttranslate (...); M. prescale (.); M. postrotate (.); this indicates first scaling, then translating, and finally rotating;
For example, M. posttranslate (..); m. setskew (.); m. postrotate (.); m. prescale (.); this indicates first scaling, then twisting, and finally rotating, and the translation is cleared because it is before the set;
To achieve the effect of a composite animation, You can inherit from the animation class to implement a custom class. The core is to reload the void applytransformation (float interpolatedtime, transformation T) method;
Note: The changed Animation Parameters are actually saved in parameter T. T will automatically create an empty transformation object during class initialization, and then call T. getmatrix () obtains the matrix and performs related settings;
At the code level, you also need to know the interpolator class. It is an interpolation tool that allows developers to customize the animation curve over time, for example, the common problems include constant speed motion, linear acceleration, curve acceleration, and first acceleration and then deceleration. In fact, this is a mathematical model problem. developers can implement any interpolation;
View animation usage
- You can create an XML file in the Res/drawable directory. Like the new shape/selector set, the animation_list label is used internally to set the image to be displayed at each time point, in essence, it creates an animation drawable, which can be simply understood as a GIF image with an animation effect or a frame-by-frame animation. The animation effect can be obtained wherever drawable is used, you can call animationdrawable to start and end an animation. start ()/stop () method;
- You can create an XML file in the Res/anim directory, and use the animationset/scale/rotate/translate/Alpha label internally to set these simple animations;
Then, use animationutil. loadanimation (.) to load and convert it to an animation object, and call view. setanimation (.);
- Implement the Custom Animation subclass and directly call view. setanimation (.); Complete the animation settings and control the animation start and end through animation. Start ()/cancel;
When a view animation is executed, it actually changes the position of the view and does not change its attributes. For example, if you scale a button, during the animation execution, the button size changes, but the button's click response area does not actually change;
Use of layout Animation
- You can create an XML file in the Res/anim directory. The layoutanimation label is used internally to define the layout animation. The code level corresponds
The layoutanimationcontroller class references a specific view animation using the Android: animation tag. in XML, the layout container that requires animation (for example, listview/linearlayout) uses the label layoutanimation to reference the layout animation just defined. When the layout container is initialized, the relevant animation settings are automatically parsed, in the dispatchdraw () method, the layout animation is converted to the view animation of the corresponding sub-view. startlayoutanimation () starts the animation;
Q: Can I manually stop a layout animation? If you are interested, find the answer by yourself;
- To customize the layoutanimationcontroller subclass, the key is to reload
Long getdelayforview (view) method. It is used to return the specific delay millisecond value based on the index of the Child view in the parent view. viewgroup can be called directly. setlayoutanimation (.); complete the animation settings through layoutanimationcontroller. start () starts the animation;
Use of window Animation
The specific implementation of window animation is in the windowmanagerservice. applyanimation () method. Here we will mainly introduce the use of the window animation corresponding to the activity, that is, the animation during activity switching;
- Like view animation, create an XML animation file in the Res/anim directory for window entry and closure, and then execute activity. startactivity (intent); then call activity. overridependingtransition (..) complete the settings of closing the old window and entering the animation effect in the new window;
- Use activityoptions. makexxx (...) A series of constructor Methods return a specific instance and pass
Activityoptions. tobundle () converts it into a bundle object, and finally calls activity. startactivity (intent, bundle); starts activity;
Note: The difference from the previous method is that when a bundle parameter is added during startup, the parameter contains the relevant animation parameters;
Activityoptions. makexxx () methods currently provide three types:
- Makecustomanimation: loads custom animations in the same way as the first method;
- Makescaleupanimation: loads an animation that scales from a specified vertex to the final display window;
- Makethumbnailscalexxxanimation: loads animations from the display window to the none, or from the none to the specified window;
Attribute Animation
Property animations appear in 3.0, and related classes are in Android. animation, while older Animations (all mentioned above) Support attribute animations in Android. View. animation;
Compared with view animation, attribute animation is more flexible. It can be used not only for view, but also for any object. During the animation, it changes the specific attribute of view, similarly, the button scaling operation changes the specific size and position of the button during the animation, and the effective click response area also changes with the attribute. The common usage is as follows:
- The most common method is to use objectanimator. ofxxx (object target, string propertyname ,...) Obtain the animator instance and call the START () method to start the animation;
Specifically, target is the subject of the animation to be executed, for example, a button object, and propertyname is the name of the property to be changed, such as Alpha. To change multiple attributes, you can use propertyvaluesholder to save the changes of each attribute;
Note: To use objectanimator, the property to be changed must have the set/get method;
- Use the valueanimator. ofxxx (...) method to obtain the animator instance and call
Addupdatelistener (animatorupdatelistener listener) registers the listener for attribute changes. The Listener parameter is the implementation class of the animatorupdatelistener interface, and then calls the START () method to start the animation;
This method has excellent scalability and versatility. The animation execution is completely decoupled from the changes in the specific attribute values in the process. developers can do whatever they want in the implementation of the animatorupdatelistener interface;
- You can create an XML file in the Res/anim directory, and use tags such as set, animator, and objectanimator internally to define the animation information. The code level corresponds to the animatorset, valueanimator, and objectanimator, and then use animatorinflater. loadanimator (..) to load the XML Description and obtain the animator object. settarget (.) set the target subject and start the animation with START;
- Initialize layouttransition and use layouttransition. setanimator (INT transitiontype, animator) specifies the animation in a specific scenario, and then calls viewgroup. setlayouttransition (.) add animation support to the layout container to complete the animation application when the viewgroup subview's visual state changes;
The specific scenarios of transitiontype include:
- Appearing: the animation applied to an element when it becomes visible.
- Disappearing: the animation applied to an element when it becomes invisible.
- Change_appearing
- Change_disappearing: When an element changes to gone, some elements need to be moved because the system needs to re-layout them.