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

Use a background image in application screens

by BlackBerry Development Advisor on ‎02-16-2010 03:07 PM - edited on ‎11-08-2010 04:29 PM by BlackBerry Development Advisor (Retired) (22,906 Views)

Summary

 

This article applies to the following:

  • BlackBerry® smartphones based on Java® technology

This article includes the following topics:

  • Using backgrounds
  • Identifying the types of backgrounds
  • Using BackgroundFactory

Description

 

To embellish your application, you may want to add a background image to a screen. Use the BlackBerry application programming interfaces (APIs) and the following few lines of code.

 

Note: The background image is applied to the first, or lowest level manager and is then added to a screen by overriding this manager's paint method.

 

 

Bitmap backgroundBitmap = Bitmap.getBitmapResource("background.png");

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 super.paint
//to paint the rest of the screen.
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(),
backgroundBitmap, 0, 0);
super.paint(graphics);
}
};

 

 

For a sample workspace that applies a background image to a screen, download the backgroundImage.zip file.

 

With BlackBerry® Device Software 4.6 and later, backgrounds can be added to any type of field, including managers and screens without having to override the paint method.

You can create any of the following types of backgrounds:

  • Solid color
  • Transparent
  • Linear gradient
  • Bitmap

To use background images, you must create a Background object using net.rim.device.api.ui.decor.BackgroundFactory as in the following code:

 

 

Background bg = BackgroundFactory.createSolidBackground(int colour);
Background bg = BackgroundFactory.createSolidTransparentBackground(int colour, int alpha);
Background bg = BackgroundFactory.createLinearGradientBackground(int color top left, int color top right, int color bottom right, int bottom left);
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap);
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap, int positionX, int positionY, int repeat);

 

 

where

  • colour is the value 0x00RRGGBB (red, green, blue)
  • alpha is the transparency level (from 0: clear to 255: opaque)
  • Bitmap is an image usually created from the file as in the following example:

    Bitmap bm = Bitmap.getBitmapResource("sample.png");

  • positionX is the horizontal bitmap anchoring using any of the following positions:
    • POSITION_X_LEFT
    • POSITION_X_CENTER
    • POSITION_X_RIGHT
  • positionY is the vertical bitmap anchoring using any of the following positions:
    • POSITION_Y_LEFT
    • POSITION_Y_CENTER
    • POSITION_Y_RIGHT
  • repeat is the tiling option that can be any following values:
    • REPEAT_NONE
    • REPEAT_HORIZONTAL
    • REPEAT_VERTICAL
    • REPEAT_BOTH
    • REPEAT_SCALE_TO_FIT

To assign background to the specific field, manager or screen, the following is used:

  • field.setBackground(Background bg);
  • field.setBackground(int visual, Background bg);
  • field.setVisualState(int visual);

where

  • bg is the background created using BackgroundFactory or retrieved using the following:
    • field.getBackground();
    • field.getBackground(int visual);
  • visual is the visual state of the field, such as the following:
    • VISUAL_STATE_NORMAL
    • VISUAL_STATE_ACTIVE
    • VISUAL_STATE_FOCUS
    • VISUAL_STATE_DISABLED
    • VISUAL_STATE_DISABLED_FOCUS

Additional Information

 

If you want to create a transparent screen, you must set the transparent background to both the screen and its manager.

Comments
by Developer on ‎09-29-2010 04:36 PM

In pre-4.6 code, painting the background is OK as long as people use non-scrolling managers.  If, for some reason, a developer adds HORIZONTAL_SCROLL and/or VERTICAL_SCROLL to the style bits and the manager scrolls, the background will scroll along with it.

 

A more robust solution would be

//Draw the background image and then call super.paint
//to paint the rest of the screen.
graphics.drawBitmap(getHorizontalScroll(), getVerticalScroll(), graphics.getScreenWidth(), graphics.getScreenHeight(), backgroundBitmap, 0, 0);
super.paint(graphics);


This way the background image will stay "docked".

by Developer on ‎10-05-2010 08:39 AM

graphics.getScreenWidth and getSreenHeight are deprecated. Use Display.getWidth() and Display.getHeight() instead.

by Developer on ‎10-14-2010 05:06 PM

Yes, that's true.  I've just left those alone since they do not illustrate my point of non-scrolling background.

 

In fact, the best values for width and height would be either

    getVisibleWidth(), getVisibleHeight()

or

    backgroundBitmap.getWidth(), backgroundBitmap.getHeight()

Users Online
Currently online: 17 members 936 guests
Please welcome our newest community members: