Android surfaceview game development example

Source: Internet
Author: User

When we need to develop a complex gameProgramWhen the execution efficiency requirement is very high, the view class cannot meet the requirements. In this case, the surfaceview class must be used for development.

For example, when a game requires a high speed, the view class cannot meet the demand. In this case, you must use the surfaceview class for development. For example, a game with high speed requirements can be displayed using dual buffering. The background, characters, and animations in the game must be drawn on a canvas, while surfaceview can directly access a canvas, surfaceview is provided for applications that need to directly draw pixels rather than using form components. Each surface creates a canvas object (but its attributes are often changed) to manage the drawing operations on the view and surface.

Example:

Activity

Public class activity01 extends activity {</P> <p> gamesurfaceview gameview = NULL; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> gameview = new gamesurfaceview (this); <br/> setcontentview (gameview); <br/>}< br/>}

Gamesurfaceview

Public class gamesurfaceview extends surfaceview implements surfaceholder. callback, runnable {</P> <p> private activity01 mactivity = NULL; </P> <p> private surfaceholder mholder = NULL; <br/> private thread mthread = NULL; <br/> private Boolean mloop = false; </P> <p> private canvas mcanvas = NULL; <br/> private paint mpaint = NULL; <br/> private long drawstarttime = 0, drawcosttime = 0, drawtotaltime = 30; </ P> <p> private float X, Y; <br/> private int screenwidth = 480, screenheight = 800; </P> <p> Public gamesurfaceview (context) {<br/> super (context); </P> <p> mactivity = (activity01) context; </P> <p> mholder = This. getholder (); <br/> mholder. addcallback (this); <br/> setfocusable (true); <br/>}</P> <p> @ override <br/> Public void surfacecreated (surfaceholder holder) {<br/> mpaint = new paint (); <br/> mpaint. se Tcolor (color. cyan); <br/> mloop = true; </P> <p> New thread (this ). start (); <br/>}</P> <p> @ override <br/> Public void surfacechanged (surfaceholder holder, int format, int width, int height) {<br/> screenwidth = width; <br/> screenheight = height; </P> <p> X = screenwidth/2; <br/> Y = screenheight/2; <br/>}</P> <p> @ override <br/> Public void surfacedestroyed (surfaceholder holder) {<br/> mloop = false; <br/>} </P> <p> @ override <br/> Public void run () {<br/> while (mloop) {<br/> drawstarttime = systemclock. uptimemillis (); </P> <p> synchronized (mholder) {<br/> try {<br/> mcanvas = mholder. lockcanvas (); <br/> ondraw (); // repaint <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (mcanvas! = NULL) {<br/> mholder. unlockcanvasandpost (mcanvas); <br/>}</P> <p> drawcosttime = systemclock. uptimemillis ()-drawstarttime; <br/> log. I ("drawcosttime", "drawcosttime =" + drawcosttime); <br/> try {<br/> If (drawcosttime <drawtotaltime) {<br/> thread. sleep (drawtotaltime-drawcosttime); // sleep elapse time <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> private void ondraw () {<br/> If (mholder = NULL & mpaint = NULL) {<br/> return; <br/>}</P> <p> mpaint. setantialias (true); <br/> mpaint. setalpha (100); </P> <p> mpaint. setcolor (color. cyan); // paint background color <br/> mcanvas. drawrect (0, 0, screenwidth, screenheight, mpaint); </P> <p> mpaint. setcolor (color. blue); // paint foreground color <br/> mcanvas. drawcircle (X, Y, 50, mpaint); <br/>}</P> <p> @ override <br/> Public Boolean ontouchevent (motionevent event) {<br/> super. ontouchevent (event); </P> <p> switch (event. getaction () {<br/> case motionevent. action_down: <br/> This. X = event. getx (); <br/> This. y = event. gety (); <br/> break; </P> <p> case motionevent. action_move: <br/> This. X = event. getx (); <br/> This. y = event. gety (); <br/> break; </P> <p> case motionevent. action_up: <br/> This. X = event. getx (); <br/> This. y = event. gety (); <br/> break; </P> <p> default: <br/> This. X = event. getx (); <br/> This. y = event. gety (); <br/> break; <br/>}</P> <p> return true; // true then event can be implemented again <br/>}</P> <p> @ override <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> super. onkeydown (keycode, event); </P> <p> If (keycode = keyevent. action_down) {<br/> This. mactivity. finish (); <br/>}</P> <p> return true; <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.