简体   繁体   中英

How to set co-ordinates to a Bitmap befroe drawing to canvas

I'm trying tile mapping, I can't seem to set the coordinates to a bitmap before I draw it to the canvas.

I'm running through a for loop trying to add bitamps to an array, so later I can draw them all where they need to go. The problem is I don't know how to create a bitmap with the coordinates already defined. I know how to make a RectF and add them while drawing them, but that doesn't help because if I make an array of Rectf instead of an array of bitmaps it only draws the last one.

I'll try and add the relevant code below. First I'll add the code to create the array then the code to draw the array.

//this is the for loop that runs through the int array, and adds the bitmaps to the bitmap array when needed  

    for (int row = 0; row < mapHeight; row++) {

                for (int col = 0; col < mapWidth; col++) {             

                    switch (board[row][col]) {
                        case 0:
                            myImage = null;
                            break;
                        case 1: 
                            int curL = rowBaseX + (col * tileWidth);
                            int curU = rowBaseY + (row * tileHeight);
                            int curR = curL + tileWidth;
                            int curD = curU + tileHeight;                          

    //The following line is one of the many ways I've tried to do it. What it       
    //needs to do is set the coordinates to the bitmap myImage

                           myImage =    Bitmap.createBitmap(myImage,curL,curU,curR,curD);// this line doesn't cause                                                                 an error but I don't know if its correct


                            break;

                    }
                          tiles[row][col] = myImage;
                }
            }
    }


//this is the code to draw the array

            for (int i = 0; i < tiles.length; i++) {

                for (int a = 0; a < tiles[i].length; a++) {

                    if (tiles[i][a] != null) {



                        Log.e("drawing", "1");

// this is the next problem, once I've added the coordinates to the bitmap
I'm not sure how to draw it without it asking me for the coordinates again

                        canvas.drawBitmap(tiles[i][a], frameToDrawTiles,null);
                        Log.e("drawing", "2");
                    }

                }
            }
}

I'm also not sure if bitmaps are the correct way of doing this, if there's a better way please let me know.

Thank you for your help.

Worked it out for my self. I need to create an array of bitmaps and an array of RectF and then combine them when I'm drawing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM