2

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

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
carefacerz
  • 1,287
  • 3
  • 16
  • 27
  • You might want to provide more information. Such as how many columns/rows you want, and in which order you want the images merged. Do you want them combined side-by-side -- or something different? – hwrdprkns Jul 14 '11 at 15:28
  • It is a 10*10 and yes the should get sidebyside just like pokemon. The order is specified bu the array – carefacerz Jul 14 '11 at 15:30
  • This would not be trivial. It might be much quicker just to subclass `View` and pass this array to the constructor and then just create 10 views any way you want. That way you can always rearrange them easily. – hwrdprkns Jul 14 '11 at 15:37
  • 2 things i am using surfaceview and not view. Second is that the 10*10 maby change depends on level, it might be 15*15 sometimes – carefacerz Jul 14 '11 at 15:41

2 Answers2

6

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; 
  } 
hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • Hi there, I tried to use the loop one, but the large bitmap just draw only one the first element. For detail: http://stackoverflow.com/questions/21044984/android-combine-many-bitmaps-to-one-large-one-goes-wrong-and-cant-recycle-bitma?noredirect=1#comment31640898_21044984 – lolyoshi Jan 10 '14 at 13:58
0

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;
}
Che Jami
  • 5,151
  • 2
  • 21
  • 18
  • Unfortuntlly not. the are different like 10 different. Any help? – carefacerz Jul 14 '11 at 18:45
  • Slightly confused, how do you maintain the width and height of the map if each of the images are different sizes? The key is knowing how big the map will be before we start drawing. – Che Jami Jul 18 '11 at 10:38