我正在制作一款包含大量像素艺术的游戏。我的游戏中的标准按钮约为 12x12 像素,并按比例放大了五倍以进行显示。根据屏幕的不同,所有字符也都放大了两倍。问题是,如果我将所有这些图像存储到放大的位图,我会在 Android 中遇到内存不足错误。如果我每次都尝试按比例放大它们进行绘图,那么游戏运行速度非常慢。
这就是我编写自己的 PixelArtBitmap 类的原因。它可以生成出色的图像并且只需要很少的内存,但是效率非常低,大大降低了我的 fps。它仍然比每次绘制位图都要快。这是课程:
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
class PixelArtBitmap{
private int[][] pixels = null;
private boolean[][] visible = null;
private float width = 0;
private float height = 0;
private float drawWidth = 0;
private float drawHeight = 0;
private int pixelsX = 0;
private int pixelsY = 0;
private static Paint boxPaint = new Paint();
public PixelArtBitmap(Bitmap bitmap, float scale){
boxPaint.setStrokeWidth(0);
pixels = new int[bitmap.getWidth()][bitmap.getHeight()];
visible = new boolean[bitmap.getWidth()][bitmap.getHeight()];
pixelsX = bitmap.getWidth();
pixelsY = bitmap.getHeight();
//Utility is an another class of mine, providing information about the device
drawWidth = scale * Utility.getScaleWidth();
drawHeight = scale * Utility.getScaleHeight();
width = pixelsX * drawWidth;
height = pixelsY * drawHeight;
for(int x = 0 ; x < bitmap.getWidth() ; x++){
for(int y = 0 ; y < bitmap.getHeight() ; y++){
pixels[x][y] = bitmap.getPixel(x,y);
if(Color.alpha(pixels[x][y]) == 0) visible[x][y] = false;
else visible[x][y] = true;
}
}
bitmap.recycle();
bitmap = null;
}
public void draw(float x, float y, Canvas canvas){
for(int drawY = 0 ; drawY < pixelsY ; drawY++){
for(int drawX = 0 ; drawX < pixelsX ; drawX++){
if(visible[drawX][drawY]){
boxPaint.setColor(pixels[drawX][drawY]);
canvas.drawRect(x + drawWidth * drawX, y + drawHeight * drawY,
x + drawWidth * (drawX + 1), y + drawHeight * (drawY + 1), boxPaint);
}
}
}
}
}
那么,我怎样才能让这门课更快,或者有没有完全不同的方法可以正确地做到这一点?