Search This Blog

Tuesday, 20 March 2012

How to resize an image programmatically in blackberry.

Hello friends,
                  so there is another issue i faced in my last app,as we know blackberry handsets have different screen size with different resolution,so how can we make our app automatically detect the size and re size our screen??? is what i can make it a function and call this every time or you just use it directly...                                                                        


    int oolddWidth;
    int oolddHeight;
    int ddispplayWidth;
    int ddispplayHeight;
    EncodedImage eih1 = EncodedImage.getEncodedImageResource("add2.png");
    oolddWidth = eih1.getWidth();
    oolddHeight = eih1.getHeight();
    ddispplayWidth = Display.getWidth()-40;//Declare the width you want for the image
    ddispplayHeight = 80;//Declare the height you want...
        int nnumeerator = net.rim.device.api.math.Fixed32.toFP(oolddWidth);
        int ddenoominator = net.rim.device.api.math.Fixed32.toFP(ddispplayWidth);
        int wwidtthScale = net.rim.device.api.math.Fixed32.div(nnumeerator, ddenoominator);
        nnumeerator = net.rim.device.api.math.Fixed32.toFP(oolddHeight);
        ddenoominator = net.rim.device.api.math.Fixed32.toFP(ddispplayHeight);
        int hheighhtScale = net.rim.device.api.math.Fixed32.div(nnumeerator, ddenoominator);
        EncodedImage newEih1 = eih1.scaleImage32(wwidtthScale, hheighhtScale);
        final Bitmap header1 = newEih1.getBitmap();//Bitmap header1 will give you a resized image..

The best part is no matter what the size of the image and resolution this will resize it to the width and height you want...hope it will be helpful for you people

No comments:

Post a Comment