SurfaceView learning in Android

Source: Internet
Author: User

The obvious difference between SurfaceView and View is that the Surface does not need to update the View through a thread, but you must use the lockCanvas method to lock the canvas before painting, and then draw the canvas, after that, use the unlockCanvasAndPost method to unlock the canvas. The SurfaceView class handles the same event as the View class.

First, let's look at a simple framework.

Drawing interface class:


[Java]
Package com. example. bonusball;
 
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. RectF;
Import android. util. AttributeSet;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
 
Public class CanvasView extends SurfaceView implements SurfaceHolder. Callback
{
Private SurfaceHolder myHolder;
Private Paint ballPaint; // Paint used to draw the cannonball
Private int screenWidth; // width of the screen
Private int screenHeight; // height of the screen
Private int ballRadius;
Private CanvasThread myThread;
// Control the loop
Private boolean isLoop;
 
Public CanvasView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
MyHolder = this. getHolder ();
MyHolder. addCallback (this );
BallPaint = new Paint ();
BallPaint. setColor (Color. BLUE );
IsLoop = true;
}
 
Public void fireBall (float startX, float startY)
{
System. out. println ("Fire ");
 
}
 
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
// TODO Auto-generated method stub
 
 
}
@ Override
Protected void onSizeChanged (int w, int h, int oldw, int oldh)
{
Super. onSizeChanged (w, h, oldw, oldh );
ScreenWidth = w; // store the width
ScreenHeight = h; // store the height
BallRadius = w/10;
}
 
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
MyThread = new CanvasThread ();
System. out. println ("SurfaceCreated! ");
MyThread. start ();
 
}
 
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
// Stop the loop
IsLoop = false;
}
Public void drawGameElements (Canvas canvas)
{
Canvas. drawCircle (100,100, ballRadius, ballPaint );
 
}
Private class CanvasThread extends Thread
{
@ Override
Public void run ()
{
While (true)
{
Synchronized (myHolder)
{
 
Canvas canvas = myHolder. lockCanvas (null); // get the Canvas
DrawGameElements (canvas );
MyHolder. unlockCanvasAndPost (canvas); // unlock the canvas and submit the painted image
// System. out. println ("run ");
}
}
}
 
}
}

Package com. example. bonusball;

Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. RectF;
Import android. util. AttributeSet;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;

Public class CanvasView extends SurfaceView implements SurfaceHolder. Callback
{
Private SurfaceHolder myHolder;
Private Paint ballPaint; // Paint used to draw the cannonball
Private int screenWidth; // width of the screen
Private int screenHeight; // height of the screen
Private int ballRadius;
Private CanvasThread myThread;
// Control the loop
Private boolean isLoop;

Public CanvasView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
MyHolder = this. getHolder ();
MyHolder. addCallback (this );
BallPaint = new Paint ();
BallPaint. setColor (Color. BLUE );
IsLoop = true;
}

Public void fireBall (float startX, float startY)
{
System. out. println ("Fire ");

}

@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
// TODO Auto-generated method stub


}
@ Override
Protected void onSizeChanged (int w, int h, int oldw, int oldh)
{
Super. onSizeChanged (w, h, oldw, oldh );
ScreenWidth = w; // store the width
ScreenHeight = h; // store the height
BallRadius = w/10;
}

@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
MyThread = new CanvasThread ();
System. out. println ("SurfaceCreated! ");
MyThread. start ();

}

@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
// Stop the loop
IsLoop = false;
}
Public void drawGameElements (Canvas canvas)
{
Canvas. drawCircle (100,100, ballRadius, ballPaint );

}
Private class CanvasThread extends Thread
{
@ Override
Public void run ()
{
While (true)
{
Synchronized (myHolder)
{

Canvas canvas = myHolder. lockCanvas (null); // get the Canvas
DrawGameElements (canvas );
MyHolder. unlockCanvasAndPost (canvas); // unlock the canvas and submit the painted image
// System. out. println ("run ");
}
}
}

}
}
Event processing class:


[Java]
Package com. example. bonusball;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. view. GestureDetector;
Import android. view. GestureDetector. SimpleOnGestureListener;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. widget. Toast;
Public class BallActivity extends Activity {
 
Private GestureDetector myGestureDetector; // listener gesture
Private CanvasView myCanvas;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MyCanvas = new CanvasView (this );
SetContentView (myCanvas );
MyGestureDetector = new GestureDetector (this, new MyGestureListener ());

}
@ Override
Public boolean onTouchEvent (MotionEvent event)
{
Return myGestureDetector. onTouchEvent (event );
 
}
 

Private class MyGestureListener extends SimpleOnGestureListener
{
Public boolean onDown (MotionEvent e1 ){
Toast. makeText (getApplicationContext (), "onDown", Toast. LENGTH_SHORT). show ();
Return true;
}

@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
System. out. println ("Fling ");
Return true;
}
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_ball, menu );
Return true;

}
}

Package com. example. bonusball;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. view. GestureDetector;
Import android. view. GestureDetector. SimpleOnGestureListener;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. widget. Toast;
Public class BallActivity extends Activity {

Private GestureDetector myGestureDetector; // listener gesture
Private CanvasView myCanvas;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MyCanvas = new CanvasView (this );
SetContentView (myCanvas );
MyGestureDetector = new GestureDetector (this, new MyGestureListener ());

}
@ Override
Public boolean onTouchEvent (MotionEvent event)
{
Return myGestureDetector. onTouchEvent (event );

}


Private class MyGestureListener extends SimpleOnGestureListener
{
Public boolean onDown (MotionEvent e1 ){
Toast. makeText (getApplicationContext (), "onDown", Toast. LENGTH_SHORT). show ();
Return true;
}

@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
System. out. println ("Fling ");
Return true;
}
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_ball, menu );
Return true;

}
}

Several concepts are explained:


Callback interface:

You only need to inherit the SurfaceView class and implement SurfaceHolder. the Callback interface can implement a custom SurfaceView, SurfaceHolder. callback notifies View and SurfaceHolder when the underlying Surface status changes. callback has the following interfaces:

SurfaceCreated (SurfaceHolder holder): This function is called immediately after the first Surface is created. The program can perform initialization work related to the drawing interface in this function. Generally, the interface is drawn in another thread, so do not draw the Surface in this function.
SurfaceChanged (SurfaceHolder holder, int format, int width, int height): This function is called when the Surface state (size and format) changes, this function will be called at least once after surfaceCreated is called.
SurfaceHolder class:

It is an interface used to control the surface. It provides an interface to control the surface size, format, and pixels above, that is, to monitor its changes.

The getHolder () function of SurfaceView can obtain the SurfaceHolder object, and the Surface is in the SurfaceHolder object. Although the Surface saves the pixel data of the current window, it does not directly deal with the Surface during use. The Canvas lockCanvas () of SurfaceHolder or Canvas lockCanvas () function to get the Canvas object, and modify the data in the Surface by drawing the content on the Canvas. If the Surface cannot be edited or you have not created it, calling this function will return null. The Surface content in unlockCanvas () and lockCanvas () is not cached, so you need to completely repaint the Surface content, to improve the efficiency, you can call the lockCanvas (Rect rect) function to specify a rect region, so that the content outside the region will be cached. After the lockCanvas function is called to obtain the Canvas, SurfaceView obtains a synchronization lock of the Surface until the unlockCanvasAndPost (Canvas canvas) function is called to release the lock, the synchronization mechanism here ensures that it will not be changed (destroyed or modified) during Surface painting ).

 

Finally, let's look at a complicated example that combines the previous gesture operations.

 


 

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.