2013.4.15 双缓冲技术

主要原理:当一个动画争先显示时,程序有在改变他,前面还没有显示完,程序又请求重新绘制,这样屏幕就会不停的闪烁。为了避免闪烁,可以用双缓冲技术,将要哦处理的图片都在内存中处理好之后,再将其显示到屏幕上。这样显示出来的总是完整的图像,不会出现闪烁的现象。

核心技术:先通过setBitmap方法将要绘制哦所有图形绘制到一个Bitmap上,然后在来调用drawBitmap方法绘制出这个Bitmap,显示在屏幕上。

ex:  

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;


public class GameView extends View implements Runnable{
/*声明Bitmap对象
**/
Bitmap mBitQQ=null;
Paint mpaint=null;
/*create teh buffer*/
Bitmap mSCBitmap=null;
/*create Canvas*/
Canvas mCanvas=null;




public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
/*load the resource*/
mBitQQ=((BitmapDrawable)getResources().getDrawable(R.drawable.yhzx)).getBitmap();
//create the buffer of screen
mSCBitmap=Bitmap.createBitmap(320,480,Config.ARGB_8888);
//create the canvas
mCanvas=new Canvas();
//put the content of draw in teh mSCBitmap
mCanvas.setBitmap(mSCBitmap);

mpaint=new Paint();
//将mBitQQ绘制到mSCBitmap上
mCanvas.drawBitmap(mBitQQ,0,0,mpaint);

//open teh thread
new Thread(this).start();
}


@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//将mSCBitmap显示到屏幕上
canvas.drawBitmap(mSCBitmap, 0, 0,mpaint);
}


@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return true;
}


@Override
public void run() {
// TODO Auto-generated method stub
while(!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(100);
}catch(InterruptedException e){
Thread.currentThread().interrupted();
}
postInvalidate();
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值