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

Learn English Grammar in 7 Days Baidu Cloud Download

Master English grammar in 7 days, master the most ...

Even the mobile version of Chrome can't do it! Kiwi browser experience

[[439911]] Which browser do you use on your phone...

Can we keep the “teeth” like the “elephant”?

Whether in the East or the West, the use of ivory...

2020 WeChat annual bill is online! Netizen: Am I so rich? ? ?

It's the end of the year again. Have you ever...

KOL Marketing: Why are couple KOLs more popular among advertisers?

Seemingly influenced by the advancement of the “s...

How to plan an event from 0 to 1?

As a planner and operator, online activities are ...

New media marketing strategy for B2B!

B2C companies have many ways to use social media,...

In-depth analysis: Xiaomi App Store ASO optimization guide!

The operations uncle learned about the Xiaomi App...

Oil spill, why donate hair?

In the long history of the evolution of life on E...

Microsoft finally fixes Windows 95 vulnerability

Microsoft released 16 security patches yesterday,...