发布于 2016-02-24 02:27:00 | 130 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了android双缓冲技术实例详解,需要的朋友可以参考下

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

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

先贴出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对象 */
 Bitmap mBitQQ = null;
 Paint  mPaint = null;
 /* 创建一个缓冲区 */
 Bitmap mSCBitmap = null;
 /* 创建Canvas对象 */
 Canvas mCanvas = null;  
 public GameView(Context context)
 {
 super(context);
 /* 装载资源 */
 mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
 /* 创建屏幕大小的缓冲区 */
 mSCBitmap=Bitmap.createBitmap(320, 480, Config.ARGB_8888); 
 /* 创建Canvas */
 mCanvas = new Canvas(); 
 /* 设置将内容绘制在mSCBitmap上 */
 mCanvas.setBitmap(mSCBitmap); 
 mPaint = new Paint();
 /* 将mBitQQ绘制到mSCBitmap上 */
 mCanvas.drawBitmap(mBitQQ, 0, 0, mPaint);
 /* 开启线程 */
 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();
 }
 }
}


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务