I have been working on an application that has a number of navigation buttons. This buttons must always be displayed and I want them to take up about half the width of the screen. This is a simple problem when you have a single width to deal with, but I recently needed to port the application from the Bold to the Pearl handset.
I wanted to keep the same images but needed to resize based on the display’s width. There are trivial solutions easily found on the web (where the image’s size is cut in half) but nothing to set an arbitrary size. So here is my solution (so that I can remember it at a later time).
I know this is an obvious thing to do, but I am continually searching through old projects or the web to remember the details on how to accomplish this task.
The key thing to remember is that the value for the scaleImage is the reciprical of what you would expect. So if the scaled image is one half (1/2) of the original then your scale factor is (2/1).
protected Bitmap scaleBitmap(EncodedImage ei, int newXSize, int newYSize)
int xScale = 0;
int yScale = 0;
int xSize = ei.getWidth();
int ySize = ei.getHeight();
xScale = Fixed32.div(xSize,newXSize);
yScale = Fixed32.div(ySize,newYSize);
return ei.scaleImage32(xScale, yScale).getBitmap();
You can add some refinements:
- keep the aspect ratio: just decide what dimension is the limiting factor for display and then apply that scale to both dimensions.
- keep images a consistent size: put the size into your class that defines your presentation style