I. Basic Knowledge: SurfaceView inherits from the View. View updates the animation in the main thread, while SurfaceView updates the animation in a new thread. Main Methods of SurfaceView class: // call pubilic abstract void surfaceCreated (SurfaceHolder holder) When SurfaceView is created // call pubilic abstract void surfaceChanged (SurfaceHolder holder, int format, int width, int height) // call pubilic abstract void surfaceDestroyed (SurfaceHolder holder) When SurfaceView is destroyed // draw the SurfaceView screen protected void onDraw (Canvas canvas Canvas) (the canvas parameter is the paint brush of the SurfaceView. This method is called for every screen change in the SurfaceView.) 2, Effect: 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 screen on the mobile phone collide with each other, the horizontal speed and direction of the image remain unchanged, the speed in the vertical direction remains the same, and the direction is the opposite; 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. 3. Programming implementation: 1. Interface editing (res \ layout \ main. xml): [java] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android =" http://schemas.android.com/apk/res/android "Android: orientation =" vertical "android: layout_width =" fill_parent "android: layout_height =" fill_parent "> <TextView android: layout_width =" fill_parent "android: layout_height = "wrap_content" android: text = "@ string/hello"/> </LinearLayout> 2. code EDIT: (\ src \ wyf \ zcl \ MyActivity. java) [java] package wyf. zcl;/** This example demonstrates how to plot a simple scenario in surfaceView * MyActivity. java is the main Activity * MySurfaceView of the program. java is the SurfaceView class * C of the program. Onstant. java constant class, which writes all constants in this class * OnDrawThread. java is used to refresh the onDraw from time to time and redraw the image * PicRunThread. java is the class that controls the motion of duke images. **/import android. app. activity; // introduce the related package import android. content. pm. activityInfo; // introduce the related package import android. OS. bundle; // introduce the related package import android. view. window; // introduce the related package import android. view. windowManager; // introduce the related package public class MyActivity extends Activity {/** Called when the activity is first created. */Private MySurfaceView msv; // get surfaceView reference @ Override public void onCreate (Bundle savedInstanceState) {// Activity life cycle function, which is called at Program Creation. onCreate (savedInstanceState); msv = new MySurfaceView (MyActivity. this); // instantiate the requestWindowFeature (Window. FEATURE_NO_TITLE); // you can specify whether the title bar getWindow () is displayed on the screen (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLA G_FULLSCREEN); // set full screen // set to only allow landscape screen this. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); setContentView (msv); // set the content displayed in the Activity to msv }}( \ src \ wyf \ zcl \ Constant. java) [java] package wyf. zcl; import android. view. display; // Constant. java Constant class, write 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 PIC WIDTH = 64; // Image WIDTH public static int PICHEIGHT = 64; // Image Height public static int ONDRAWSPEED = 30; // onDraw Thread class draw interval public static float PICXSPEED = 1.5f; // picture horizontal movement speed public static float PICYSPEED = 2; // vertical movement speed of the image public static int PICRUNSPEED = 30; // The refreshing speed of the motion thread of the image public static int PICALPHASPEED = 20; // demonstrate the refresh speed for the image fading effect} (\ src \ wyf \ zcl \ MySurfaceView. java) [java] package wyf. zcl; import android. content. context; // introduce related package I Mport android. graphics. bitmap; // introduce the related package import android. graphics. bitmapFactory; // introduce the related package import android. graphics. canvas; // introduce the related package import android. graphics. color; // introduce the related package import android. graphics. paint; // introduce the related package import android. view. display; // import related packages to android. view. surfaceHolder; // introduce the related package import android. view. surfaceView; // introduce the related package public class MySurfaceView extends SurfaceView implements SurfaceHolder. cal Lback {// SurfaceHolder is implemented here. callback interface to add the life cycle Callback function int dy = Display for surfaceView. DEFAULT_DISPLAY; MyActivity ma; // get the reference Paint paint of MyActivity; // The reference OnDrawThread odt of the Paint brush; // The OnDrawThread class references PicRunThread prt; // The Thread class of image motion references private float picX = 0; // The image x coordinate private float picY = 0; // The image y coordinate boolean picAlphaFlag = false; // mark the dimmed image. false indicates that the image is not displayed, and true indicates that 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); // register the callback interface paint = new Paint (); // instantiate the paint brush odt = new OnDrawThread (this ); // instantiate OnDrawThread class prt = new PicRunThread (this); // instantiate PicRunThread class prt. start ();} public void setPicX (float picX) {// specifies the x coordinate of the image. This. picX = picX;} public void setPicY (float picY) {// this. picY = picY;} public void setPicAlphaNum (int picAlphaNum) {// set the alpha parameter for image darkening effect this. picAlphaNum = picAlphaNum;} @ Override protected void onDraw (Canvas canvas) {// onDraw method, which is used to draw images, graphics, and other super. onDraw (canvas); paint. setColor (Color. WHITE); // set the paint brush to a WHITE canvas. drawRect (0, 0, Constant. SCREENWIDTH, Constant. SCREENHEIGHT, paint); // The image is drawn here. A white full-screen rectangle to set the background to white and clear the background at the same time. // map Bitmap bitmapDuke = BitmapFactory. decodeResource (ma. getResources (), R. drawable. duke); canvas. drawBitmap (bitmapDuke, picX, picY, paint); // 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 (Su RfaceHolder holder, int format, int width, int height) {// this method is called when surfaceView is changed, such as screen size change.} @ Override public void surfaceCreated (SurfaceHolder holder) {// This method calls odt when surfaceView is created. start (); // start the onDraw draw thread} @ Override public void surfaceDestroyed (SurfaceHolder holder) {// this method is called before surfaceView destruction} (\ src \ wyf \ zcl \ OnDrawThread. java) [java] package wyf. zcl; import android. graphics. canvas; // introduce the related package import android. view. surfaceHolder; // introduce related packages // This class is used to refresh onDraw from time to time and redraw public class OnDrawThread e Xtends Thread {MySurfaceView msv; // obtain the reference SurfaceHolder sh of MySurfaceView; // SurfaceHolder references public OnDrawThread (MySurfaceView msv) {super (); this. msv = msv; // In the constructor, point msv reference to the object sh = msv that calls the MySurfaceView class. getHolder () ;}@ Override public void run () {super. run (); Canvas canvas = null; // Canvas reference while (true) {try {canvas = sh. lockCanvas (null); // point the reference of the canvas to the object synchronized (this. sh ){ // The rendering process may cause synchronization problems. Lock if (canvas! = Null) {msv. onDraw (canvas) ;}} finally {try {if (sh! = Null) {sh. unlockCanvasAndPost (canvas); // unlock after drawing} catch (Exception e) {e. printStackTrace () ;}try {Thread. sleep (Constant. ONDRAWSPEED); // rest for 1 second} catch (Exception e) {e. printStackTrace () ;}}}( \ src \ wyf \ zcl \ PicRunThread. java) [java] package wyf. zcl; // This class is the public class PicRunThread extends Thread {MySurfaceView msv; // reference of MySurfaceView private float picX = 0; // image x coordinate private float picY = Consta Nt. SCREENHEIGHT-Constant.PICHEIGHT; // picture y coordinate boolean yRunFlag = false; // motion marker on y direction, false y = y + speed, when 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) {// controls the motion of the duke image while (this. picX <Constant. SCREENWIDTH) {// when the left side of the image completely exceeds the right side of the screen, the loop ends msv. setPicX (picX); Msv. setPicY (picY); picX = picX + Constant. PICXSPEED; if (yRunFlag) {// should move up, auto minus picY = picY-Constant.PICYSPEED;} else {// should move down, auto plus picY = picY + Constant. PICYSPEED;} if (picY <= 0) {// yRunFlag = false along the screen;} else if (picY> Constant. SCREENHEIGHT-Constant.PICHEIGHT) {// arrive at the bottom of the screen along yRunFlag = true;} try {Thread. sleep (Constant. PICRUNSPEED);} catch (Exception e) {e. printStackTrace () ;}// demonstrate msv in the case of image darkening. picAlphaFlag = true; // Enabled For (picAlphaNum = 0; picAlphaNum <= 255; picAlphaNum ++) {if (picAlphaNum = 255) {msv. picAlphaFlag = false; // when the image fades out, mark the reset picX = 0; // The image x coordinate 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 () ;}}} However, it was hard for people to understand the framework after two days. The Code involves some basic java knowledge. I made some notes as follows: [extends]: One class uses the keyword extends to inherit other classes, when the keyword extends appears in the class declaration, 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 of A and B wants to call the method of A, so you can use the method of 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 the interface. For example, people is an interface with the say method. Public interface people () {public say ();} but 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 from the parent class. As long as the class is not declared as final or defined as abstract, it can be inherited, JAVA does not support multi-inheritance, but can be implemented using interfaces. implements must be used to inherit only one class, but implements can implement multiple interfaces, separate them with commas (,). For example, class A extends B implements C, D, E.