SurfaceView is generally used for games.
[Java]
Package com. himi;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;
Import android. view. animation. Animation;
/**
* @ Author Himi
*/
Public class MySurfaceView extends SurfaceView implements Callback, Runnable {// Note 1
Private SurfaceHolder sfh;
Private Thread th;
Private Canvas canvas;
Private Paint paint;
Private int ScreenW, ScreenH;
Public MySurfaceView (Context context ){
Super (context );
Th = new Thread (this );
Sfh = this. getHolder ();
Sfh. addCallback (this); // Note 1
Paint = new Paint ();
Paint. setAntiAlias (true );
Paint. setColor (Color. RED );
This. setKeepScreenOn (true); // keep the screen always on
}
@ Override
Public void startAnimation (Animation animation ){
Super. startAnimation (animation );
}
Public void surfaceCreated (SurfaceHolder holder ){
ScreenW = this. getWidth (); // Note 2
ScreenH = this. getHeight ();
Th. start ();
}
Private void draw (){
Try {
Canvas = sfh. lockCanvas (); // get a canvas instance.
Canvas. drawColor (Color. WHITE); // screen Flushing
Canvas. drawText ("Himi", 100,100, paint); // draw text
Canvas. drawText ("this is a simple game framework", 100,130, paint );
Sfh. unlockCanvasAndPost (canvas); // submit the painted canvas
} Catch (Exception ex ){
} Finally {// Note 3
If (canvas! = Null)
Sfh. unlockCanvasAndPost (canvas );
}
}
Public void run (){
While (true ){
Draw ();
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
}