Analysis of animation principles from the source code perspective of android Animation

Source: Internet
Author: User

Analysis of animation principles from the source code perspective of android Animation

I have never understood the android animation mechanism before and how the android system implements animation. As a result, only some animations that have been encapsulated by the android system, namely AlphaAnimation, TranslateAnimation, ScaleAnimation, rotateAnimation and these animations are used together. In fact, the android system provides these animations to meet our basic needs, but it is impossible to do advanced animations, for example, in 3D animation, I took a look at the source code of the animation in the android system and made some summary. The following is my new understanding of animation.

1. the most important class is the Animation class, an abstract class, which is the base class of all animations. It defines the public attributes and methods of Animation. The most important attributes are AnimationListener and Transformation, the animation listener listens to the start, execution, and end of an animation to implement some of its own logic. Transformation is the information contained in each frame of the animation (translation, rotation, Bloom, transparency) the most important method is:

Public boolean getTransformation (long currentTime, Transformation outTransformation); and protected void applyTransformation (float interpolatedTime, Transformation t ).

The first method is called by the system. The Transformation information is calculated based on the current animation time. You do not need to rewrite this method. The second method must be rewritten, the system performs an animation based on the Transformation calculated by the first method.

2. from the above Animation class, you can know the most important attributes and the most important methods. Both of them have a class called Transformation. It can be seen that this class is also very important, in the Tranfrormation class, we can see that the most important attributes are alphat and Matrix. alpha truly stores the transparency information of the animation, while Matrix stores (translation, rotation, and Bloom) information. It can be seen that these two are the carriers that truly store all the information of an animation.

The most important method is getMatrix (). Get all the animation information in the existing matrix in the current frame animation.

3. We can see from the second point that apart from the transparency information, the animation frame information is stored in the Matrix class, so we also need to understand how the Matrix works. In the Matrix class, we can see that this class provides a series of setXXX, preXXX, and postXXX methods, namely, the Rotate, Scale, and Translate settings for the three series. I checked some articles on the Internet and understood the differences between the three methods. The preXXX method is to right-multiply the original matrix, that is, M * A (later M represents the original matrix), and postXXX represents the left multiplication of the original matrix, that is, B * M, setXXX indicates that the original matrix data is cleared first and then right multiplied. The difference between left multiplication and right multiplication is that right multiplication is performed first when the matrix is multiplied, and right multiplication is executed after the right multiplication is completed, which corresponds to the animation effect, first, the result data of the right multiplication matrix is superimposed, and then the result data of the Left multiplication matrix is superimposed. We can see that the effects of left multiplication and right multiplication are completely different. For details about matrix operations, see this article. Android Matrix theory and application. All matrices are processed by the android system. All the programmer has to do is pass the offset to the corresponding method.

4. the android system provides the most basic and two-dimensional animations for us. Therefore, all 3D animations must be implemented by ourselves, while android provides graphic for us. the camera class under the Camera package is the class that implements 3D effects. My understanding of this class is that the android system provides a pair of eyes for our programs. This is Camera. Our human eyes can observe it from the three axes of XYZ. This Camera is also possible by moving the Camera class on different axes, it can also achieve the animation effect. From the code of the Camera class, we can see several of its methods: public native void translate (float x, float y, float z) and public native void rotate (float x, float y, float z), public void getMatrix (Matrix matrix ). the first of these three methods is to move Camera on the three axes of xyz, and shift Camera to the left to achieve the right translation animation effect. Other translation effects are the same. The second method is to rotate on the XYZ axis. By rotating on these three axes, the three-dimensional animation effect can be achieved, the third method is to overwrite all operations on the Camera to the Matric object.

After understanding all the functions of the above four classes, we have basically understood the android animation principle, inherited the base class: Aniomation and rewritten applyTransformation, and implemented some effects using Matrix and Camera, it is an animation.

Below, we use Camera to implement general rotation and 3D rotation, and use Camear to implement our commonly used scaling animation.

1) use Camera to implement Pan animation ..

Package com. example. animationdemo; import android. graphics. camera; import android. graphics. matrix; import android. view. animation. animation; import android. view. animation. transformation; public class extends Animation {private float mFromXValue = 0.0f; private float mToXValue = 0.0f; private float mFromYValue = 0.0f; private float Limit = 0.0f; private float centerX, centerY; private Camera camera; public CameraTranslateAnimation (float fromXValue, float toXValue, float fromYValue, float toYValue, float centerX, float centerY) {this. mFromXValue = fromXValue; this. mToXValue = toXValue; this. mFromYValue = fromYValue; this. mToYValue = toYValue; this. centerX = centerX; this. centerY = centerY;} @ Overridepublic void initialize (int width, int height, int parentWidth, int parentHeight) {// TODO Auto-generated method stubsuper. initialize (width, height, parentWidth, parentHeight); camera = new Camera () ;}@ Overrideprotected void applyTransformation (float interpolatedTime, Transformation t) {float dx = (mFromXValue + (mToXValue-mFromXValue) * interpolatedTime); float dy = (mFromYValue + (mToYValue-mFromYValue) * interpolatedTime); Matrix m = t. getMatrix (); camera. save (); camera. translate (dx, dy, 0); // transmits the camera value to the coordinates to be moved. getMatrix (m); camera. restore (); m. preTranslate (-centerX,-centerY); m. postTranslate (centerX, centerY); // return to Center }}
2) use Camera to implement rotation animation.

 

 

Package com. example. animationdemo; import android. graphics. camera; import android. graphics. matrix; import android. view. animation. animation; import android. view. animation. transformation; public class extends Animation {private final float indexes; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera; public round (float indexes, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) {round = fromDegrees; Round = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; mReverse = reverse;} @ Overridepublic void initialize (int width, int height, int parentWidth, int parentHeight) {// TODO Auto-generated method stubsuper. initialize (width, height, parentWidth, parentHeight); mCamera = new Camera ();}/*** note that the applyTransformation function is also a non-stop loop call process. similar to getView in Adapter * When drawing an animation, each frame is always drawn based on the position, scaling, and translation information in Transformation, to achieve the animation effect */@ Overrideprotected void applyTransformation (float interpolatedTime, Transformation t) {// TODO Auto-generated method stubfinal float fromDegrees = mFromDegrees; float degrees = fromDegrees + (mToDegrees-fromDegrees) * interpolatedTime); // final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix = t. getMatrix (); // get all the animation information matrices in Transformation (all stored in the matrix) camera. save (); if (mReverse) {camera. translate (0.0f, 0.0f,-mDepthZ * interpolatedTime); // implement the translation effect on different axes by moving Camear} else {camera. translate (0.0f, 0.0f, mDepthZ * (1.0f-interpolatedTime);} camera. rotateY (degrees); // use the angle change on the Y axis. If the rotation on the Z axis is used, the rotation is normal. getMatrix (matrix); // This matrix information must be superimposed on the animation matrix, otherwise the settings on the Camera do not work. restore (); matrix. preTranslate (-centerX,-centerY); // return to the center point matrix. postTranslate (centerX, centerY );}}
Summary: By studying Camera, we can easily implement some animations with special effects. only by truly mastering the animation principles of the android system can we truly grasp the implementation of the animation. upload demo code. You can download and view the Code if necessary.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.