Android improve performance of map of multi-dimensional arrays

30 Views Asked by At

I have a working code but what I want to achieve is better FPS performance in drawMap() (this method is called later in custom SurfaceView's onDraw() method). The problem is of course with these huge bitmapsAnimated and canvasesAnimated objects. As a solution I would like for example to split them to smaller constructions. I guess it could much improve my FPS but I don't know how to modify my code to do this. I am also open to another improvements regarding this issue that you suggest.

private static final Map<Integer, Bitmap[][][]> bitmapsAnimated = new HashMap<>();
private static final Map<Integer, Canvas[][][]> canvasesAnimated = new HashMap<>();

public void init(List<Integer> animFrames) {
        for (int i = 0; i < animFrames.size(); i++) {
            runnables.add(new FrameDurationRunnable(animFrames.get(i), animsDuration.get(i)));
            bitmapsAnimated.put(i, new Bitmap[animFrames.get(i)][40][60]);
            canvasesAnimated.put(i, new Canvas[animFrames.get(i)][40][60]);
            for (int j = 0; j < animFrames.get(i); j++) {
                for (int a = 0; a < 40; a++) {
                    for (int b = 0; b < 60; b++) {
                        bitmapsAnimated.get(i)[j][a][b] = Bitmap.createBitmap(GameSurface.cellWidth, GameSurface.cellHeight, Bitmap.Config.ARGB_8888);
                        canvasesAnimated.get(i)[j][a][b] = new Canvas(bitmapsAnimated.get(i)[j][a][b]);
                    }
                }
            }
        }
    }

later in loop inside another method:

canvasesAnimated.get(l)[frame][k][j].drawBitmap(tilesets[currentTileSetIndex], source, dest, null);

and finally:

public static void drawMap(Canvas canvas, GameSurface gs) {

    int moveX = -((GameMainSurface) gs).moveX;
    int moveY = -((GameMainSurface) gs).moveY;

    for (int i = moveX / GameSurface.cellWidth; i < (moveX / GameSurface.cellWidth) + GameSurface.VISIBLE_COLUMN_NUMBER + 1; i++) {
        for (int j = moveY / GameSurface.cellHeight; j < (moveY / GameSurface.cellHeight) + GameSurface.VISIBLE_ROW_NUMBER + 1; j++) {

            for (int k = 0; k < bitmapsAnimated.size(); k++) {
                canvas.drawBitmap(bitmapsAnimated.get(k)[((FrameDurationRunnable) runnables.get(k)).iter][i][j], i * GameSurface.cellWidth - moveX, j * GameSurface.cellHeight - moveY, null);
            }
        }
    }

    if (!isIterStarted) {
        iterTmxFrames();
        isIterStarted = true;
    }
}


private static void iterTmxFrames() {
    for (int i = 0; i < runnables.size(); i++) {
        handler.postDelayed(runnables.get(i), animsDuration.get(i));
    }
}
0

There are 0 best solutions below