Android Dual Buffering Technology example detailed _android

Source: Internet
Author: User

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; }
 }
}
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.