Android animation from the perspective of source code, android source code

Source: Internet
Author: User

Android animation from the perspective of source code, android source code

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 ..



Related Article

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: 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.