Android SurfaceView 双缓冲机制

来源:互联网 发布:网络剧排行榜前十名 编辑:程序博客网 时间:2024/05/14 06:44

   转自:http://blog.csdn.net/liubingzhao/article/details/5563113

    Android中的SurfaceView类就是双缓冲机制。因此,开发游戏时尽量使用SurfaceView而不要使用View,这样的话效率较高,而且SurfaceView的功能也更加完善。为了更容易的了解双缓冲技术,下面介绍用View实现双缓冲的方法。

    先概述一下,双缓冲的核心技术就是先通过setBitmap方法将要绘制的所有的图形会知道一个Bitmap上,然后再来调用drawBitmap方法绘制出这个Bitmap,显示在屏幕上。具体的实现代码如下:

先贴出View类代码:

  1. package com.lbz.pack.test;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Bitmap.Config;  
  8. import android.graphics.drawable.BitmapDrawable;  
  9. import android.view.KeyEvent;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12.   
  13. public class GameView extends View implements Runnable  
  14. {  
  15.     /* 声明Bitmap对象 */  
  16.     Bitmap  mBitQQ  = null;  
  17.       
  18.     Paint   mPaint = null;  
  19.       
  20.     /* 创建一个缓冲区 */  
  21.     Bitmap  mSCBitmap = null;  
  22.       
  23.     /* 创建Canvas对象 */  
  24.     Canvas mCanvas = null;     
  25.       
  26.     public GameView(Context context)  
  27.     {  
  28.         super(context);  
  29.           
  30.         /* 装载资源 */  
  31.         mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();  
  32.           
  33.         /* 创建屏幕大小的缓冲区 */  
  34.         mSCBitmap=Bitmap.createBitmap(320480, Config.ARGB_8888);    
  35.           
  36.         /* 创建Canvas */  
  37.         mCanvas = new Canvas();    
  38.           
  39.         /* 设置将内容绘制在mSCBitmap上 */  
  40.         mCanvas.setBitmap(mSCBitmap);   
  41.           
  42.         mPaint = new Paint();  
  43.           
  44.         /* 将mBitQQ绘制到mSCBitmap上 */  
  45.         mCanvas.drawBitmap(mBitQQ, 00, mPaint);  
  46.           
  47.         /* 开启线程 */  
  48.         new Thread(this).start();  
  49.     }  
  50.       
  51.     public void onDraw(Canvas canvas)  
  52.     {  
  53.         super.onDraw(canvas);  
  54.           
  55.         /* 将mSCBitmap显示到屏幕上 */  
  56.         canvas.drawBitmap(mSCBitmap, 00, mPaint);  
  57.     }  
  58.       
  59.     // 触笔事件  
  60.     public boolean onTouchEvent(MotionEvent event)  
  61.     {  
  62.         return true;  
  63.     }  
  64.   
  65.   
  66.     // 按键按下事件  
  67.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  68.     {  
  69.         return true;  
  70.     }  
  71.   
  72.   
  73.     // 按键弹起事件  
  74.     public boolean onKeyUp(int keyCode, KeyEvent event)  
  75.     {  
  76.         return false;  
  77.     }  
  78.   
  79.   
  80.     public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)  
  81.     {  
  82.         return true;  
  83.     }  
  84.       
  85.       
  86.     /** 
  87.      * 线程处理 
  88.      */  
  89.     public void run()  
  90.     {  
  91.         while (!Thread.currentThread().isInterrupted())  
  92.         {  
  93.             try  
  94.             {  
  95.                 Thread.sleep(100);  
  96.             }  
  97.             catch (InterruptedException e)  
  98.             {  
  99.                 Thread.currentThread().interrupt();  
  100.             }  
  101.             //使用postInvalidate可以直接在线程中更新界面  
  102.             postInvalidate();  
  103.         }  
  104.     }  
  105. }