Drawing Bitmap as MainScreen Background. How to Stretch Image? - blackberry

I want the background image to stretch all screen. I simply created a VerticalFieldManager and modified paint method.
verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH
| VerticalFieldManager.NO_HORIZONTAL_SCROLLBAR) {
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(),
backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
And when i add a ListField to the verticalFieldManager and scroll, background image is not repaint. How can i repaint background when scroll?

Create EncodedImage from your Bitmap, or even directly extract EncodedImage from resources - EncodedImage.getEncodedImageResource("res/bg.png").
Then use this to scale it:
public static EncodedImage resize(EncodedImage eImage, int toWidth,
int toHeight, boolean keepAspectRatio) {
int scaleX = Fixed32.div(
Fixed32.toFP(eImage.getWidth()),
Fixed32.toFP(toWidth)
);
int scaleY = Fixed32.div(
Fixed32.toFP(eImage.getHeight()),
Fixed32.toFP(toHeight)
);
if (keepAspectRatio) {
int scale = (scaleX > scaleY) ? scaleX : scaleY;
return eImage.scaleImage32(scale, scale);
} else {
return eImage.scaleImage32(scaleX, scaleY);
}
}
Then you may draw the EncodedImage on the Graphics object:
graphics.drawImage(
0, 0,
encodedImage.getScaledWidth(), ncodedImage.getScaledHeight(),
encodedImage, 0, 0, 0
);

Related

Blackberry add border to image when focus

I'm trying to make touchable image which can focus when I scroll it by touchable button on device this is my code for touchable image:
class TouchBitmapField extends BitmapField {
Bitmap bitmap;
boolean second;
int width;
public TouchBitmapField(Bitmap startBitmap, long style, int width, boolean second) {
super(startBitmap, style);
this.second = second;
bitmap = startBitmap;
this.width = width;
}
protected void drawFocus(Graphics g, boolean on){
g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true );
if(on){
drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, on, width-(bitmap.getWidth()/2), 0, bitmap.getWidth(), bitmap.getHeight());
}
paint(g);
}
protected boolean touchEvent(TouchEvent message) {
if (TouchEvent.CLICK == message.getEvent()) {
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
}
But the problem is that only transparent part of image can focus and I'm resising this Bitmap before create TouchBitmapField and as you maybe know resising bitmap remove transparent part and replace it with black. I tried this class http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/ to resize image but I have 25 images on my screen and it's inefficient. Any idea how to make border on image? Any efficient way to draw border in drawFocus(Graphics g, boolean on) method?
The reason why you can only focus the transparent parts of an image is because (as far as I know) drawFocus is fired before paint, causing the bitmap to be drawn last.
I think this solution is what you're looking for. If you want something other than a solid colour as the border, you can play around with the BorderFactory.
class TouchBitmapField extends BitmapField
{
Bitmap bitmap;
Border defaultBorder;
Border focusBorder;
public TouchBitmapField(Bitmap startBitmap, long style)
{
super(startBitmap, style | FOCUSABLE);
bitmap = startBitmap;
XYEdges thickness = new XYEdges(5, 5, 5, 5);
XYEdges colours = new XYEdges(0xff0000, 0xff0000, 0xff0000, 0xff0000);
XYEdges alpha = new XYEdges(0, 0, 0, 0);
// Transparent border used by default, so that adding the focus border does not resize the view
defaultBorder = BorderFactory.createSimpleBorder(thickness, colours, alpha, Border.STYLE_SOLID);
focusBorder = BorderFactory.createSimpleBorder(thickness, colours, Border.STYLE_SOLID);
setBorder(defaultBorder);
}
protected void drawFocus(Graphics graphics, boolean on)
{
// Override default blue highlight
}
protected void onFocus(int direction)
{
super.onFocus(direction);
setBorder(focusBorder);
}
protected void onUnfocus()
{
super.onUnfocus();
setBorder(defaultBorder);
}
}
If you want your border to be overlapping your image, you can override your paint method and draw as you want.
protected void paint(Graphics graphics)
{
super.paint(graphics);
if(isFocus())
{
int tempCol = graphics.getColor();
int tempAlpha = graphics.getGlobalAlpha();
graphics.setColor(0xff0000);
graphics.setGlobalAlpha(128);
for(int i = 0; i < 5; i++)
{
graphics.drawRect(i, i, getWidth() - i * 2, getHeight() - i * 2);
}
// Reset back to original state
graphics.setColor(tempCol);
graphics.setGlobalAlpha(tempAlpha);
}
}

Set width and height on BlackBerry BitmapField

How do I set width and height of a BitmapField in BlackBerry? The following code works, but I can't show the entire image. The display shows only some portion of my image.
BitmapField bmp = new BitmapField(connectServerForImage(ImageUrl)) {
protected void layout(int width, int height)
{
setExtent(80, 70);
}
};
I assume you're saying that your BitmapField is showing the image you retrieve cropped?
And you would like it to scale, perhaps scaled to fit the size of your BitmapField? There's many ways to do that, but one is to resize the image after downloading, before giving it to the BitmapField:
From this answer on resizing a Bitmap
public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
Bitmap newImage = new Bitmap(newWidth, newHeight);
originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
return newImage;
}
(Note that the link I provided has an alternate method for resizing images, if you have to support BlackBerry OS < 5.0).
Then, use
Bitmap img = resizeImage(connectServerForImage(ImageUrl), 80, 70);
BitmapField bmp = new BitmapField(img) {
protected void layout(int width, int height)
{
setExtent(80, 70);
}
};
However, I believe that if you actually set the size of the Bitmap img to 80x70, then there's no need to also set the extent of the BitmapField to 80x70. It should default to the size of the image you give it. So, you could simplify the code to:
BitmapField bmp = new BitmapField(img);
int prefWidth=(30*bitmap.getWidth())/bitmap.getHeight();
Bitmap temp=resizeBitmap(bitmap,prefWidth,30);
where
private Bitmap resizeBitmap(Bitmap image, int width, int height)
{
int rgb[] = new int[image.getWidth()*image.getHeight()];
image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
This support bb os 4.5

how to set an image as background in blackberry

I was able to add the background only to the fields that were added in the screen with the below code:
VerticalFieldManager manager = new VerticalFieldManager();
Bitmap image = Bitmap.getBitmapResource("Penguins.jpg");
Background bg = BackgroundFactory.createBitmapBackground(image);
manager.setBackground(bg);
manager.add(new LabelField("HEll"));
add(manager);</code>
the output screen is like below:
How will get the image filled with whole screen, with the fields visible on top of it. Thank you
Bitmap background = Bitmap.getBitmapResource("background.png");
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public void paint(Graphics g) {
g.drawBitmap(0, 0, "Device Width", "Device Height",
background, 0, 0);
super.paint(g);
}
};
vfm.add(new LabelField("HEll"));
add(vfm);
You can also use this code to set the background image in Blackberry, But this works only 5.0 or above.
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap);
// OR
Background bg = BackgroundFactory.createBitmapBackground(Bitmap bitmap, int positionX, int positionY, int repeat);
this.getMainManager().setBackground(bg);

How do you set the background image in a BlackBerry application using Java?

how i set background image in java blackberry simulator ?
backgroundBitmap = Bitmap.getBitmapResource("background.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);
}
};
try this blog post:
How to set Background Image in Blackberry
Or this support forum thread:
drawing bitmap in Mainscreen background
Bitmap bitmapOrig = Bitmap.getBitmapResource("ICON.png");
// Create a Bitmap of arbitrary size
int scaledX = 360;
int scaledY = 415;
Bitmap bitmapScaled = new Bitmap(scaledX, scaledY);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_LANCZOS);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled , Field.FOCUSABLE);
homeScreen.add(bitmapFieldScaled2);
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);
}
protected void sublayout(int maxWidth, int maxHeight){
super.sublayout(240,240);
setExtent(240,240);
}
};

Status buttons gets hidden by overridding paint method in BlackBerry

Need lil help regarding paint method in MainScreen. Using the code above, I was trying to render some lists. It works fine. But it hides all my status bar icons i added using setStatus() method. When I remove the paint method , my buttons in status bar shows up. Also I have tried using setRe g.pushRegion() , but no luck.
Here is sample code :
private void addStatusBar(){
manager = new HorizontalFieldManager();
manager.add(new BitmapField(ImageUtility.loadBitMap("ask.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("experts.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("search.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("my_profile.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("groups.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("analitics.jpg")));
setStatus(manager);
}
protected void paint(Graphics graphics) {
//graphics.pushRegion(new XYRect(0, 0, getPreferredWidth(), getPreferredHeight()));
Bitmap image = ImageUtility.loadBitMap("header2.jpg");
graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
for(int i =0; i < 5; i++ ){
fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
}
}
Have you tried setting the "y" offset for the graphics.drawBitmap() call to the height of the status bar?
you need to call super.paint Otherwise the status is never painted.
protected void paint(Graphics graphics) {
Bitmap image = ImageUtility.loadBitMap("header2.jpg");
graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
for(int i =0; i < 5; i++ ){
fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
}
super.paint(graphics); }

Resources