views:

792

answers:

2

Hi

i am new to blackberry, i want to put an image over a Background image. and i want to set the position of the image. position should be any where on the screen. can i have a sample code or any link or tutorial for that.

thanks a lot

A: 

Here's how I do it:

This works in 4.6.0 and later because of BackgroundFactory

// Create the background image and the image field to put on top
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource(bgImgPath);
Bitmap bmp = Bitmap.getBitmapResource(imgPath);
BitmapField imgField = new BitmapField(bmp);
// Create the field manager
VerticalFieldManager manager = new VerticalFieldManager()
{
  // Overide the sublayout of the field manager to set the position of
  // the image directly
  protected void sublayout(int width, int height)
  {
     setPositionChild(imgField, positionX, positionY)
     setExtent(width, height)
  }
};
// Set the background of the field manager
manager.setBackground(bg);
// add the bitmap field to the field manager
manager.add(imgField);
// add the field manager to the screen
add(manager);

For multiple images you can make a layout manager class and use that position all your images where you want them using similar techniques. There's a tutorial for making and using a layout manager, I'll try and dig it up and post it back here.

If your using 4.5.0 or earlier, I use a layout manager and just add the background image like any other image but add it first so it draws on the bottom.

Like I said I'll try and find that tutorial for the Layout Manager.

Matt Swanson
thanks a lot for the sample code. m waiting for the tutorial as i am developing the application on 4.5.0 version
Master
Here's one: http://docs.blackberry.com/en/developers/deliverables/1180/development.pdf#24It's on page 24 of the pdf.That whole manual is from 4.5.0 so it should all work for you.
Matt Swanson
A: 

You can create a class that extends Manager class Here you can specify the background image as well as you can position the other image at a position you want

class Test extends MainScreen
 {
   Test()
    {
      super();
      Bitmap bmp = Bitmap.getBitmapResource("image1.png");
      BitmapField bmpf = new BitmapField(bmp);
      Mymanager obj = new Mymanager();
      obj.add(bmpf);
    }
 }

class Mymanager extends Manager
{
final Bitmap background = Bitmap.getBitmapResource("back.png");
  protected void paint(Graphics g)
   {
     g.drawrect(0,0,background.getWidth,background.getheight,background,0,0);
   }
  protected void sublayout(int width, int height)
  {
    Field field = getfield(0);
    layoutchild(field,100,100);
    setPositionchild(field,20,10);  

    setExtent(Display.getWidth,Display.getHeight);
  }
}
SWATI