Reputation: 1246
I'm an absolute beginner at Android programming, so please bear with me :) For my simple game, I'm drawing a 20 x 10 grid consisting of squares at the beginning. Within the game, I would need to draw a sprite right at the center of some of the squares. (analogous to a Checkers grid, with a chip at the center of each square). I want this game to work across various screen sizes and densities. My approach has been to get the canvas width and height at the beginning of the game, calculate the side of the squares and then draw the grid out and save that as a bitmap, and then use that bitmap in the game. That way my grid would scale perfectly across screens.
1) Is there a better approach? Can I use an image of a grid as a bitmap instead? My concern there is that when the image is scaled for different screens, the squares might get distorted into rectangles. Plus it will be hard to calculate where the center of each scaled square is (to place the sprite).
2) I also need to display the title, score, level etc. at some place on the screen. Is it better to just leave space for it on the bitmap and then write the score on the bitmap, or to create a layout with a SurfaceView for the grid and another view where I write the score, level, etc?
Thanks!
Upvotes: 1
Views: 1237
Reputation: 48871
Use Nine-Patch .png images and first create either an image which represents one square or create one which represents your 20x10 grid - your choice. Do this so it looks right on an mdpi (medium dot-per-inch) screen.
Then you will need to create pre-scaled images for the other screen densities you intend to support. The one for ldpi will be 0.75 of the original, hdpi is 1.5x the size of the original and xhdpi is 2x the original.
You then put the images in the relevant drawable resource directory of your project - example /res/drawable-mdpi and /res/drawable-hdpi and so on.
Give the images exactly the same name for each density, e.g., square.png or grid.png. As long as those drawable directories contain an image of the same name, Android will find the right one for the right density and use it.
It's a long bit of documentation but you really need to read...
...particularly the part for Alternative Drawables.
Upvotes: 1