Android surfaceview learning example

Source: Internet
Author: User

Surfaceview is a subclass of view. It is used in the same way as any class derived from view. You can apply animations like other views and place them in the layout.
Surfaceview's surface can be drawn using all the standard canvas methods described earlier in this chapter. It also supports a full OpenGL ES library.
With OpenGL, you can draw any supported 2D or 3D objects on the surface. Compared with simulating the same effect on the 2D canvas, this method can rely on hardware acceleration (when available) to greatly improve the performance.

For displaying dynamic 3D images, for example, applications that use the Google Earth FunctionProgramSurfaceview is particularly useful for interactive games that provide immersive experience. It is also the best choice for real-time camera preview.


The obvious difference between surfaceview and view is:

1. A view inherited from surfaceview can start another thread, or update the view in the Child thread.

2. The surfaceview drawing method is executed in the Child thread, while the example drawing method of the View class is executed in the UI thread.

3. surfaceview must be used before drawingThe lockcanvas method is used to lock the canvas, obtain the canvas, and draw it on the canvas. After the painting is complete, use the unlockcanvasandpost method to unlock the canvas and display it on the screen.

The event processing rules of the surfaceview class are the same as those of the View class.


Example:

Activity

public class activity01 extends activity {<br/> gamesurfaceview mgamesurfaceview; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> mgamesurfaceview = new gamesurfaceview (this); <br/> setcontentview (mgamesurfaceview ); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> If (event. getaction () = motionevent. action_down) {<br/> mgamesurfaceview. X = event. getx (); <br/> mgamesurfaceview. y = event. gety (); <br/>}</P> <p> return true; <br/>}</P> <p> @ override <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back) {<br/> This. finish (); <br/>}</P> <p> return true; <br/>}
gamesurfaceview

Public class gamesurfaceview extends surfaceview implements surfaceholder. callback, runnable {</P> <p> Boolean mbloop = false; <br/> surfaceholder msurfaceholder = NULL; </P> <p> int COUNT = 0; <br/> float x = 50, y = 50; <br/> int screenwidth = 480, screenheight = 800; </P> <p> Public gamesurfaceview (context) {<br/> super (context); </P> <p> mbloop = true; </P> <p> msurfaceholder = This. getholder (); <br/> msurfaceholder. addcallback (this); <br/> This. setfocusable (true); <br/>}</P> <p> @ override <br/> Public void surfacecreated (surfaceholder holder) {<br/> New thread (this ). start (); // start paint thread <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, int height) {<br/> screenwidth = width; // reset width when screen orientation is changed <br/> screenheight = height; // reset height when screen orientation is changed <br/>}</P> <p> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> mbloop = false; <br/>}</P> <p> @ override <br/> Public void run () {<br/> while (mbloop) {<br/> synchronized (msurfaceholder) {<br/> ondraw (); <br/>}</P> <p> try {<br/> thread. sleep (200); <br/>} catch (exception E) {<br/>}</P> <p> Public void ondraw () {<br/> canvas = msurfaceholder. lockcanvas (); <br/> If (msurfaceholder = NULL | canvas = NULL) {<br/> return; <br/>}</P> <p> If (count <100) {<br/> count ++; <br/>}else {<br/> COUNT = 0; <br/>}</P> <p> paint mpaint = new paint (); <br/> mpaint. setantialias (true); <br/> mpaint. setcolor (color. cyan); <br/> canvas. drawrect (0, 0, screenwidth, screenheight, mpaint); // repaint background color <br/> switch (count % 4) {<br/> case 0: <br/> mpaint. setcolor (color. blue); <br/> break; <br/> case 1: <br/> mpaint. setcolor (color. green); <br/> break; <br/> case 2: <br/> mpaint. setcolor (color. red); <br/> break; <br/> case 3: <br/> mpaint. setcolor (color. yellow); <br/> break; <br/> default: <br/> mpaint. setcolor (color. white); <br/> break; <br/>}< br/> canvas. drawcircle (X, Y, 50, mpaint); <br/> msurfaceholder. unlockcanvasandpost (canvas); <br/>}< br/>}
Running effect:



Source code download





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.