Search code examples
blackberryblackberry-simulatorblackberry-eclipse-plugin

Handling different screen resolutions in blackberry with single image


I have one PNG image and am applying it to all the screens as background. Its resolution is 360x480 because I started programming for 9800 simulator. I am developing for OS 6.0. When I test my app for 9700 simulator having screen resolution 480x360, the image covers only half the screen. I have researched and learnt about using Fixed32 and scale and I have applied it as well. But still the background seems to cause an issue. I have only one image in resource folder. How to handle this one image to apply suitably for all different blackberry devices


Solution

  • You have to resize the image based to screen width and height to fit to the screen you are are running.

    For this the following methods will use.

    public static EncodedImage sizeImage(EncodedImage image, int width, 
                  int height) {
                  EncodedImage result = null;
    
                  int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
                  int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
    
                  int requiredWidthFixed32 = Fixed32.toFP(width);
                  int requiredHeightFixed32 = Fixed32.toFP(height);
    
                  int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
                    requiredWidthFixed32);
                  int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
                    requiredHeightFixed32);
    
                  result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
                  return result;
                 }
    

    After resizeing use below method for cropping.

    public static Bitmap cropBitmap(Bitmap original, int width, int height) {
            Bitmap bmp = new Bitmap(width, height);
    
             int x = (original.getWidth() / 2) - (width / 2); // Center the new size by width
            int y = (original.getHeight() / 2) - (height / 2); // Center the new size by height
            int[] argb = new int[width * height]; 
            original.getARGB(argb, 0, width, x, y, width, height);
            bmp.setARGB(argb, 0, width, 0, 0, width, height);
            return bmp;
    

    Create bitmap image as follow.

     EncodedImage  eImage = EncodedImage.getEncodedImageResource("img/new.png" );
     EncodedImage bitimage=sizeImage(eImage,Display.getWidth(),Display.getHeight());
    
     Bitmap image=cropBitmap(bitimage.getBitmap(),Display.getWidth(),Display.getHeight());
    

    pass above bitmap to you manager.

    Now set the returned bitmap as background to screen.It worked for me.Hope this will help to you.