Resizing a Bitmap on the Blackberry

June 30, 2008

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"));
	add(bmp);

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());
	add(bmp);

7 Comments »

  1. How would i incorporate this into the WebBitmapField ? I’m having a little trouble with it.

    Comment by Matthew — March 23, 2009 @ 11:34 pm

  2. Hi Matthew,

    You just need to add the line “bitmap.setScale(i);” before the setImage call. Line 28 in the last code block on http://www.coderholic.com/blackberry-webbitmapfield/

    Thanks, Ben

    Comment by Ben — April 12, 2009 @ 6:59 pm

  3. Hi all!

    And how could we resize depending on the size of the screen and not on a fixed size?
    I am new at BB dev and really mess at that point.

    Comment by blablabla — May 6, 2009 @ 11:20 am

  4. @blablabla: You can get the screen size with Display.getWidth() and Display.getHeight()

    Comment by Ben — May 6, 2009 @ 12:24 pm

  5. Hi,
    how would you go about tiling an image across the width of the screen (say for a background)

    Comment by Christophe — July 29, 2009 @ 2:59 pm

  6. how can i add image while coding,where i have keep the image,i have to make a folder seperately for this?

    Comment by aby — August 26, 2009 @ 10:59 am

  7. @aby – you need to add it as a resource, or you can get it from the web using my WebBitmapField: http://www.coderholic.com/blackberry-webbitmapfield/

    Comment by Ben — August 28, 2009 @ 8:26 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment