Android game development-detailed explanation of animation SurfaceView

Source: Internet
Author: User

SurfaceView plays an important role in game development. Let's have a good look at the information today. With Examples written by yourself.
It is enough to use View when writing a picture that is not changing fast. If you use View to write a picture that is changing too fast, the screen may flash. When writing games such as plants vs. Zombies and Fruit Ninja, you cannot use View to meet your requirements. Android provides SurfaceView, which is used for animation and a subclass of View.
SurfaceView comes with two-level cache. When writing a game with a fast change, the second-level cache will make the changes in the game picture more coherent. The role of level 2 cache is to put the image to be drawn into the memory in advance.
1. The most basic SurfaceView can be implemented in just four steps. Directly go to the code and view the comments in the code in detail.
[Java]
Public class GameView extends SurfaceView
{
SurfaceHolder surfaceHolder;
 
Public GameView (Context context)
{
Super (context );
// 1. The SurfaceView is SurfaceHolder. surfaceHolder is equivalent to a remote control to control SurfaceView.
SurfaceHolder = this. getHolder ();
// This is the callback method, which must exist. Otherwise, a null pointer exception is reported. This means that the surface is created, destroyed, and changed.
SurfaceHolder. addCallback (new Callback ()
{
 
@ Override
Public void surfaceDestroyed (SurfaceHolder holder)
{
 
}
 
@ Override
Public void surfaceCreated (SurfaceHolder holder)
{
// 2. Lock the canvas after the surface is created
Canvas canvas = surfaceHolder. lockCanvas ();
// 3. Any painting operation can be performed on the canvas (below is a red line)
Paint paint = new Paint ();
Paint. setColor (Color. RED );
Canvas. drawLine (0, 0,100,100, paint );
// 4. Unlock the canvas and display it on the screen
SurfaceHolder. unlockCanvasAndPost (canvas );
}
 
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format,
Int width, int height)
{
 
}
});
 
}

Of course, this writing is obviously not the best. Only SurfaceView obviously cannot implement animation. Of course, it must be combined with the thread. Use a thread to screen.
2. Use SurfaceView to write the animation in general. Directly go to the Code. For more information, see the comments in the code.

[Java]
Public class GameViewOK extends SurfaceView implements Callback, Runnable
{
SurfaceHolder surfaceHolder;
Private boolean isThreadRunning = true;
Canvas canvas;
Float r = 10;
 
Public GameViewOK (Context context)
{
Super (context );
SurfaceHolder = this. getHolder ();
SurfaceHolder. addCallback (this); // registers the callback Method
}
 
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height)
{
 
}
 
@ Override
Public void surfaceCreated (SurfaceHolder holder)
{
// Start the thread when creating surfaceView
New Thread (this). start ();
}
 
@ Override
Public void surfaceDestroyed (SurfaceHolder holder)
{
// When surfaceView is destroyed, stop the running of the thread to avoid an error when surfaceView is still running.
IsThreadRunning = false;
// The third method prevents exceptions during exit. When the surfaceView is destroyed, the thread is suspended for 300 ms. When you wake up and execute the run () method, isThreadRunning is false.
Try
{
Thread. sleep (300 );
} Catch (InterruptedException e)
{
E. printStackTrace ();
}
}
 
/**
* Write the drawing method into this method separately.
*/
Private void drawVieW ()
{
Try
{// First method to prevent exceptions during exit: When isThreadRunning is set to false, the drawView method will be executed again, but surfaceView has been destroyed.
// Determine surfaceHolder
If (surfaceHolder! = Null)
{
// 1. Lock the canvas after the surface is created
Canvas = surfaceHolder. lockCanvas ();
// 2. Any painting operation can be performed on the canvas (below is a red line)
Paint paint = new Paint ();
Paint. setColor (Color. BLUE );
// Paint. setStyle (Style. STROKE); // only the border
Paint. setStrokeWidth (5 );
Canvas. drawCircle (100,100, r ++, paint );
}
} Catch (Exception e)
{
E. printStackTrace ();
} Finally
{
// Canvas is obtained based on surfaceHolder. The last surfaceView has been destroyed, and the canvas does not exist.
If (canvas! = Null)
// 3. Unlock the canvas and display it on the screen
SurfaceHolder. unlockCanvasAndPost (canvas );
}
 
}
 
@ Override
Public void run ()
{
// Refresh the screen every 100ms
While (isThreadRunning)
{
DrawVieW ();
Try
{
Thread. sleep (100 );
} Catch (Exception e)
{
E. printStackTrace ();
}
}
}
 
/*
* This is the second method to solve the problem of exit error. When you press the return key, set isThreadRunning to false in advance to end the thread.
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event)
{
If (keyCode = KeyEvent. KEYCODE_BACK)
{
IsThreadRunning = false;
}
Return super. onKeyDown (keyCode, event );
}
*/
}

Running Effect: an increasing circle

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.