Reputation: 1230
I am new to BlackBerry Development (5.0). I have a little experience in Android Applications Development.
What I'm trying to do is fill an image on the whole screen (Horizontally)
Similar to what you could do in Android using fill_parent
in a layout file.
I've looked on some forums to find a solution, but didn't get a satisfactory one.
This is how I'm getting my image
Bitmap headerLogo = Bitmap.getBitmapResource("uperlogo.png");
BitmapField headerLogoField =
new BitmapField(headerLogo, BitmapField.USE_ALL_WIDTH | Field.FIELD_HCENTER);
setTitle(headerLogoField);
This code is giving me my header on top (as required) and in the center. I simply want this to stretch horizontally to cover all space.
Upvotes: 2
Views: 790
Reputation: 3674
It is possible to stretch the Bitmap
horizontally before creating the BitmapField
, and that will solve the problem. But stretching Bitmap
and using it as title will create problem for devices that supports screen rotation (e.g. Storm, Torch series). In that case you have to maintain two stretched Bitmap
instance, one for the portrait mode and other for landscape mode. And you also need to write some extra code for setting the appropriate Bitmap
depending on the orientation. If you don't want to do that then check following 2 approaches:
Using CustomBitmapField instance
A CustomBitmapField that can stretch Bitmap
horizontally can be used. Check the implementation.
class MyScreen extends MainScreen {
public MyScreen() {
Bitmap bm = Bitmap.getBitmapResource("uperlogo.png");
setTitle(new CustomBitmapField(bm));
}
class CustomBitmapField extends Field {
private Bitmap bmOriginal;
private Bitmap bm;
private int bmHeight;
public CustomBitmapField(Bitmap bm) {
this.bmOriginal = bm;
this.bmHeight = bm.getHeight();
}
protected void layout(int width, int height) {
bm = new Bitmap(width, bmHeight);
bmOriginal.scaleInto(bm, Bitmap.FILTER_BILINEAR);
setExtent(width, bmHeight);
}
protected void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, bm.getWidth(), bmHeight, bm, 0, 0);
}
}
}
Using Background instance
Background
object can solve the problem easily. If a Background
instance can be set to a HorizontalFieldManager
which will use all the width available to its, then in case of screen rotation it will take care of its size and background painting. And the Background
instance will itself take care of the streching of the supplied Bitmap
. Check the following code.
class MyScreen extends MainScreen {
public MyScreen() {
setTitle(getMyTitle());
}
private Field getMyTitle() {
// Logo.
Bitmap bm = Bitmap.getBitmapResource("uperlogo.png");
// Create a manager that contains only a dummy field that doesn't
// paint anything and has same height as the logo. Background of the
// manager will serve as the title.
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
Background bg = BackgroundFactory.createBitmapBackground(bm, Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT);
hfm.setBackground(bg);
hfm.add(new DummyField(bm.getHeight()));
return hfm;
}
// Implementation of a dummy field
class DummyField extends Field {
private int logoHeight;
public DummyField(int height) {
logoHeight = height;
}
protected void layout(int width, int height) {
setExtent(1, logoHeight);
}
protected void paint(Graphics graphics) {
}
}
}
Upvotes: 3