Android apidemos Study II: Android. Graphics. Camera

Source: Internet
Author: User

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.

Related Article

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.