欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android雙緩沖技術(shù)實(shí)例詳解

 更新時(shí)間:2014年07月22日 09:34:19   投稿:shichen2014  
這篇文章主要介紹了android雙緩沖技術(shù)實(shí)例詳解,需要的朋友可以參考下

Android中的SurfaceView類就是雙緩沖機(jī)制。因此,在進(jìn)行Android游戲開(kāi)發(fā)時(shí)應(yīng)盡量使用SurfaceView而不要使用View,這樣的話效率較高,并且SurfaceView的功能也更加完善。為了更容易的了解雙緩沖技術(shù),下面介紹用View實(shí)現(xiàn)雙緩沖的方法。

在此需要說(shuō)明一下,雙緩沖的核心技術(shù)就是先通過(guò)setBitmap方法將要繪制的所有的圖形繪制到一個(gè)Bitmap上,然后再來(lái)調(diào)用drawBitmap方法繪制出這個(gè)Bitmap,顯示在屏幕上。其具體的實(shí)現(xiàn)代碼如下:

先貼出View類代碼:

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
{
 /* 聲明Bitmap對(duì)象 */
 Bitmap mBitQQ = null;
 Paint  mPaint = null;
 /* 創(chuàng)建一個(gè)緩沖區(qū) */
 Bitmap mSCBitmap = null;
 /* 創(chuàng)建Canvas對(duì)象 */
 Canvas mCanvas = null;  
 public GameView(Context context)
 {
 super(context);
 /* 裝載資源 */
 mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
 /* 創(chuàng)建屏幕大小的緩沖區(qū) */
 mSCBitmap=Bitmap.createBitmap(320, 480, Config.ARGB_8888); 
 /* 創(chuàng)建Canvas */
 mCanvas = new Canvas(); 
 /* 設(shè)置將內(nèi)容繪制在mSCBitmap上 */
 mCanvas.setBitmap(mSCBitmap); 
 mPaint = new Paint();
 /* 將mBitQQ繪制到mSCBitmap上 */
 mCanvas.drawBitmap(mBitQQ, 0, 0, mPaint);
 /* 開(kāi)啟線程 */
 new Thread(this).start();
 }
 public void onDraw(Canvas canvas)
 {
 super.onDraw(canvas);
 /* 將mSCBitmap顯示到屏幕上 */
 canvas.drawBitmap(mSCBitmap, 0, 0, mPaint);
 }
 // 觸筆事件
 public boolean onTouchEvent(MotionEvent event)
 {
 return true;
 }
 // 按鍵按下事件
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
 return true;
 }
 // 按鍵彈起事件
 public boolean onKeyUp(int keyCode, KeyEvent event)
 {
 return false;
 }
 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
 {
 return true;
 }
 /**
 * 線程處理
 */
 public void run()
 {
 while (!Thread.currentThread().isInterrupted())
 {
  try
  {
  Thread.sleep(100);
  }
  catch (InterruptedException e)
  {
  Thread.currentThread().interrupt();
  }
  //使用postInvalidate可以直接在線程中更新界面
  postInvalidate();
 }
 }
}

相關(guān)文章

最新評(píng)論