I. Basic Knowledge:
SurfaceView is inherited from the View. The View is responsible for updating the animation in the main thread, while the SurfaceView is used to update the animation in a new thread.
Main Methods of SurfaceView class:
// Called when SurfaceView is created
Pubilic abstract void surfaceCreated (SurfaceHolder holder)
// Called when SurfaceView changes
Pubilic abstract void surfaceChanged (SurfaceHolder holder, int format, int width, int height)
// Called when SurfaceView is destroyed
Pubilic abstract void surfaceDestroyed (SurfaceHolder holder)
// Draw a SurfaceView Image
Protected void onDraw (Canvas canvas)
(The canvas parameter is the paint brush of the SurfaceView. This method is called for every screen change in the SurfaceView)
II. Implementation results:
First, a pair of images start to move from the lower left corner of the screen to the upper right corner. When the image and the cell phone screen collide, the horizontal speed and direction of the image remain unchanged, and the vertical speed remains unchanged,
In the opposite direction; after the current collision, the same effect will remain until the image is flying out of the screen. Then, the screen gradually displays an image.
Iii. Programming implementation:
1. Edit the interface (reslayoutmain. xml ):
[Java]
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
2. Edit the Code:
(SrcwyfzclMyActivity. java)
[Java]
Package wyf. zcl;
/*
* This example demonstrates how to draw a simple scenario in surfaceView.
* MyActivity. java is the main Activity of the program.
* MySurfaceView. java is the SurfaceView class of the program.
* Constant. java Constant class, which writes all constants in this class
* The OnDrawThread. java class is used to refresh the onDraw from time to time and repaint the image.
* PicRunThread. java: This class controls the motion of duke images.
**/
Import android. app. Activity; // introduce related packages
Import android. content. pm. ActivityInfo; // introduce the related package
Import android. OS. Bundle; // introduce related packages
Import android. view. Window; // introduce related packages
Import android. view. WindowManager; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private MySurfaceView msv; // get surfaceView reference
@ Override
Public void onCreate (Bundle savedInstanceState) {// Life Cycle Function of the Activity, called during Program Creation
Super. onCreate (savedInstanceState );
Msv = new MySurfaceView (MyActivity. this); // instantiate the object of MySurfaceView
RequestWindowFeature (Window. FEATURE_NO_TITLE); // you can specify whether the title bar is displayed on the screen.
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); // set full screen
// Set to allow only landscape screens
This. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
SetContentView (msv); // set the content displayed in the Activity to msv
}
}
Package wyf. zcl;
/*
* This example demonstrates how to draw a simple scenario in surfaceView.
* MyActivity. java is the main Activity of the program.
* MySurfaceView. java is the SurfaceView class of the program.
* Constant. java Constant class, which writes all constants in this class
* The OnDrawThread. java class is used to refresh the onDraw from time to time and repaint the image.
* PicRunThread. java: This class controls the motion of duke images.
**/
Import android. app. Activity; // introduce related packages
Import android. content. pm. ActivityInfo; // introduce the related package
Import android. OS. Bundle; // introduce related packages
Import android. view. Window; // introduce related packages
Import android. view. WindowManager; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private MySurfaceView msv; // get surfaceView reference
@ Override
Public void onCreate (Bundle savedInstanceState) {// Life Cycle Function of the Activity, called during Program Creation
Super. onCreate (savedInstanceState );
Msv = new MySurfaceView (MyActivity. this); // instantiate the object of MySurfaceView
RequestWindowFeature (Window. FEATURE_NO_TITLE); // you can specify whether the title bar is displayed on the screen.
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN); // set full screen
// Set to allow only landscape screens
This. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
SetContentView (msv); // set the content displayed in the Activity to msv
}
}
(SrcwyfzclConstant. java)
[Java]
Package wyf. zcl;
Import android. view. Display;
// Constant. java Constant class, which writes all constants in this class
Public class Constant {
Public static int SCREENWIDTH = 480; // screen width (this program is a horizontal screen)
Public static int SCREENHEIGHT = 320; // screen height
Public static int PICWIDTH = 64; // The image width.
Public static int PICHEIGHT = 64; // Image Height
Public static int ONDRAWSPEED = 30; // draw interval of the onDraw Thread class
Public static float PICXSPEED = 1.5f; // horizontal movement speed of the image
Public static float PICYSPEED = 2; // vertical image movement speed
Public static int PICRUNSPEED = 30; // refresh speed of the motion thread of the image
Public static int PICALPHASPEED = 20; // The image fades in to demonstrate the refreshing speed.
}
Package wyf. zcl;
Import android. view. Display;
// Constant. java Constant class, which writes all constants in this class
Public class Constant {
Public static int SCREENWIDTH = 480; // screen width (this program is a horizontal screen)
Public static int SCREENHEIGHT = 320; // screen height
Public static int PICWIDTH = 64; // The image width.
Public static int PICHEIGHT = 64; // Image Height
Public static int ONDRAWSPEED = 30; // draw interval of the onDraw Thread class
Public static float PICXSPEED = 1.5f; // horizontal movement speed of the image
Public static float PICYSPEED = 2; // vertical image movement speed
Public static int PICRUNSPEED = 30; // refresh speed of the motion thread of the image
Public static int PICALPHASPEED = 20; // The image fades in to demonstrate the refreshing speed.
}
(SrcwyfzclMySurfaceView. java)
[Java]
Package wyf. zcl;
Import android. content. Context; // introduce related packages
Import android. graphics. Bitmap; // introduce related packages
Import android. graphics. BitmapFactory; // introduce related packages
Import android. graphics. Canvas; // introduce related packages
Import android. graphics. Color; // introduce related packages
Import android. graphics. Paint; // introduce related packages
Import android. view. Display; // introduce related packages
Import android. view. SurfaceHolder; // introduce related packages
Import android. view. SurfaceView; // introduce related packages
Public class MySurfaceView extends SurfaceView
Implements SurfaceHolder. Callback {
// SurfaceHolder. Callback interface is implemented here to add the lifecycle Callback function for surfaceView
Int dy = Display. DEFAULT_DISPLAY;
MyActivity ma; // get reference of MyActivity
Paint paint; // Paint brush reference
OnDrawThread odt; // OnDrawThread class reference
PicRunThread prt; // Thread class reference for Image Motion
Private float picX = 0; // x coordinate of the image
Private float picY = 0; // y coordinate of the image
Boolean picAlphaFlag = false; // indicates the dimmed image. If the value is false, the image is not displayed. If the value is true, the image is displayed.
Int picAlphaNum = 0; // The alpha value of the paint brush in the dimmed image.
Public MySurfaceView (Context context ){
Super (context );
This. ma = (MyActivity) context;
// Point the reference of ma to the object that calls the Surfaceview constructor method. In this example, MyActivity
This. getHolder (). addCallback (this); // registers the callback Interface
Paint = new Paint (); // instantiate the paint brush
Odt = new OnDrawThread (this); // instantiate the OnDrawThread class
Prt = new PicRunThread (this); // instantiate the PicRunThread class
Prt. start ();
}
Public void setPicX (float picX) {// set the image x coordinate
This. picX = picX;
}
Public void setPicY (float picY) {// set the y coordinate of the image
This. picY = picY;
}
Public void setPicAlphaNum (int picAlphaNum) {// sets the alpha parameter for image darkening.
This. picAlphaNum = picAlphaNum;
}
@ Override
Protected void onDraw (Canvas canvas) {// onDraw method, which is used to draw images and images.
Super. onDraw (canvas );
Paint. setColor (Color. WHITE); // set the paint brush to WHITE.
Canvas. drawRect (0, 0, Constant. SCREENWIDTH, Constant. SCREENHEIGHT, paint );
// A white full-screen rectangle is drawn to set the background to white and clear the background each time the screen is re-painted.
// Perform a flat map
Bitmap bitmapDuke = BitmapFactory. decodeResource (ma. getResources (), R. drawable. duke );
Canvas. drawBitmap (bitmapDuke, picX, picY, paint );
// Fades in the image
If (picAlphaFlag ){
Bitmap bitmapBG = BitmapFactory. decodeResource (ma. getResources (), R.drawable.jpg 1 );
Paint. setAlpha (picAlphaNum );
Canvas. drawBitmap (bitmapBG, 0, 0, paint );
}
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height) {// this method is called when surfaceView changes, such as the screen size.
}
@ Override
Public void surfaceCreated (SurfaceHolder holder) {// this method is called when surfaceView is created
Odt. start (); // start the onDraw draw thread
}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder) {// this method is called before the surfaceView is destroyed.
}
}
Package wyf. zcl;
Import android. content. Context; // introduce related packages
Import android. graphics. Bitmap; // introduce related packages
Import android. graphics. BitmapFactory; // introduce related packages
Import android. graphics. Canvas; // introduce related packages
Import android. graphics. Color; // introduce related packages
Import android. graphics. Paint; // introduce related packages
Import android. view. Display; // introduce related packages
Import android. view. SurfaceHolder; // introduce related packages
Import android. view. SurfaceView; // introduce related packages
Public class MySurfaceView extends SurfaceView
Implements SurfaceHolder. Callback {
// SurfaceHolder. Callback interface is implemented here to add the lifecycle Callback function for surfaceView
Int dy = Display. DEFAULT_DISPLAY;
MyActivity ma; // get reference of MyActivity
Paint paint; // Paint brush reference
OnDrawThread odt; // OnDrawThread class reference
PicRunThread prt; // Thread class reference for Image Motion
Private float picX = 0; // x coordinate of the image
Private float picY = 0; // y coordinate of the image
Boolean picAlphaFlag = false; // indicates the dimmed image. If the value is false, the image is not displayed. If the value is true, the image is displayed.
Int picAlphaNum = 0; // The alpha value of the paint brush in the dimmed image.
Public MySurfaceView (Context context ){
Super (context );
This. ma = (MyActivity) context;
// Point the reference of ma to the object that calls the Surfaceview constructor method. In this example, MyActivity
This. getHolder (). addCallback (this); // registers the callback Interface
Paint = new Paint (); // instantiate the paint brush
Odt = new OnDrawThread (this); // instantiate the OnDrawThread class
Prt = new PicRunThread (this); // instantiate the PicRunThread class
Prt. start ();
}
Public void setPicX (float picX) {// set the image x coordinate
This. picX = picX;
}
Public void setPicY (float picY) {// set the y coordinate of the image
This. picY = picY;
}
Public void setPicAlphaNum (int picAlphaNum) {// sets the alpha parameter for image darkening.
This. picAlphaNum = picAlphaNum;
}
@ Override
Protected void onDraw (Canvas canvas) {// onDraw method, which is used to draw images and images.
Super. onDraw (canvas );
Paint. setColor (Color. WHITE); // set the paint brush to WHITE.
Canvas. drawRect (0, 0, Constant. SCREENWIDTH, Constant. SCREENHEIGHT, paint );
// A white full-screen rectangle is drawn to set the background to white and clear the background each time the screen is re-painted.
// Perform a flat map
Bitmap bitmapDuke = BitmapFactory. decodeResource (ma. getResources (), R. drawable. duke );
Canvas. drawBitmap (bitmapDuke, picX, picY, paint );
// Fades in the image
If (picAlphaFlag ){
Bitmap bitmapBG = BitmapFactory. decodeResource (ma. getResources (), R.drawable.jpg 1 );
Paint. setAlpha (picAlphaNum );
Canvas. drawBitmap (bitmapBG, 0, 0, paint );
}
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height) {// this method is called when surfaceView changes, such as the screen size.
}
@ Override
Public void surfaceCreated (SurfaceHolder holder) {// this method is called when surfaceView is created
Odt. start (); // start the onDraw draw thread
}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder) {// this method is called before the surfaceView is destroyed.
}
}
(SrcwyfzclOnDrawThread. java)
[Java]
Package wyf. zcl;
Import android. graphics. Canvas; // introduce related packages
Import android. view. SurfaceHolder; // introduce related packages
// This class is used to refresh the onDraw from time to time and redraw the screen.
Public class OnDrawThread extends Thread {
MySurfaceView msv; // get the reference of MySurfaceView
SurfaceHolder sh; // SurfaceHolder reference
Public OnDrawThread (MySurfaceView msv ){
Super ();
This. msv = msv; // In the constructor, point the msv reference to the object that calls the MySurfaceView class.
Sh = msv. getHolder ();
}
@ Override
Public void run (){
Super. run ();
Canvas canvas = null; // Canvas reference
While (true ){
Try {
Canvas = sh. lockCanvas (null); // point the canvas reference to the canvas object of surfaceView.
Synchronized (this. sh) {// The Painting Process may cause synchronization problems, locking
If (canvas! = Null ){
Msv. onDraw (canvas );
}
}
} Finally {
Try {
If (sh! = Null ){
Sh. unlockCanvasAndPost (canvas); // unlock after painting
}
} Catch (Exception e) {e. printStackTrace ();}
}
Try {
Thread. sleep (Constant. ONDRAWSPEED); // rest for 1 second
} Catch (Exception e) {e. printStackTrace ();}
}
}
}
Package wyf. zcl;
Import android. graphics. Canvas; // introduce related packages
Import android. view. SurfaceHolder; // introduce related packages
// This class is used to refresh the onDraw from time to time and redraw the screen.
Public class OnDrawThread extends Thread {
MySurfaceView msv; // get the reference of MySurfaceView
SurfaceHolder sh; // SurfaceHolder reference
Public OnDrawThread (MySurfaceView msv ){
Super ();
This. msv = msv; // In the constructor, point the msv reference to the object that calls the MySurfaceView class.
Sh = msv. getHolder ();
}
@ Override
Public void run (){
Super. run ();
Canvas canvas = null; // Canvas reference
While (true ){
Try {
Canvas = sh. lockCanvas (null); // point the canvas reference to the canvas object of surfaceView.
Synchronized (this. sh) {// The Painting Process may cause synchronization problems, locking
If (canvas! = Null ){
Msv. onDraw (canvas );
}
}
} Finally {
Try {
If (sh! = Null ){
Sh. unlockCanvasAndPost (canvas); // unlock after painting
}
} Catch (Exception e) {e. printStackTrace ();}
}
Try {
Thread. sleep (Constant. ONDRAWSPEED); // rest for 1 second
} Catch (Exception e) {e. printStackTrace ();}
}
}
}
(SrcwyfzclPicRunThread. java)
[Java]
Package wyf. zcl;
// This class controls the motion of duke Images
Public class PicRunThread extends Thread {
MySurfaceView msv; // reference of MySurfaceView
Private float picX = 0; // x coordinate of the image
Private float picY = Constant. SCREENHEIGHT-Constant.PICHEIGHT; // picture y coordinate
Boolean yRunFlag = false; // The motion mark in the y direction. If it is false, y = y + speed. If it is true, y = y-speed.
Int picAlphaNum = 0; // The alpha value of the paint brush in the dimmed image.
Public PicRunThread (MySurfaceView msv ){
Super ();
This. msv = msv; // point the reference of this thread class to the object that calls its MySurfaceView
}
@ Override
Public void run (){
Super. run ();
While (true ){
// Control the motion of the duke Image
While (this. picX
Msv. setPicX (picX );
Msv. setPicY (picY );
PicX = picX + Constant. PICXSPEED;
If (yRunFlag) {// It should be upward, auto-minus
PicY = picY-Constant.PICYSPEED;
} Else {// downward movement, auto-Increment
PicY = picY + Constant. PICYSPEED;
}
If (picY <= 0) {// arrive at the top of the screen
YRunFlag = false;
} Else if (picY> Constant. SCREENHEIGHT-Constant.PICHEIGHT) {// arrive at the bottom of the screen
YRunFlag = true;
}
Try {
Thread. sleep (Constant. PICRUNSPEED );
} Catch (Exception e) {e. printStackTrace ();}
}
// Demo of the dimmed Image
Msv. picAlphaFlag = true; // enable the dimmed image.
For (picAlphaNum = 0; picAlphaNum <= 255; picAlphaNum ++ ){
If (picAlphaNum = 255 ){
Msv. picAlphaFlag = false; // when the image fades out, mark the reset
PicX = 0; // x coordinate of the image
PicY = Constant. SCREENHEIGHT-Constant.PICHEIGHT; // picture y coordinate
System. out. println (msv. picAlphaFlag + "picX:" + picX + "picY:" + picY );
}
Msv. setPicAlphaNum (picAlphaNum );
Try {
Thread. sleep (Constant. PICALPHASPEED );
} Catch (Exception e) {e. printStackTrace ();}
}
}
}
}
Package wyf. zcl;
// This class controls the motion of duke Images
Public class PicRunThread extends Thread {
MySurfaceView msv; // reference of MySurfaceView
Private float picX = 0; // x coordinate of the image
Private float picY = Constant. SCREENHEIGHT-Constant.PICHEIGHT; // picture y coordinate
Boolean yRunFlag = false; // The motion mark in the y direction. If it is false, y = y + speed. If it is true, y = y-speed.
Int picAlphaNum = 0; // The alpha value of the paint brush in the dimmed image.
Public PicRunThread (MySurfaceView msv ){
Super ();
This. msv = msv; // point the reference of this thread class to the object that calls its MySurfaceView
}
@ Override
Public void run (){
Super. run ();
While (true ){
// Control the motion of the duke Image
While (this. picX
Msv. setPicX (picX );
Msv. setPicY (picY );
PicX = picX + Constant. PICXSPEED;
If (yRunFlag) {// It should be upward, auto-minus
PicY = picY-Constant.PICYSPEED;
} Else {// downward movement, auto-Increment
PicY = picY + Constant. PICYSPEED;
}
If (picY <= 0) {// arrive at the top of the screen
YRunFlag = false;
} Else if (picY> Constant. SCREENHEIGHT-Constant.PICHEIGHT) {// arrive at the bottom of the screen
YRunFlag = true;
}
Try {
Thread. sleep (Constant. PICRUNSPEED );
} Catch (Exception e) {e. printStackTrace ();}
}
// Demo of the dimmed Image
Msv. picAlphaFlag = true; // enable the dimmed image.
For (picAlphaNum = 0; picAlphaNum <= 255; picAlphaNum ++ ){
If (picAlphaNum = 255 ){
Msv. picAlphaFlag = false; // when the image fades out, mark the reset
PicX = 0; // x coordinate of the image
PicY = Constant. SCREENHEIGHT-Constant.PICHEIGHT; // picture y coordinate
System. out. println (msv. picAlphaFlag + "picX:" + picX + "picY:" + picY );
}
Msv. setPicAlphaNum (picAlphaNum );
Try {
Thread. sleep (Constant. PICALPHASPEED );
} Catch (Exception e) {e. printStackTrace ();}
}
}
}
}
This part of the code is hard for me, a beginner of java, but I tried to figure out the framework after two days.
The Code involves some basic java knowledge. I made some notes as follows:
[Extends]:
A class uses the keyword extends to inherit other classes. The keyword extends appears after the class name when the class is declared,
Extends is followed by the name of the class to be inherited, and extends implements inheritance. Only one class can be inherited in Java.
[Super]:
B inherits the method A and B wants to call the method A, so you can use the method super.. In Chinese, super is an alias of the parent class.
[Implements]:
Implements is a key word used by a class to implement an interface,
It is used to implement the abstract methods defined in interfaces.
. For example, people is an interface with the say method.
Public interface people ()
{
Public say ();
}
However, the interface does not have a method body.
Only one specific class can be used to implement the method body.
For example, the chinese class implements the people interface.
Public class chinese implements extends El {
Public say ()
{System. out. println ("Hello! ");}
}
[Differences between extends and implements]:
[Plain]
Extends inherits the parent class, as long as the class is not declared as final or defined as abstract, it can inherit,
JAVA does not support multiple inheritance, but can be implemented using interfaces. In this case, implements must be used to inherit only one class,
However, implements can implement multiple interfaces, which can be separated by commas.
For example
Class A extends B implements C, D, E
Extends inherits the parent class, as long as the class is not declared as final or defined as abstract, it can inherit,
JAVA does not support multiple inheritance, but can be implemented using interfaces. In this case, implements must be used to inherit only one class,
However, implements can implement multiple interfaces, which can be separated by commas.
For example
Class A extends B implements C, D, E