- 注册时间
- 2004-8-29
- 最后登录
- 1970-1-1
|
发表于 2009-10-7 23:29:02
|
显示全部楼层
刚才去看了一下AD版的代码,这里似乎可以优化?- public void update() {
- // TODO: use buffer directly
- IntBuffer buffer = IntBuffer.allocate(ScreenModel.WIDTH * ScreenModel.HEIGHT);
- buffer.put(mBuffer);
-
- buffer.position(0);
- mBitmap.copyPixelsFromBuffer(buffer);
- mDirty = false;
- }
复制代码 每update一次,会allocate一个新的IntBuffer,这个肯定会严重影响性能。
至少根据我查看docs,这个IntBuffer完全是不必要的。
直接使用mBitmap的setPixels方法将mBuffer传入就OK了。
另一方面,这个mDirty应该也是不需要的,我的ScreenModel已经对刷屏事件进行了优化,只在需要的时候刷屏。
这个mDirty会造成跳帧。
附:Bitmap的setPixels方法文档:
public void setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
Since: API Level 1
Replace pixels in the bitmap with the colors in the array. Each element in the array is a packed int prepresenting a Color
Parameters
pixels The colors to write to the bitmap
offset The index of the first color to read from pixels[]
stride The number of colors in pixels[] to skip between rows. Normally this value will be the same as the width of the bitmap, but it can be larger (or negative).
x The x coordinate of the first pixel to write to in the bitmap.
y The y coordinate of the first pixel to write to in the bitmap.
width The number of colors to copy from pixels[] per row
height The number of rows to write to the bitmap
Throws
IllegalStateException if the bitmap is not mutable
IllegalArgumentException if x, y, width, height are outside of the bitmap's bounds.
ArrayIndexOutOfBoundsException if the pixels array is too small to receive the specified number of pixels. |
|