Game Development Reference
In-Depth Information
The rectangle is positioned at (100,100). Depending on the screen resolution, the distance to
the screen center will differ. The size of the rectangle is 100×100 pixels. On the bigger screen, it
takes up far less relative space than on the smaller screen.
The circle's position is again screen resolution independent, but its radius is not. Therefore, it
again takes up more relative space on the smaller screen than on the bigger one.
We already see that handling different screen resolutions might be a bit of a problem. It gets
even worse when we factor in different physical screen sizes. However, we'll try to solve that
issue in the next chapter. Just keep in mind that screen resolution and physical size matter.
Note The Canvas and Paint classes offer a lot more than what we just talked about. In fact, all
of the standard Android View s draw themselves with this API, so you can image that there's more
behind it. As always, check out the Android Developers site for more information.
Using Bitmaps
While making a game with basic shapes such as lines or circles is a possibility, it's not exactly
sexy. We want an awesome artist to create sprites and backgrounds and all that jazz for us,
which we can then load from PNG or JPEG files. Doing this on Android is extremely easy.
Loading and Examining Bitmaps
The Bitmap class will become our best friend. We load a bitmap from a file by using the
BitmapFactory singleton. As we store our images in the form of assets, let's see how we can
load an image from the assets/ directory:
InputStream inputStream = assetManager.open("bob.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
The Bitmap class itself has a couple of methods that are of interest to us. First, we want to get to
know a Bitmap instance's width and height in pixels:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
The next thing we might want to know is the color format of the Bitmap instance:
Bitmap.Config config = bitmap.getConfig();
Bitmap.Config is an enumeration with the following values:
ï?® Config.ALPHA_8
ï?® Config.ARGB_4444
ï?® Config.ARGB_8888
ï?® Config.RGB_565
 
Search WWH ::




Custom Search