【发布时间】:2011-10-26 20:00:24
【问题描述】:
我正在使用 Android 2.2 构建游戏。主游戏Activity使用自定义SurfaceView:
class GameView extends SurfaceView
据我了解,onDraw() 方法需要执行自己的线程。考虑到这一点,我打算在onDraw() 中添加背景图片:
canvas.drawBitmap(wallpaper, 0, 0, paint);
paint = new Paint();
但是当我执行游戏时,它变得非常缓慢。如果我注释掉new Paint() 行,游戏会加速。
是我做错了什么,或者我的问题有解决方案吗?例如,有没有办法减少对onDraw() 的调用次数?或者将XML 属性添加到我的自定义SurfaceView 类?
这是我如何加载可绘制图像的代码。
public Bitmap loadBitmap(String image) {
Bitmap bitmap = null;
try {
int id = R.drawable.class.getField(image).getInt(new Integer(0));
bitmap = BitmapFactory.decodeResource(context.getResources(), id);
// bitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
} catch(Exception ex) {
Log.e("loadBitmap", ex.getMessage());
}
return bitmap;
}
这是onDraw 方法的代码。
不幸的是,我无法发布所有内容。
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
canvas.drawBitmap(gameLevel.getBitmap(), 0, 0, paint);
// draw object(1) 320x25
// draw object(5) 50x50 each
// draw object(n) 15x15 each, estimate
// draw object(n) 50x50 each
// collision check, draw hit tile on the image sheet
//使用canvas.drawText()绘制游戏信息 时间线++;
提前致谢!
【问题讨论】:
标签: android canvas surfaceview ondraw