Android - determine specific locations (X,Y coordinates) on a Bitmap on different resolutions?

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-11-10T22:01:53Z Indexed on 2010/12/24 0:54 UTC
Read the original article Hit count: 219

Filed under:
|
|
|

My app that I am trying to create is a board game. It will have one bitmap as the board and pieces that will move to different locations on the board. The general design of the board is square, has a certain number of columns and rows and has a border for looks. Think of a chess board or scrabble board.

Before using bitmaps, I first created the board and boarder by manually drawing it - drawLine & drawRect. I decided how many pixels in width the border would be based on the screen width and height passed in on "onSizeChanged". The remaining screen I divided by the number of columns or rows I needed.

For examples sake, let's say the screen dimensions are 102 x 102.

I may have chosen to set the border at 1 and set the number of rows & columns at 10. That would leave 100 x 100 left (reduced by two to account for the top & bottom border, as well as left/right border). Then with columns and rows set to 10, that would leave 10 pixels left for both height and width.

No matter what screen size is passed in, I store exactly how many pixels in width the boarder is and the height & width of each square on the board. I know exactly what location on the screen to move the pieces to based on a simple formula and I know exactly what cell a user touched to make a move.

Now how does that work with bitmaps? Meaning, if I create 3 different background bitmaps, once for each density, won't they still be resized to fit each devices screen resolution, because from what I read there were not just 3 screen resolutions, but 5 and now with tablets - even more. If I or Android scales the bitmaps up or down to fit the current devices screen size, how will I know how wide the border is scaled to and the dimensions of each square in order to figure out where to move a piece or calculate where a player touched. So far the examples I have looked at just show how to scale the overall bitmap and get the overall bitmaps width and height. But, I don't see how to tell how many pixels wide or tall each part of the board would be after it was scaled. When I draw each line and rectangle myself based in the screen dimensions from onSizeChanged, I always know these dimensions.

If anyone has any sample code or a URL to point me to that I can a read about this with bitmaps, I would appreciate it.

Thanks,

--Mike

BTW, here is some sample code (very simplified) on how I know the dimensions of my game board (border and squares) no matter the screen size. Now I just need to know how to do this with the board as a bitmap that gets scaled to any screen size.


 @Override
   protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      intScreenWidth = w;
      intScreenHeight = h;

      // Set Border width - my real code changes this value based on the dimensions of w
      // and h that are passed in. In other words bigger screens get a slightly larger
      // border.
      intOuterBorder = 1;

      /** Reserve part of the board for the boardgame and part for player controls & score
          My real code forces this to be square, but this is good enough to get the point
          across.
      **/
      floatBoardHeight = intScreenHeight / 4 * 3;

      // My real code actually causes floatCellWidth and floatCellHeight to
      // be equal (Square).
      floatCellWidth = (intScreenWidth - intOuterBorder * 2 ) / intNumColumns;
      floatCellHeight = (floatBoardHeight - intOuterBorder * 2) / intNumRows;

      super.onSizeChanged(w, h, oldw, oldh);
   }

© Stack Overflow or respective owner

Related posts about android

Related posts about scaling