android:如何将一组位图合并到一个位图中? [英] android: how to merge an array of bitmap together in one bitmap?

查看:75
本文介绍了android:如何将一组位图合并到一个位图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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 文件加载和构建的地图..

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

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

( 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 .. )

感谢您的帮助

推荐答案

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

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

MapLoader 类为数组block[x][y] 填充每个图块的 int 值.

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

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

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

当找到一个时,对应的位图,取自瓦片(总是相同的……在瓷砖上的相同位置:0,0,16,16 这是一个用于碰撞的 16x16 红色瓷砖),被绘制到与数组循环相同的 pos.x,y 处的临时位图.

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.

当循环退出时,已经构建了collisionPicDest位图,在最后一张照片中混合了许多较小的部分.

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

这篇关于android:如何将一组位图合并到一个位图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆