android:如何将位图数组合并到一个位图中?

发布于 2024-09-07 15:02:44 字数 735 浏览 1 评论 0原文

我正在尝试在 Android 上制作一个用于平铺的地图加载器.. 到目前为止,我可以解析 tmx 文件,获取所有图块数据,并将它们放入二维数组中,如下所示: Bitmaptiles[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 中。所以我可以获得 1 个大图片,代表我已加载并从平铺 tmx 文件构建的地图..

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

感谢您的帮助

i'm trying to make a map loader for tiled, on android..
so far i could parse the tmx file, grab all the tile data, and put them in a 2dimensional array, like this: Bitmap tiles[x][y] ...
it works and i can render tiled maps on android now, but only by interating through that tiles[][] array, like shown below..

how can i merge together in one single bitmap the content of a bitmap array ?

here's my render method:

//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);

i would like to itterate through picSource[x][y] grab all the
bitmaps and put them all in picDest. so i can get 1 single big pic, representing the map i've loaded and constructed from tiled tmx file..

( note that no bitmap contained in the picSource[][] array is located a the same position ..
there's no bitmap on top of any other, they're just displayed in a grid
each is a 32x32 bitmap in a 4x3 grid for example..
each its own spot on the grid .. )

thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-09-14 15:02:44

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

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;

I made the following version that would work using only the Bitmap library. It works with one-dimensional arrays but change it for working with matrices would be trivial.

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;
冷了相思 2024-09-14 15:02:44

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

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

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

当找到一个时,相应的位图,取自
瓦片
(总是同一个......在图块表上的相同位置:0,0,16,16,它是用于碰撞的 16x16 红色图块),
被绘制到数组循环所在的相同 pos.x,y 处的临时位图中。

当退出循环时,collisionPicDest位图已经构建完毕,
由许多较小的部件混合在一起形成最后一张照片。

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..
    }
  }

In case someone else is wondering, i'm posting how i did it:

MapLoader class filled the array "block[x][y] with an int value for each tile.

we loop through that array, looking for all tiles at pos:x,y with value 169.

when one is found, the corresponding bitmap, taken from the
tilesheet
(alway the same one..at same position on the tilesheet: 0,0,16,16 it's a 16x16 red tile used for collisions),
is drawn into a temporary bitmap at the same pos.x,y that the array loop is at.

when the loop is exited, collisionPicDest bitmap have been built,
out of lots of smaller parts blended together in 1 final pic.

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..
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文