Young man, it’s time to create your first cool 3D effect!

Young man, it’s time to create your first cool 3D effect!

Background

There are two Camera classes in Android. One is android.hardware.Camera, which is used to operate the device's camera. The other is android.graphics.Camera, which can be used to perform 3D transformations and then apply the transformed matrix Matrix to Canvas, etc. This article will introduce this Camera class.

Camera

As we mentioned before, Camera is a class that can perform 3D transformations. After performing 3D transformations, we can assign the transformation matrix Matrix through mCamera.getMatrix(Matrix) and then use it on Canvas. Alternatively, you can directly apply the transformation to a Canvas through mCamera.applyToCanvas(Canvas).

Three-dimensional coordinate axes in Android

The 3D coordinate axes in Android conform to the left-handed coordinate system.

The default position of the Camera is (0, 0, -8).

Camera transformation operations

method illustrate
getMatrix(mMatrix) Assign values ​​to mMatrix.
applyToCanvas(mCanvas) Apply the transformed Matrix directly to mCanvas.
rotate(x,y,z) Rotate.
rotateX, rotateY, rotateZ Rotate.
getLocationX, getLocationY, getLocationZ Get the position of the Camera, the default is (0,0,-8).
setLocation(x,y,z) Set the camera's position.
translate(x,y,z) Pan the Camera.
save() Similar to Canvas.
restore() Similar to Canvas.

Camera does not have many methods, so it is relatively simple and clear to use.

Camera usage examples

Since the core of using Camera is to obtain a transformed Matrix, you need to have a certain understanding of Matrix.

Demo1

3D ViewGroup Demo

Camera is used for custom animation

Here is a code example. The usage is essentially the same as the previous example. Both use the Camera transformation to obtain the Matrix matrix.

  1. public class Custom3DAnimation extends Animation {
  2.  
  3.  
  4. private Camera mCamera;
  5.  
  6. private int centerWidth;
  7.  
  8. private int centerHeight;
  9.  
  10.  
  11. public void setmRotateY( float mRotateY) {
  12.  
  13. this.mRotateY = mRotateY;
  14.  
  15. }
  16.  
  17.  
  18. private float mRotateY;
  19.  
  20.  
  21. public Custom3DAnimation() {
  22.  
  23. mCamera = new Camera();
  24.  
  25. mRotateY = 90;
  26.  
  27. }
  28.  
  29.  
  30. @Override
  31.  
  32. protected void applyTransformation( float interpolatedTime, Transformation t) {
  33.  
  34. Matrix matrix = t.getMatrix(); //Get the Transformation Matrix
  35.  
  36. mCamera.save(); //Save the current lens status
  37.  
  38. mCamera.rotateY(mRotateY * interpolatedTime); //Rotate the camera
  39.  
  40. mCamera.getMatrix(matrix); //Apply the rotation transformation to the matrix
  41.  
  42. mCamera.restore(); //Merge lens layer
  43.  
  44. matrix.preTranslate(centerWidth, centerHeight); //Translate before operation
  45.  
  46. matrix.postTranslate(-centerWidth, -centerHeight); //Translate after operation
  47.  
  48.  
  49. }
  50.  
  51.  
  52. @Override
  53.  
  54. public void initialize( int width, int height, int parentWidth, int parentHeight) {
  55.  
  56. super.initialize(width, height, parentWidth, parentHeight);
  57.  
  58. setDuration(5 * 1000); //Set the default duration
  59.  
  60. setFillAfter( true ); //Set whether to keep the state after the animation ends
  61.  
  62. setInterpolator(new LinearInterpolator()); //Set the interpolator
  63.  
  64. centerWidth = width / 2;
  65.  
  66. centerHeight = height / 2;
  67.  
  68. }
  69.  
  70. }

Summarize

The use of Camera is not complicated. You just need to remember the methods mentioned above. Since Camera eventually outputs a matrix, you also need to have a certain understanding of the matrix. I have given a quick guide to using the matrix above, and you can refer to it according to your situation.

<<:  Summary of Android methods to avoid memory overflow (Out of Memory)

>>:  Technology Post: How to apply AI natively to Android system

Recommend

Security experts recommend not installing these apps on your phone

In recent weeks, tech news headlines have been fi...

What is a nuclear submarine? How is it different from a normal submarine?

Produced by: Science Popularization China Author:...

Tourism promotion, how do travel companies conduct online promotion?

As people's living standards continue to impr...

Hacker Attack and Defense SQLMAP Practical Edition

Course Description This course starts with SQLMAP...

Detailed explanation of using Android HOOK tool Cydia Substrate

Cydia Substrate is a code modification platform. ...

Changes in advertising and marketing: Is “creativity” out of favor?

"Advertising is not art. The purpose of adve...

Knowledge payment, the logic of hot selling

1. Knowledge Entrepreneurship was Hot Recently, a...

The earliest of the four great inventions of China is... | Illustrated Science

What are the Four Great Inventions of China? When...