开发者

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 360x48开发者_StackOverflow社区0 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


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.


To set your image with required width and height use Bitmap scale; And if you want to put your image as Background then put the bitmap image to VerticalFieldManager like this: For all devices it works:

Bitmap bit=Bitmap.getBitmapResource("background.png");
Bitmap scalingBitmap=new Bitmap(Device.getWidth(),Device.getHeight());  //here you can give any size.Then it sets the image according to your required width and height;
bit.scaleInto(scalingBitmap, Bitmap.FILTER_LANCZOS);  //This is important;
VerticalFieldManager mainVertical=new VerticalFieldManager()
{
     //Here Device.getWidth() and Device.getHeight(); These two are your Blackberry Device width and height;(According your 9800 it takes 480x360)
    protected void paint(Graphics g) 
    {
        g.clear();
        g.drawBitmap(0, 0, Device.getWidth(),Device.getHeight(), scalingBitmap, 0, 0); 
        super.paint(g);
    }
    protected void sublayout(int maxWidth, int maxHeight) 
    {               
        super.sublayout(Device.getWidth(),Device.getHeight());
        setExtent(Device.getWidth(),Device.getHeight());
    }
};
add(mainVertical);

This is enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜