1

我正在尝试在android上为平铺制作地图加载器..到目前为止,我可以解析tmx文件,获取所有平铺数据,并将它们放入二维数组中,如下所示:Bitmap tiles[x][y] ...它可以工作,我现在可以在 android 上渲染平铺地图,但只能通过该 tiles[][] 数组进行交互,如下所示..

如何将位图数组的内容合并到一个位图中?

这是我的渲染方法:

//here's what i have:
for (int x = 0; x < MapLoader.width; x++) {
  for (int y = 0; y < MapLoader.height; y++) {
    g.drawBitmap( picSource[x][y], posX, posY, BitmapPaint);
  }
}

//and here's what i'd like to have:
g.drawBitmap( picDest, posX, posY, BitmapPaint);

我想遍历 picSource[x][y] 获取所有位图并将它们全部放在 picDest 中。所以我可以获得一张大图,代表我从平铺的 tmx 文件加载和构建的地图。

(请注意, picSource[][] 数组中包含的任何位图都没有位于相同的位置.. 没有位图在任何其他位图之上,它们只是显示在一个网格中,每个位图都是 4x3 网格中的 32x32 位图..每个网格上都有自己的位置..)

谢谢您的帮助

4

2 回答 2

1

我制作了以下版本,仅使用 Bitmap 库即可。它适用于一维数组,但将其更改为使用矩阵将是微不足道的。

private Bitmap rebuildBitmapFromPatches(Bitmap [] patches, int finalWidth, int finalHeight){
        int width = patches[0].getWidth();
        int height = patches[0].getHeight();
        Bitmap image = Bitmap.createBitmap(finalWidth,finalHeight,patches[0].getConfig());
        int i=0;
        int [] pixelsAux = new int [width*height];
        for(int x = 0; x<finalWidth; x+=width){
            for(int y = 0; y<finalHeight; y+=height){
                patches[i].getPixels(pixelsAux,0,width,0,0,width,height);
                image.setPixels(pixelsAux,0, width,x,y,width, height);
                i++;
            }
        }
        return image;
于 2020-07-16T15:02:01.187 回答
0

如果其他人想知道,我会发布我是如何做到的:

MapLoader 类用每个图块的 int 值填充数组“block[x][y]。

我们遍历该数组,查找 pos:x,y 处所有值为 169 的图块。

当找到一个时,从 tilesheet 中获取的相应位图(始终相同..在 tilesheet 上的相同位置:0,0,16,16 它是用于碰撞的 16x16 红色瓷砖),被绘制到临时位图中在数组循环所在的同一个 pos.x,y 处。

当循环退出时,collisionPicDest 位图已经建立,在 1 个最终图片中混合在一起的许多较小的部分。

collisionSheet = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.collision16);
collisionPic = new Bitmap[width][height];
collisionPicDest = Bitmap.createBitmap(width*tileSize, height*tileSize, Bitmap.Config.RGB_565);
collisionCanvas = new Canvas(collisionPicDest());
// ===============
// collision layer
// ===============
for (int y = 0; y < height; y++) {
  for (int x = 0; x < width; x++) {

    tileNbr = MapLoader.block[x][y];


    switch(tileNbr){
    case 169:
      // ============
      // normal block
      // ============
      collisionPic[x][y]= Bitmap.createBitmap(collisionSheet, 0, 0, tileSize, tileSize);
      collisionCanvas.drawBitmap(collisionPic[x][y],x*tileSize,y*tileSize,bitmapPaint);
      break;

      // do other cases..
    }
  }
于 2011-04-05T23:12:56.137 回答