EncodedImage – How to Resize

Posted on November 2, 2009. Filed under: blackberry, multiple devices, software development | Tags: , , , |


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:

  1. keep the aspect ratio: just decide what dimension is the limiting factor for display and then apply that scale to both dimensions.
  2. keep images a consistent size: put the size into your class that defines your presentation style

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: