how to resize images in Black Berry according to the width and height of the display screen?

StackOverflow https://stackoverflow.com/questions/8663955

  •  08-04-2021
  •  | 
  •  

Question

How can I re-size the image stored in resource folder using code according to the display width and height of blackberry screen?

Was it helpful?

Solution

Firstly create a encoded image like this

 EncodedImage ei = EncodedImage.getEncodedImageResource("res/helpscreen.png");   

then pass this encoded image to the below function

EncodedImage ei1= scaleImage(ei,reqWidth,requiredHeight);


 public EncodedImage scaleImage(EncodedImage source, int requiredWidth, int requiredHeight) 
    {  
        int currentWidthFixed32 = Fixed32.toFP(source.getWidth());  
        int requiredWidthFixed32 = Fixed32.toFP(requiredWidth);  
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);  
        int currentHeightFixed32 = Fixed32.toFP(source.getHeight());  
        int requiredHeightFixed32 = Fixed32.toFP(requiredHeight);  
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);  
        return source.scaleImage32(scaleXFixed32, scaleYFixed32);  
    } 

this will give you a encoded image. then convert it to Bitmap using

        BitmapField logoBitmap = new BitmapField(ei1.getBitmap());   

OTHER TIPS

To scale a Bitmap image try Bitmap.scaleInto(...). API Link.

Bitmap targetBm = new Bitmap(Display.getWidth(), Display.getHeight());
srcBitmap.scaleInto(targetBm, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_STRETCH);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top