Using surfaceholder in Android to process surfaceview plotting

Source: Internet
Author: User

When surfaceview is used for game development, it is essential to use surfaceholder to process the effects and animations drawn on its canvas. Used to control the surface, size, pixels, and so on.
Abstract interface to someone holding a display surface. allows you to control the surface size and format,
Edit the pixels in the surface, and monitor changes to the surface. This interface is typically available
Through the surfaceview class.
Pay special attention to the following functions:
Abstract void addcallback (surfaceholder. Callback callback );
// Send a callback object to the current owner of surfaceview.
Abstract canvas lockcanvas ();
// Lock the canvas. After locking the canvas, you can use its returned canvas object to draw a picture on it.
Abstract canvas lockcanvas (rect dirty );
// Draw a picture in a certain area of the canvas. After the painting, the unlockcanvasandpost file is called to change the display content.
// For games with relatively high memory requirements, you do not need to redraw pixels in other regions outside dirty to increase the speed.
Abstract void unlockcanvasandpost (canvas );
// Stop the locked drawing and submit the changes.

Example:

Class drawthread extends thread {private surfaceholder holder; private Boolean running = true; protected drawthread (surfaceholder holder) {This. holder = holder;} protected void dostop () {running = false;} public void run () {canvas c = NULL; while (running) {c = holder. lockcanvas (null); // lock the entire canvas. We recommend that you do not set the parameter to null try {synchronized (holder) {bgrid when the memory requirement is high. drawgrid (c); // draw the grid bboom in the game. drawbooms ( C, booms); // draw the bomb bfairy in the game. drawfairy (c); // draw the main character in the game. // The content is in the Z axis, and the latter will overwrite the previous one. } Catch (exception ex) {}finally {holder. unlockcanvasandpost (c); // update the Screen Content }}}};

From http://blog.csdn.net/py890000/article/details/5439233

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.