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

How to open the "All Photos" permission in iOS 14

Apple has brought new privacy management features...

Interesting fact: Does exercise have an impact on human life expectancy?

Some time ago, I was watching videos and saw that...

How to help Socrates find the most beautiful flower?

Once upon a time, there was a story that told the...

Does breathing through the mouth really ruin your appearance?

We need to breathe every moment, but have you eve...

"Harbin, you make me feel strange..." What kind of city is Harbin?

In winter, there is a kind of happiness called &q...

Why is iPhone unwilling to give up 16GB?

According to foreign media reports, if Apple foll...

The logic behind creating popular articles on Xiaohongshu is all here!

Although I have talked a lot about Xiaohongshu no...