Welcome!

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

Reply
Highlighted
Developer
Posts: 32
Registered: ‎03-11-2014
My Device: 8520
My Carrier: 8520
Accepted Solution

How to set a background image

Hy guys, I am trying to set a background image on a screen in Blackberry. I used this code but the image dousen't cover the full screen. Can somebody tell me what the problem is, or give me an example of how it's done?package mypackage;



public class BackgroundImage extends UiApplication
{
    private Bitmap backgroundBitmap;
    private Bitmap fieldBitmap;
    
    public static void main(String[] args)
    {
            BackgroundImage theApp = new BackgroundImage();
            theApp.enterEventDispatcher();
    }
    
    public BackgroundImage()
    {
        //The background image.
        backgroundBitmap = Bitmap.getBitmapResource("loginBackground.png");
        
        MainScreen mainScreen = new MainScreen();
        
        HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT){
            
            //Override the paint method to draw the background image.
            public void paint(Graphics graphics)
            {
                //Draw the background image and then call paint.
                graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
                super.paint(graphics);
            }            
            
        };
        
        //The LabelField will show up through the transparent image.
        LabelField labelField = new LabelField("This is a label");
        
        //A bitmap field with a transparent image.
        //The background image will show up through the transparent BitMapField image.
        BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png"));

        //Add the manager to the screen.
        mainScreen.add(horizontalFieldManager);
        
        //Add the fields to the manager.
        horizontalFieldManager.add(labelField);
        horizontalFieldManager.add(bitmapField);
        
        //Push the screen.
        pushScreen(mainScreen);
    }
}

 

Thanks in advance Smiley Happy

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to set a background image

you can scale the image with EncodedImage, but usually it is better to use an image with sufficient size
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 32
Registered: ‎03-11-2014
My Device: 8520
My Carrier: 8520

Re: How to set a background image

I tried with an image that covers the full screen of my computer, and it still dosen.t cover the screen in Backberry

Developer
Posts: 32
Registered: ‎03-11-2014
My Device: 8520
My Carrier: 8520

Re: How to set a background image

I got it, In the drawBitmap metohod i was using the sizes 240, 240. Those values are two low, I've set them to 500, 500 and it works properly.

 

Thanks simon_hain, you're answer helped me find the solution

Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to set a background image

you can use Display.getWidth and Display.getHeight to get the correct values.
Or use the setBackground method instead of overwriting paint
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter