现在的位置: 首页 > 综合 > 正文

VIEW双缓冲

2013年10月14日 ⁄ 综合 ⁄ 共 1089字 ⁄ 字号 评论关闭
package game.yb;

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

/**
 * @author yubin
 * @version 2012-8-15 上午10:06:07
 **/
public class GameView extends View implements Runnable {
    Bitmap bit = null;
    Paint mPaint = null;
    // 创建一个缓冲区
    Bitmap mSCBitmap = null;
    // 创建Canvas
    Canvas mCanvas = null;

    public GameView(Context context) {
        super(context);
        // 装载资源
        bit = ((BitmapDrawable) getResources().getDrawable(R.drawable.icon))
                .getBitmap();
        // 创建屏幕大小缓冲区
        mSCBitmap = Bitmap.createBitmap(480, 800, Config.ARGB_8888);
        // 创建Canvas
        mCanvas = new Canvas();
        // 将内容绘制在mSCBitmap
        mCanvas.setBitmap(mSCBitmap);
        mPaint = new Paint();
        // bit绘制在mSCBitmap
        mCanvas.drawBitmap(bit, 0, 0, mPaint);
        new Thread(this).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 将mSCBitmap显示在屏幕上
        canvas.drawBitmap(mSCBitmap, 0, 0, mPaint);
    }

    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100);
            }
            catch(Exception e) {
                Thread.currentThread().interrupt();
            }
            postInvalidate();
        }
    }
}

抱歉!评论已关闭.