Android Camera 3D effect

Source: Internet
Author: User

Version: 1.0 Date: 2014.4.14 copyright:©2014 kince reprinted to indicate the source

I. Concepts

To achieve 3D effects in Android, the first thought should be OpenGL ES, because it is almost mentioned in many basic textbooks. However, it is still a little troublesome to use, and it is mainly used in the gaming field. Is there a better choice in terms of application? The answer is yes. You can use the Camera class to complete 3D effects. It has a series of methods of rotation and translation. In fact, after a Matrix object is changed and a series of operations are completed, we can get this Matrix and then draw our objects. In fact, the internal mechanism is opengl, But it greatly simplifies the use.
In this case, some friends may wonder if Camera is not a Camera. Actually, after reading the following picture, I will understand it. Yes, Camera is responsible for cameras in the hardware package; it is graphic in the graphics package, so don't confuse it. Therefore, we will focus on some of the latter's usage. Camera is used to calculate 3D transformations, generate matrices, and then apply them to the canvas. Its constructor has only one Camera () without parameters, which is used to instantiate a Camera with empty conversions. The coordinate system of Camera is the left-hand coordinate system. As shown in: imagine that your Android device is flat on the desktop. the X axis is the horizontal direction of the mobile phone, the Y axis is the vertical direction of the mobile phone, and the Z axis is the vertical direction of the mobile phone. The following are some details:
1. camera is located at coordinate points (0, 0), that is, the upper left corner of the view;

2. camera. translate (, 30) means moving the observed object to the right 10, moving up 20, and moving forward 30 (that is, moving the object away from camera, so that the object will become smaller );

3. camera. rotateX (45) means to rotate 45 degrees clockwise around the X axis. For example, if the middle line and X axis of an object overlap, 45 degrees clockwise rotation around the X axis means that the upper part of the object is flipped inward, and the lower part is flipped outward;

4. camera. rotateY (45) means to rotate 45 degrees clockwise around the Y axis. For example, if the middle line and Y axis of an object overlap, 45 degrees clockwise rotation around the Y axis means that the left half of the object is flipped outward, and the right half is flipped inward;

5. camera. rotateZ (45) means to rotate 45 degrees around the Z axis counterclockwise. For example, if the middle line and the Z axis of an object overlap, 45 degrees clockwise rotation around the Z axis is the top half of the object to the left, and the bottom half to the right. It has few methods and is easy to understand, but is commonly used: Next let's talk about the usage of each method :. 1. applyToCanvas calculates the corresponding matrix based on the current transformation and applies it to the Canvas. Note that the matrix is set by the canvas.
2. rotateX (float degree) rotates degree degrees around the X axis
3. rotateY (float degree) rotates degree degrees around the Y axis.
4. rotateZ (float degree) rotates degree degrees around the Z axis.
5. translate (float x, float y, float z) perform the Transformation Operation on the x, y, and zcoordinate axes.
6. save () and restore () save the original state. After the operation is completed, it is restored to the original state.
In addition, a class must be used with Camera, that is, Matrix. Matrix is used to process three-dimensional Matrix coordinate transformations and is also commonly used for image processing. Matrix provides four operations: translate, rotate, scale, and skew. setValues (...) To change the image. Each operation of Matrix has three operations: set, pre, and post. set is to clear the queue and then add, pre is inserted at the beginning of the queue, and post is inserted at the end of the queue. Except for translate, you can specify the center point for all three operations.
Ii. Example 1: If you want to flip an image, you can write it as follows:
import android.graphics.Camera;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.Transformation;public class Rotate3DAnimation extends Animation {     // 3d rotate     private float mFromDegrees;     private float mToDegrees;     private float mCenterX;     private float mCenterY;         private Camera mCamera;        public Rotate3DAnimation(float fromDegrees, float toDegrees)    {         mFromDegrees = fromDegrees;         mToDegrees = toDegrees;    }     @Override     public void initialize(int width, int height, int parentWidth,               int parentHeight)     {          super.initialize(width, height, parentWidth, parentHeight);          mCenterX = width / 2;          mCenterY = height / 2;          mCamera = new Camera();     }     @Override     protected void applyTransformation(float interpolatedTime, Transformation t)     {                   final float fromDegrees = mFromDegrees;          float degrees = fromDegrees + (mToDegrees - mFromDegrees) * interpolatedTime;                   final Matrix matrix = t.getMatrix();                   mCamera.save();                   mCamera.rotateY(degrees);          mCamera.getMatrix(matrix);          mCamera.restore();          //          matrix.preTranslate(-mCenterX, -mCenterY);          matrix.postTranslate(mCenterX, mCenterY);     }}
Check the code. First, the initialize () method is reloaded to initialize the intermediate coordinates mCenterX, mCenterY, And the instantiated Camera object. initialize () is a callback function that tells Animation the size parameter of the target View, some related parameters can be initialized here, such as setting the animation duration, setting the Interpolator, and setting the animation reference point. The applyTransformation (float interpolatedTime, Transformation t) function is used to customize the animation effect. During the animation process, the applyTransformation function is called repeatedly. The Transformation parameter is used to obtain the transformed matrix. final Matrix matrix = t. getMatrix () can achieve various complex effects by changing the matrix. The Camera class is used to rotate the pivot projection after the Y axis. The current matrix is obtained through t. getMatrix (), and then camera. rotateY rotates the matrix. The preTranslate function moves before the rotation, while postTranslate moves after the rotation is completed. It mainly serves to make the object rotate around its center.
3. In summary, we can see that in order to achieve 3D effects, we also need to use the combination of the Animation class, because this class is just an Animation class and provides a platform for object transformation. In Android apidemo, there is also a 3D effect program named Rotate3dAnimation. The above code is a simplified version of Rotate3dAnimation. The 3D effect described in this article is only the flip effect, but there are many other aspects, such as folding. We will continue to introduce it later. Thank you for your attention.
Source code download



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.