Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 28
Registered: ‎09-07-2010
My Device: Not Specified
Accepted Solution

Bitmap drawing



I'm a blackberry novice experimenting with a custom ui control . I'd like to know if it is possible to draw a bitmap at a x and y position and stretch that bitmap for a desired width and heigth.  Also I'm interested to know  if   I could  modify the color of the bitmap pixels. Snippets would be great ! Thanks !

Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Bitmap drawing


it is indeed possible to draw a bitmap at a specific location, as well as scale it while doing so.  However, the full explanation is way beyond the scope of a single message.  I'll assume you are writing a RIM application, not a MIDlet.


1) net.rim.device.api.ui.Field and all its descendants have a protected method paint(net.rim.device.api.ui.Graphics). You custom draw inside the FIeld by overriding this method. There is also an under-documented but working protected void paintBackground(net.rim.device.api.ui.Graphics) method.

2) Graphics class has all kinds of bitmap drawing.  The main one (which does not scale Bitmaps) is drawBitmap.  Still, it lets you specify the position, width and height as well the point of origin inside the Bitmap.

3) You need Graphics.drawTexturedPath for bitmap scaling. This method, however, is not for the faint of heart. Search this forum for drawTexturedPath to find some info (and a few rants).

4) A good alternative, which also makes it easier to scale images, is Graphics.drawImage method. If you look at EncodedImage class, you will find that it has scaleImage32 method. You will need to learn about net.rim.device.api.math.Fixed32 class to scale images using either EncodedImage.scaleImage32 or Graphics.drawTexturedPath.


Above all, don't be intimidated by all this info. If you are persistent enough you'll get through!


Good luck!

please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Bitmap drawing

Use Graphics.drawTexturedPath().

Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Bitmap drawing

Following link solve your scale of Bitmap problem




and following function change the color of Bitmap pixel to set the RBG value.

This function return the new Bitmap with different color.



public static Bitmap generateHitBitmap(Bitmap passBitmap) {
	     Bitmap retBitmap;  // Altered, tinted bitmap being returned
	     int[] argbData; // Array holding the ARGB data
	     int redData;  // The red value of a pixel

	     // Create a new, empty bitmap with the same dimensions
	     retBitmap = new Bitmap(passBitmap.getType(), passBitmap.getWidth(), passBitmap.getHeight());

	     // Prepare the ARGB array
	     argbData = new int[passBitmap.getWidth() * passBitmap.getHeight()];

	     // Grab the ARGB data
	     passBitmap.getARGB(argbData, 0, passBitmap.getWidth(), 0, 0, passBitmap.getWidth(), passBitmap.getHeight());

	     // Loop through each pixel in the array
	     for (int lcv = 0 ; lcv < argbData.length ; lcv++) {
	        // Get the red data by masking out the 2nd byte
	        redData = (argbData[lcv] & 0x00FF0000) >> 16;

	        // Increase the red value by 80 (maximum of 255)
	        redData += 80;
	        if (redData > 255) redData = 255;

	        // Shift it back to the right place, and set it back into array
	        redData = redData << 16;
	        argbData[lcv] = (argbData[lcv] & 0xFF00FFFF) + redData;

	     //  Set the return Bitmap to use this altered ARGB array
	     retBitmap.setARGB(argbData, 0, passBitmap.getWidth(), 0, 0, passBitmap.getWidth(), passBitmap.getHeight());

	     return retBitmap;



Vivek Kumar Srivastava


Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Bitmap drawing

[ Edited ]

Thanks for this Aman_vivek - good stuff.


Just one minor thing.  Given the size of data typically involved in a Bitmap, can I just recommend that the processing shown that changes the color be changed to process a line at a time, which means the processing only requires an ARGB buffer (argbData) for one line rather than the whole Bitmap. It is possible to getARGB and setARGB to process in this way.