the Surfaceview class in Android is a double buffering mechanism. Therefore, in the development of the Android game should try to use Surfaceview rather than view, so the efficiency is high , and Surfaceview function is also more perfect. To make it easier to understand double buffering, here's how to implement double buffering with view.
In this need to explain, the core of double buffering technology is the SetBitmap method to draw all the graphics to a bitmap, and then call the Drawbitmap method to draw the bitmap, displayed on the screen. Its specific implementation code is as follows:
First post the View class code:
Package com.lbz.pack.test;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Bitmap.Config;
Import android.graphics.drawable.BitmapDrawable;
Import android.view.KeyEvent;
Import android.view.MotionEvent;
Import Android.view.View;
public class Gameview extends View implements Runnable {/* declares Bitmap object/Bitmap mbitqq = null;
Paint mpaint = null;
/* Create a buffer * * Bitmap mscbitmap = null;
* * Create Canvas object/Canvas Mcanvas = null;
Public Gameview {Super (context);
/* Load resource/MBITQQ = ((bitmapdrawable) getresources (). getdrawable (R.DRAWABLE.QQ)). Getbitmap ();
/* Create screen size buffer * * MSCBITMAP=BITMAP.CREATEBITMAP (config.argb_8888);
/* Create Canvas */Mcanvas = new Canvas ();
/* settings to draw the content on the Mscbitmap/Mcanvas.setbitmap (MSCBITMAP);
Mpaint = new Paint ();
* * To draw the MBITQQ to the Mscbitmap/mcanvas.drawbitmap (mbitqq, 0, 0, mpaint); /* Open thread/new thread (this). Start ();
public void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
/* Display MSCBITMAP to the screen * * CANVAS.DRAWBITMAP (mscbitmap, 0, 0, mpaint);
}//Stylus Event public boolean ontouchevent (Motionevent event) {return true;
//Press the event public boolean onKeyDown (int keycode, keyevent event) {return true;
}//Key Bounce Event public boolean onKeyUp (int keycode, keyevent event) {return false;
public boolean onkeymultiple (int keycode, int. RepeatCount, KeyEvent event) {return true; /** * thread handling/public void run () {while!
Thread.CurrentThread (). isinterrupted ()) {try {thread.sleep (100);
The catch (Interruptedexception e) {thread.currentthread (). interrupt ();
///Use Postinvalidate to update the interface Postinvalidate () directly in the thread; }
}
}