Android game development-06-examples of three descriptive methods and methods

Source: Internet
Author: User
Tags drawtext

Method 1: Use the surfaceview class to describe

Public class mainactivity extends activity {mysurfaceview; thread mainloop; private mysurfaceview view; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); view = new mysurfaceview (this); super. setcontentview (View) ;}@ overrideprotected void ondestroy () {super. ondestroy (); thread. interrupted ();} class mysurfaceview extends surfaceview implements runnab Le {Bitmap bitmap; Boolean U, D, L, R; paint = new paint (); int left, top; long time; int Tx, Ty; public mysurfaceview (context) {super (context); // todo auto-generated constructor stubsetfocusable (true); requestfocus (); // request focus bitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. ic_launcher); paint. setcolor (color. red); Tx = bitmap. getwidth (); ty = bitmap. getheight ()/2; mainlo OP = new thread (this); mainloop. Start ();} void dodraw () {canvas = getholder (). lockcanvas (); If (canvas! = NULL) {canvas. drawcolor (color. white); If (u) Top --; If (d) Top ++; If (l) left --; If (r) left ++; canvas. drawbitmap (bitmap, left * 10, top * 10, paint); Long Now = system. currenttimemillis (); system. out. println (now-time); canvas. drawtext (1000f/(now-Time) + "FPS", TX, Ty, paint); time = now; getholder (). unlockcanvasandpost (canvas) ;}} public void run () {// todo auto-generated method stubwhile (true) {dodraw () ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = true; break; Case keyevent. keycode_dpad_down: D = true; break; Case keyevent. keycode_dpad_left: L = true; break; Case keyevent. keycode_dpad_right: r = true; break; default: Return false;} return true;} @ overridepublic Boolean onkeyup (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = false; break; Case keyevent. keycode_dpad_down: D = false; break; Case keyevent. keycode_dpad_left: L = false; break; Case keyevent. keycode_dpad_right: r = false; break; default: Return false;} return true ;}}}


Method 2: Use the View class to describe.

Public class mainactivity extends activity {mysurfaceview; thread mainloop; private mysurfaceview view; handler = new handler (); @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); view = new mysurfaceview (this); super. setcontentview (View) ;}@ overrideprotected void ondestroy () {super. ondestroy (); thread. interrupted ();} class mysurfaceview exte NDS view implements runnable {Bitmap bitmap; Boolean U, D, L, R; paint = new paint (); int left, top; long time; int Tx, Ty; public mysurfaceview (context) {super (context); // todo auto-generated constructor stubsetfocusable (true); requestfocus (); // request focus bitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. ic_launcher); paint. setcolor (color. red); Tx = bitmap. getwidth (); ty = bit Map. getheight ()/2; mainloop = new thread (this); mainloop. Start () ;}@ overrideprotected void ondraw (canvas) {If (canvas! = NULL) {canvas. drawcolor (color. white); If (u) Top --; If (d) Top ++; If (l) left --; If (r) left ++; canvas. drawbitmap (bitmap, left * 10, top * 10, paint); Long Now = system. currenttimemillis (); system. out. println (now-time); canvas. drawtext (1000f/(now-Time) + "FPS", TX, Ty, paint); time = now ;}} public void run () {// todo auto-generated method stubwhile (true) {handler. post (New runnable () {public void run () {// todo auto-generated method stubinvalidate (); // make the window invalid, meaning re-painting, ondraw method will be called}) ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = true; break; Case keyevent. keycode_dpad_down: D = true; break; Case keyevent. keycode_dpad_left: L = true; break; Case keyevent. keycode_dpad_right: r = true; break; default: Return false;} return true;} @ overridepublic Boolean onkeyup (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = false; break; Case keyevent. keycode_dpad_down: D = false; break; Case keyevent. keycode_dpad_left: L = false; break; Case keyevent. keycode_dpad_right: r = false; break; default: Return false;} return true ;}}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Method 3: Use the bitmapdrawable class to depict

Public class mainactivity extends activity {mysurfaceview; thread mainloop; private mysurfaceview view; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); view = new mysurfaceview (this); super. setcontentview (View) ;}@ overrideprotected void ondestroy () {super. ondestroy (); thread. interrupted ();} class mysurfaceview extends surfaceview implements runnab Le {bitmapdrawable BD; Bitmap bitmap; Boolean U, D, L, R; paint = new paint (); int left, top; long time; int Tx, Ty; int imagewidth, imageheight; Public mysurfaceview (context) {super (context); // todo auto-generated constructor stubsetfocusable (true); requestfocus (); // request focus bitmap = bitmapfactory. decoderesource (context. getresources (), R. drawable. ic_launcher); paint. setcolor (color. red); BD = new B Itmapdrawable (context. getresources (), bitmap); Tx = bitmap. getwidth (); ty = bitmap. getheight ()/2; imagewidth = bitmap. getwidth (); imageheight = bitmap. getheight (); mainloop = new thread (this); mainloop. start ();} public void dodraw () {canvas = getholder (). lockcanvas (); If (canvas! = NULL) {canvas. drawcolor (color. white); If (u) Top --; If (d) Top ++; If (l) left --; If (r) left ++; BD. draw (canvas); BD. setbounds (left * 10, top * 10, left * 10 + imagewidth, top * 10 + imageheight); Long Now = system. currenttimemillis (); system. out. println (now-time); canvas. drawtext (1000f/(now-Time) + "FPS", TX, Ty, paint); time = now; getholder (). unlockcanvasandpost (canvas) ;}} public void run () {// todo auto-generated method stubwhile (true) {dodraw () ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = true; break; Case keyevent. keycode_dpad_down: D = true; break; Case keyevent. keycode_dpad_left: L = true; break; Case keyevent. keycode_dpad_right: r = true; break; default: Return false;} return true;} @ overridepublic Boolean onkeyup (INT keycode, keyevent event) {Switch (keycode) {Case keyevent. keycode_dpad_up: U = false; break; Case keyevent. keycode_dpad_down: D = false; break; Case keyevent. keycode_dpad_left: L = false; break; Case keyevent. keycode_dpad_right: r = false; break; default: Return false;} return true ;}}}

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.