Surfaceview plotting Mechanism

Source: Internet
Author: User
Tags call back

Although the previous view is used for plotting, the view plotting mechanism has two defects:

1. Lack of Dual-BUFFER MECHANISM

2. When updating an image, you must update the entire image on the view, which is inefficient.

Therefore, surfaceview is recommended for game plotting. It can only update the specified region to improve efficiency. The following is a simple example to demonstrate the use of surfaceview:

Activity:

Package COM. home. activity; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. OS. bundle; import android. view. motionevent; import android. view. surfaceholder; import android. view. surfaceholder. callback; import android. view. surfaceview; import android. view. view; import android. view. view. ontouchlistener; import COM. home. surfaceviewtest. r; public class surfaceviewactivity extends activity {// surfaceholder maintains the private surfaceholder; private paint; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); paint = new paint (); // obtain surfaceview instance surfaceview surface = (surfaceview) findviewbyid (R. id. main_sv); // initialize the surfaceholder object holder = surface. getholder (); holder. addcallback (New callback () {// call back this method when the surface is to be destroyed @ overridepublic void surfacedestroyed (surfaceholder holder) {} // call back this method when the surface is created @ overridepublic void surfacecreated (surfaceholder holder) {// lock the whole surfaceviewcanvas canvas = holder. lockcanvas (); // obtain the background resource Bitmap bitmap = bitmapfactory. decoderesource (surfaceviewactivity. this. getresources (), R. drawable. image2); // draw the canvas background. drawbitmap (bitmap, 0, 0, null); // after the painting is completed, release the canvas and submit the modification to holder. unlockcanvasandpost (canvas); // re-lock twice to prevent the next lockcanvas from blocking holder. lockcanvas (New rect (0, 0, 0, 0); holder. unlockcanvasandpost (canvas); holder. lockcanvas (New rect (0, 0, 0, 0); holder. unlockcanvasandpost (canvas);} // calls back this method when the format or size of a surface changes @ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}}); surface. setontouchlistener (New ontouchlistener () {@ overridepublic Boolean ontouch (view V, motionevent event) {// only process the pressed event if (event. getaction () = motionevent. action_down) {int Cx = (INT) event. getx (); int Cy = (INT) event. gety (); // lock the partial area of surfaceview, and only update the partial content canvas = holder. lockcanvas (New rect (CX-60, cy-60, cx + 60, Cy + 60); // Save the current status canvas of the canvas. save (); // rotate the canvas. rotate (30, CX, CY); paint. setcolor (color. red); // draw the red canvas. drawrect (CX-40, cy-40, CX, Cy, paint); // restore the Save status canvas before the canvas. restore (); paint. setcolor (color. green); // draw a Green Box canvas. drawrect (CX, Cy, cx + 40, Cy + 40, paint); // after painting, release the canvas and submit the modification to holder. unlockcanvasandpost (canvas);} return false ;}});}}

 

Layout XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <SurfaceView        android:id="@+id/main_sv"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>

 

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.