Create Transparent Mainscreen in Blackberry - blackberry

i want to create a type of MainScreen in my Blackberry app which should be Transparen/Translucent.
I tried with following code on MyCustomMainScreen's constructor but it still shows a white screen
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
this.setBackground(bg);
I have read in the forum and also tested it for popup screens and it works fine with them but fails with mainscreens.
Does anyone have any idea how to achieve this for MainScreen..?
-Big O

override the paint method like:
class M extends MainScreen
{
public M()
{
setBackground(BackgroundFactory.createSolidTransparentBackground(Color.ANTIQUEWHITE, 100));
LabelField l=new LabelField("hello");
add(l);
}
protected void paint(Graphics graphics)
{
super.subpaint(graphics);
}
}

Instead of
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
this.setBackground(bg);
maybe better
Background bg = BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50);
getMainManager().setBackground(bg);

I found the solution without overriding the paint method. You need to set the background full transparent for the Screen and translucent for the main manager:
// Full transparent background
setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
// Manager translucent background
getMainManager().setBackground(BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 50));

Related

Adding background image to blackberry app not working

i am new to blackberry development and want to add an image to a sample blackberry application. I have tried multiple tutorials, but the image is not displaying in the background
can any one kindly tell me what is the problem?
/**
* This class extends the UiApplication class, providing a graphical user
* interface.
*/
public class Diverse extends UiApplication {
private Bitmap backgroundBitmap;
private Bitmap fieldBitmap;
public static void main(String[] args) {
Diverse diverse = new Diverse();
diverse.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public Diverse() {
//The background image.
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);
}
};
//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 a screen onto the UI stack for rendering.
pushScreen(new DiverseScreen());
}
}
and DiverseScreen class is
package diverse;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class DiverseScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public DiverseScreen()
{
// Set the displayed title of the screen
setTitle("Diverse");
}
}
The problem is that you have set the background image for one screen, but you never displayed that screen. Then, you displayed a different screen that did not have a background image set.
First, it helps to understand the BlackBerry UI framework. It allows you to create a hierarchy of objects on screen. At the top level, you have a Screen (or subclass of Screen), and then inside of the Screen, you have Managers, and inside them are Fields. But, each level must be added to a container of some kind, and finally, the top-level Screen must be displayed with something like pushScreen().
See a little more description on this hierarchy in a recent answer here
In your situation, you should change this line
MainScreen mainScreen = new MainScreen();
to
DiverseScreen mainScreen = new DiverseScreen();
and then change this line
pushScreen(new DiverseScreen());
to
pushScreen(mainScreen);
since mainScreen is the instance where you added a horizontal field manager that draws your background image.

Blackberry VerticalfieldManager virtual scroll view size too large

Developing on the blackberry (OS 7.0) and I have an extended Vertical Field manager created as such:
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL){};
However, when I scroll the view, the virtual scroll view size appears way too big.
i.e, I can scroll quite alot further down than is needed and I cant work out why?
Any body any quick ideas? I do have a background image in there that is created as such:
public void paint(Graphics graphics)
{
Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg.png");
Bitmap newBackground = new Bitmap(Display.getWidth(), Display.getHeight());
backgroundBitmap.scaleInto(newBackground, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
graphics.clear();
graphics.drawBitmap(0, 0, Display.getWidth(),
Display.getHeight(), newBackground, 0, 0);
super.paint(graphics);
}
Please and thanks,
Burrows
You can redefine the method to define the height of the manager
protected void setExtent(int width, int height) {
super.setExtent(width, myHeight);
}
To not repeat the background image is necessary to redefine the following method, returning false
protected boolean isScrollCopyable() {
return false;
}
Another comment is that it is a bad practice to obtain an image from the paint method. What you are doing is every time you call the paint method is going to get this image.
It's best to get the image once and then use it.
public MyScreen() {
super(NO_VERTICAL_SCROLL);
I managed to fix the issue in my extension of MainScreen using 'NO_VERTICAL_SCROLL' style parameter
Combined with Rupak's suggestion of setting the Background in the constructor of my Vertical Field Manager rather than overriding paint (
https://gist.github.com/3248319)
everything seems Good now - thanks all for your help.
Burrows
No need to USE_ALL_HEIGHT in your constructor.
// instead use this
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL){};

BlackBerry textfield

I am trying to develop an application in which text is displayed on a background image.
Text must be displayed on an image and when the background image is changed text also must change.
To achive above requirement first you need to set Background image for your MainScreen for this there are more methods one is to override paint method of MainScreen.
Example code:
Bitmap screen1=Bitmap.getBitmapResource("Screen_1.jpg");
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(),screen1, 0, 0);
super.paint(graphics);
}
};
After setting background image for screen you need to add LabelField or RichTextField to screen.
Example:
LabelField lf=new LabelField("text",Field.USE_ALL_WIDTH|DrawStyle.HCENTER);
add(lf);
When you need to change Backgound image set.
screen1=Bitmap.getBitmapResource("you image.jpg");
lf.setText("your labelfield text");
invalidate();
Hope this will help you.

Image over a background Image in Blackberry

I want to set the position of one image over a background image. The position could be anywhere on the screen.
Can I have a sample code or a link or tutorial for that?
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.
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);
}
}

please tell me how i can paint the whole screen in a rimlet?

i am developing a blackberry app. i want to paint the screen. i have implemented this
VerticalFieldManager hfmBg = new VerticalFieldManager(Field.USE_ALL_HEIGHT )
{
protected void paint(Graphics g)
{
g.setBackgroundColor(Color.SILVER);
g.clear();
super.paint(g);
}//end of paint method
};//end of vertical field manager
but tell me is there any other approach to paint the whole screen?
You have the right idea - just put that paint method in your Screen (MainScreen or FullScreen) class instead of your Manager class. You may also have to add the following empty method to your Screen class to override themes that set the default colors:
protected void applyTheme() {
}

Resources