Surfaceview plays an important role in game development. It has a greater degree of freedom for Screen Control (unlike view update with handler, about view ), however, there are not many references in this regard. The examples that can be found are a bit overwhelming and cannot be clearly displayed. The following is a simple example, strive to clearly display the process, while others can be simplified.
Program effect: Draw a blue rectangle with a thread.
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://rayleung.javaeye.com/blog/420410 ">
- Package com. Ray. test;
- /*
- * Surfaceview sample program
- * Demonstrate the process
- */
- Import Android. App. activity;
- Import Android. content. context;
- Import Android. Graphics. Canvas;
- Import Android. Graphics. color;
- Import Android. Graphics. paint;
- Import Android. Graphics. rectf;
- Import Android. OS. Bundle;
- Import Android. View. surfaceholder;
- Import Android. View. surfaceview;
- Public class test extends activity {
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (New myview (this ));
- }
- // Internal class
- Class myview extends surfaceview implements surfaceholder. Callback {
- Surfaceholder holder;
- Public myview (context ){
- Super (context );
- Holder = This. getholder (); // Get holder
- Holder. addcallback (this );
- // Setfocusable (true );
- }
- @ Override
- Public void surfacechanged (surfaceholder holder, int format, int width,
- Int height ){
- }
- @ Override
- Public void surfacecreated (surfaceholder holder ){
- New thread (New mythread (). Start ();
- }
- @ Override
- Public void surfacedestroyed (surfaceholder holder ){
- }
- // Internal class of the internal class
- Class mythread implements runnable {
- @ Override
- Public void run (){
- Canvas canvas = holder. lockcanvas (null); // get the canvas
- Paint mpaint = new paint ();
- Mpaint. setcolor (color. Blue );
- Canvas. drawrect (New rectf (40, 60, 80), mpaint );
- Holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image
- }
- }
- }
- }
Package COM. ray. test;/** the surfaceview sample program * demonstrates the process */import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectf; import android. OS. bundle; import android. view. surfaceholder; import android. view. surfaceview; public class test extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (New myview (this);} // internal class myview extends surfaceview implements surfaceholder. callback {surfaceholder holder; Public myview (context) {super (context); holder = This. getholder (); // get holderholder. addcallback (this); // setfocusable (true) ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacecreated (surfaceholder holder) {New thread (New mythread ()). start () ;}@ overridepublic void surfacedestroyed (surfaceholder holder) {}// internal class mythread implements runnable {@ overridepublic void run () {canvas = holder. lockcanvas (null); // obtain the canvas paint mpaint = new paint (); mpaint. setcolor (color. blue); canvas. drawrect (New rectf (40, 60, 80), mpaint); holder. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image }}}}
- Testsurfaceview0.1.rar (25.6 KB)