Simpler Solution

Replica Island has one advantage: it does all this stretching and scaling via OpenGL ES, which is hardware accelerated. So far we've only discussed how to draw to a Bitmap and a View via the Canvas class, which doesn't involve hardware acceleration on the GPU, but slow number-crunching on the CPU.

We'll therefore perform a simple trick: we'll create a framebuffer in the form of a Bitmap instance that has our target resolution. This way we don't have to worry about the actual screen resolution when designing our graphical assets or when rendering them via code. We just pretend that the screen resolution is the same on all devices. All our draw calls will target this "virtual" framebuffer Bitmap via a Canvas instance. When we're done rendering a frame of our game, we'll simply draw this framebuffer Bitmap to our SurfaceView via a call to the Canvas.drawBitmap() method, which allows us to draw a Bitmap stretched.

If we want to use the same technique as Replica Island, we just need to adjust the size of our framebuffer on the bigger axis (i.e., on the x-axis in landscape mode, and on the y-axis in portrait mode). We also have to make sure that we fill the extra pixels we get so there's no blank space.

0 0

Post a comment