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: 74
Registered: ‎03-04-2012
My Device: developer
My Carrier: developer

How to crop image from center..

Hey hi all,


I am facing a problem. The problem is I have image coming from server and I want to show them in a grid view like an album gallery images that I am getting from server are of resolution 137x87 when I resize these image to size 80x80 they get distorted so is there any way to crop image from center so that I can show image without any resizing 




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

Re: How to crop image from center..

You can do this by converting the image to a Bitmap, extracting the ARG values into an array, then create a new smaller array with the pixels you want, then create a new Bitmap with this array.  The API documentation for this is enough to give you the detail I suspect.


Alternatively you can create a new Bitmap, and paint the larger Bitmap into the new Bitmap at an offset and limiting the size to match your requirement, then use the new Bitmap.


But the approach I would use is to just paint the 'centre' of the image.  If you look the Graphic drawBitmap parameters, you will see that you tell the draw method where to start on the image (the last two parameters) and the height and width of the image to be drawn.  So you can calculate mathematically the offset (x and y) you want to centre the image and set the width and height to 80, then you will display just the central bit of the image that you want. 


How you use this approach depends on the Field you are painting into.  if this was a BitmapField, then you will add the Image as is, and override layout, and setPreferredWidth and setPreferredHeight to return the sizes you want.  The override paint to centre the image as above.


Hope this makes sense.