Tuesday, March 6, 2012

Android Note: Draw once and scale to fit different screen size

When develops Game Apps, how to draw the UI to fit all devices with different screen size may be a problem.

After study related document, I find one simple way is to draw all the component on a prepared bitmap, and then scale it according to target screen ratio.

In EasyMatch App, the bitmap size is 480x800, all components are drawn on it. Before showing the content on the phone's screen, use Matrix to compute the scale factor. The code snippet looked like:
float scaleWidth = screenWidth/480;
float scaleHeight = screenHeight/800;
Bitmap preparedBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);

// draw the components on the preparedBitmap

// then scale it
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
targetScreenCanvas.drawBitmap(preparedBitmap, matrix, null);

No comments:

Post a Comment