Android 开发:将小图块/位图组合成一张位图

发布于 2024-11-24 05:23:36 字数 612 浏览 3 评论 0原文

我试图将所有的小图像(例如草、水和沥青等)整合到一张位图中。

我有一个像这样的数组:

public int Array[]={3, 1, 3, 3, 1, 1, 3, 3, 3, 3, 
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
            1, 1, 1, 1 ,1 ,1, 1, 1 ,1 ,1 
            ,7 ,7 ,7, 7, 7 ,7, 7 ,7 ,7, 7 
            ,7 ,7 ,7 ,7, 7 ,7, 7 ,7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7, 7, 7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7 ,7 ,7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7, 7 ,7 ,7, 7 
            ,6, 6, 6, 6, 6 ,6 ,6, 6, 6 ,6 
            ,6, 6, 6 ,6, 6, 6 ,6, 6 ,6 ,6 };

所以基本上这是一个 10*10 每个数字都是 image(number).png 的占位符

但是如何将它们合并在一起?

//西蒙

Im trying to get all my small images like grass, water and asphalt and so on, into one bitmap.

I have an Array that goes like this:

public int Array[]={3, 1, 3, 3, 1, 1, 3, 3, 3, 3, 
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
            1, 1, 1, 1 ,1 ,1, 1, 1 ,1 ,1 
            ,7 ,7 ,7, 7, 7 ,7, 7 ,7 ,7, 7 
            ,7 ,7 ,7 ,7, 7 ,7, 7 ,7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7, 7, 7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7 ,7 ,7 ,7 ,7 
            ,7 ,7 ,7 ,7, 7, 7, 7 ,7 ,7, 7 
            ,6, 6, 6, 6, 6 ,6 ,6, 6, 6 ,6 
            ,6, 6, 6 ,6, 6, 6 ,6, 6 ,6 ,6 };

So basicly this is a 10*10
Every number is a placeholder for a image(number).png

But how do i merge them together?

//Simon

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

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

发布评论

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

评论(2

一向肩并 2024-12-01 05:23:39

如果所有图块大小相同,您可以创建一个大的位图并将所有图块绘制在正确的位置。例如:

private static final int MAP_WIDTH = 10; // in tiles
private static final int MAP_HEIGHT = 10;
private static final int TILE_WIDTH = 10;
private static final int TILE_HEIGHT = 10;

public Bitmap createMap() {
    Bitmap mainBitmap = Bitmap.createBitmap(TILE_WIDTH * MAP_WIDTH, TILE_HEIGHT * MAP_HEIGHT,
            Bitmap.Config.ARGB_8888);
    Canvas mainCanvas = new Canvas(mainBitmap);
    Paint tilePaint = new Paint();
    for (int i = 0; i < map.length; i++) {
        // Grab tile image (however you want - don't need to follow this)
        Bitmap tile = BitmapFactory.decodeResource(getResources(), getResources()
                .getIdentifier(String.valueOf(map[i]), "drawable", "your.package"));

        // Draw tile to correct location
        mainCanvas.drawBitmap(tile, (i % MAP_WIDTH) * TILE_WIDTH,
                (i / MAP_WIDTH) * TILE_HEIGHT, tilePaint);
    }
    return mainBitmap;
}

If all the tiles are the same size you can create one large Bitmap and draw all the tiles in the right location. For example:

private static final int MAP_WIDTH = 10; // in tiles
private static final int MAP_HEIGHT = 10;
private static final int TILE_WIDTH = 10;
private static final int TILE_HEIGHT = 10;

public Bitmap createMap() {
    Bitmap mainBitmap = Bitmap.createBitmap(TILE_WIDTH * MAP_WIDTH, TILE_HEIGHT * MAP_HEIGHT,
            Bitmap.Config.ARGB_8888);
    Canvas mainCanvas = new Canvas(mainBitmap);
    Paint tilePaint = new Paint();
    for (int i = 0; i < map.length; i++) {
        // Grab tile image (however you want - don't need to follow this)
        Bitmap tile = BitmapFactory.decodeResource(getResources(), getResources()
                .getIdentifier(String.valueOf(map[i]), "drawable", "your.package"));

        // Draw tile to correct location
        mainCanvas.drawBitmap(tile, (i % MAP_WIDTH) * TILE_WIDTH,
                (i / MAP_WIDTH) * TILE_HEIGHT, tilePaint);
    }
    return mainBitmap;
}
岁月蹉跎了容颜 2024-12-01 05:23:38

好的,下面的代码片段应该并排组合两个图像。我不想推断 10,但我相信您会自己弄清楚 for 循环。

public Bitmap combineImages(Bitmap c, Bitmap s) {
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth() + s.getWidth(; 
      height = c.getHeight()); 
    } else { 
      width = s.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } 

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 0f, null); 
    //notice that drawing in the canvas will automagically draw to the bitmap
    //as well
    return cs; 
  } 

Okay so the following snippet should combine two images side by side. I didn't want to extrapolate for 10, but I'm sure you'll figure out the for loops by yourself.

public Bitmap combineImages(Bitmap c, Bitmap s) {
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth() + s.getWidth(; 
      height = c.getHeight()); 
    } else { 
      width = s.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } 

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 0f, null); 
    //notice that drawing in the canvas will automagically draw to the bitmap
    //as well
    return cs; 
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文