This camera is not a camera in Android. the camera in the graphics package can be understood as the angle of view in the 2D graphics system, or the camera location. Based on the perspective principle, we can implement some simple 3D effects.
You can refer to the rotate3danimation example in apidemo.
In fact, camera has two methods: translate and rotate. Let's discuss them one by one.
The code is very simple. Just make a small custom view for testing. The size of the image I introduced is 480*600 (because the mobile phone is 480*854 ).
Protected void ondraw (canvas) {<br/> canvas. drawcolor (0 xffcccccc); </P> <p> // log. D (TAG, "bit W" + bitmap. getwidth () + ", H" + bitmap. getheight (); </P> <p> paint = new paint (); <br/> paint. setcolor (color. dkgray); <br/> paint. setstrokewidth (2.0f); <br/> paint. setstyle (paint. style. stroke); </P> <p> canvas. drawline (0,600,480,600, paint); <br/> for (INT I = 0; 50 * I <600; I ++) <br/> canvas. drawline (0, 50 * I, 480, 50 * I, paint); <br/> for (INT I = 0; 50 * I <480; I ++) <br/> canvas. drawline (50 * I, 0, 50 * I, 600, paint); </P> <p> matrix = new matrix (); </P> <p> camera C = new camera (); <br/> C. save (); </P> <p> // The forward rotation of the location causes the image to flip to the screen <br/> // but rotate around the Z axis, the image plane is rotated (2d effect) <br/> C. rotatez (45); </P> <p> // X causes the image to move to the right 50 (forward to right) <br/> // y causes the image to move up 50 (forward to face up) <br/> // Z causes the image to be zoomed out (pointing to itself in the forward direction) <br/> C. translate (100f, 0f, 300f); <br/> C. getmatrix (matrix); <br/> // C. applytocanvas (canvas); </P> <p> C. restore (); </P> <p> // move to the center <br/> matrix. pretranslate (-240,-300); <br/> matrix. posttranslate (240,300); </P> <p> canvas. concat (matrix); </P> <p> canvas. drawbitmap (bitmap, 0, 0, null); <br/> // canvas. drawbitmap (bitmap, matrix, null); </P> <p >}< br/>}
The experiment shows that:
Translate (x, y, z), which is easy to understand. For more information, see comments in the code. Does the coordinate axis seem different from our conventional understanding? Why?
Rotatex, rotatey, and rotatez are also easy to understand. The effect of location movement on images is reversed.
The last note is that there are two ways to apply the modification of this location, and the effect is different. You can try it yourself.