Resizing a Bitmap on the Blackberry

The RIM Blackberry API makes it really simple to display bitmap images on the screen. Once you've added your image as a resource file the code is as simple as the following two lines:

 BitmapField bmp = new BitmapField(Bitmap.getBitmapResource("image.png"));

However, if you need to display a resized version of the image you'll soon notice that the Bitmap class doesn't have any methods for performing such an action. Don't worry though, the EncodedImage class does, and also provides a method to return the required Bitmap object. Below is the code to scale an image to half its original size and then add it to the current screen:

 EncodedImage ei = EncodedImage.getEncodedImageResource("image.png");
    ei.setScale(2); // divide size by this number
    BitmapField bmp = new BitmapField(ei.getBitmap());
Posted on 30 Jun 2008
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.