To develop a game in android, SurfaceView must be used to develop a complex game.
After studying android for a while, I found the misunderstanding of Game Development in android. Don't always think about using Layout and view to implement it, do not make a game object into a component for processing. You should try to draw the background, characters, and animations of the game on the Canvas...
SurfaceView provides direct access to a graphic interface and controls the subview layer at the top of the interface. SurfaceView is provided for applications that need to directly draw pixels rather than using form components. An important concept and clue in the Android graphics system is surface. View and its subclass (such as TextView and Button) must be painted on the surface. Each surface creates a Canvas object (but its attributes are often changed) to manage the drawing operations of the view on the surface, such as drawing points.
Note that when using it, it usually appears at the top layer: the view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that wowould normally appear on top of it.
When using SurfaceView, it is usually required to be created, destroyed, and monitored during changes. This requires SurfaceHolder. Callback. class BBatt extends SurfaceView implements SurfaceHolder. Callback {
Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){}
// Check its name to understand its meaning, which is triggered when the surface size changes
Public void surfaceCreated (SurfaceHolder holder ){}
// Same as above. It is triggered during creation. Generally, the painting thread is called here.
Public void surfaceDestroyed (SurfaceHolder holder ){}
// Same as above. It is triggered when the image is destroyed. Generally, the painting thread is stopped and released here.
}
Example:
Public class BBatt extends SurfaceView implements SurfaceHolder. Callback, OnKeyListener {
Private BFairy bFairy;
Private DrawThread drawThread;
Public BBatt (Context context ){
Super (context );
This. setLayoutParams (new ViewGroup. LayoutParams (Global. battlefieldWidth, Global. battlefieldHeight ));
This. getHolder (). addCallback (this );
This. setFocusable (true );
This. setOnKeyListener (this );
BFairy = new BFairy (this. getContext ());
}
Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){
DrawThread = new DrawThread (holder );
DrawThread. start ();
}
Public void surfaceDestroyed (SurfaceHolder holder ){
If (drawThread! = Null ){
DrawThread. doStop ();
While (true) try {
DrawThread. join ();
Break;
} Catch (Exception ex ){}
}
}
Public boolean onKey (View view, int keyCode, KeyEvent event ){}
}
Example 2: Draw a blue rectangle with a thread.
Package com. g3.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 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
}
}
}
}
The underlying figure accessing SurfaceView is implemented through the SurfaceHolder interface. The SurfaceHolder object can be obtained through the getHolder () method. You should implement the surfaceCreated (SurfaceHolder) and surfaceDestroyed (SurfaceHolder) Methods to know when the Surface is created and destroyed during the display and hiding of the window.
SurfaceView can be accessed in multiple threads.
Note: A SurfaceView is only available between SurfaceHolder. Callback. surfaceCreated () and SurfaceHolder. Callback. surfaceDestroyed () calls, and its Canvas object (null) cannot be obtained at other times ).
My access process:
Create a subclass of SurfaceView to implement the SurfaceHolder. Callback interface.
Obtain the SurfaceHolder object of this SurfaceView.
Holder. addCallback (callback) is the class object that implements the SurfaceHolder. Callback interface.
After SurfaceHolder. Callback. surfaceCreated () is called, the holder. lockCanvas () object can get the Canvas object canvas corresponding to the SurfaceView object.
Draw images with a canvas object.
Call holder. unlockCanvasAndPost () to display the picture in the window.
SurfaceView can be accessed by multiple threads and painted in multiple threads.
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. util. Log;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Public class MySurfaceView extends SurfaceView implements SurfaceHolder. Callback {
Private Context mContext;
Private SurfaceHolder mHolder;
Public TouchScreenAdjusterSurfaceView (Context context ,){
Super (context );
MContext = context;
MHolder = TouchScreenAdjusterSurfaceView. this. getHolder ();
MHolder. addCallback (TouchScreenAdjusterSurfaceView. this );
This. setFocusableInTouchMode (true); // to make sure that we can get
// Touch events and key events, and
// "SetFocusable ()" to make sure we
// Can get key events
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){
// TODO Auto-generated method stub
}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// Now you can get the Canvas and draw something here
}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
Public void drawMyShape (PointPostion ps ){
MCanvas = mHolder. lockCanvas ();
// Draw anything you like
MHolder. unlockCanvasAndPost (mCanvas );
}
}
Author "Evil Valley"