Note: we have been developing games on the j2's platform for a long time. I want to switch to android this time. I think it's okay to think about it. So I wrote this article.
Let's talk about the source code.
Package Sur. Face. Pro;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. util. AttributeSet;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. SurfaceHolder. Callback;
Public class MySurfaceView extends SurfaceView implements Callback, Runnable {
Public SurfaceHolder sfh;
Public Canvas canvas;
Public Paint paint;
Public static int SC _W, SC _H;
Public MySurfaceView (Context context, AttributeSet attrs ){
Super (context, attrs );
Sfh = getHolder ();
Sfh. addCallback (this); // binding surface to surface view
Paint = new Paint ();
Paint. setAntiAlias (true );
Paint. setColor (Color. RED );
This. setKeepScreenOn (true); // keep screen light
}
Public void run (){
Int count = 0;
While (count ++ <20 ){
Logic ();
Paint ();
Sleep ();
}
}
Private void logic (){
// Do your logic
}
Public void sleep (){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
Public void paint (){
Canvas = sfh. lockCanvas (); // obtain the class canvas. Other operations are the same as those of j2s. // do your paint
Sfh. unlockCanvasAndPost (canvas );
}
Public void surfaceCreated (SurfaceHolder holder ){
SC _W = getWidth ();
SC _H = getHeight ();
New Thread (this). start ();
}
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}
Public void surfaceDestroyed (SurfaceHolder holder ){
}
}