In the previous article, I introduced how to customize view animation in Android, which uses a matrix transformation method for animation processing. For details, refer to my previous article: [Basic Android knowledge] creating your own animation Effects
Through the transformation matrix introduced in this article, we can achieve most of the animation effects. However, most of this method implements a 2D effect, what are the methods to achieve the effect of 3D space? Next we will introduce the camera class, which means cameras in Chinese. Of course, this is not the camera used for photography on our mobile phone. This is a logical concept, compare the screen of our mobile phone to the camera window. Through this window, we can see what is displayed in it (that is, the interface of our application). Of course, if we look at the objects on the screen from different angles, naturally, there will be a three-dimensional effect. What does it mean from different angles? For example, if there is a cup on the table, we can see it on the front, from the end, we can see that it is another one.
This concept should not be hard to understand, and the camera class is used to do this. Let's take a look at the following code:
- Private camera = new camera ();
- .......
- Protected void applytransformation (float interpolatedtime, transformation T ){
- Final matrix = T. getmatrix ();
- Camera. Save ();
- Camera. Translate (0.0f, 0.0f, (3000-3000.0f * interpolatedtime ));
- Camera. rotateys (360 * interpolatedtime );
- Camera. getmatrix (matrix );
- Matrix. pretranslate (-halfwidth,-halfheight );
- Matrix. posttranslate (halfwidth, halfheight );
- Camera. Restore ();
- }
Copy code The following explains the basic concepts of my previous article. If you need to know the pre-knowledge, refer to the previous article.
Similar to the previous article, we first got a transformation matrix, camera. save () saves the current camera position so that the camera can be restored to the original position after the transformation is completed. Next, call camera. translate. This method accepts three parameters, x, y, and z. Here we only offset the Z axis, and the starting offset is 3000, over time, this offset will become smaller and smaller. This will produce such an effect. Our view is moving from a very long distance to us, getting closer and closer, and finally moving to the top of our window ~
The camera. rotatey (360 * interpolatedtime) clause adds a rotation effect to our view. During the moving process, the view also shifts the Y axis to the center for rotation.
The following camera. getmatrix (matrix), which applies a series of transformations we just defined to the transformation matrix. After calling this sentence, we can restore the location of camera, for the next use. The next call has been introduced in the previous article, so I will not repeat it again ~, After this code is completed, we can see the 3D animation effect ~
Previous:
|