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 ;}}}