05-29-2012 07:55 AM
Hello,
I found a workaround for a transparent popup on any blackberry Mainscreen. I would like to share my code here.
The PopupScreen class is as follows:
/*************************************************
import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManag
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class PopupLoadingScreen extends PopupScreen {
Font fntPlain = Font.getDefault().derive(Font.PLAIN, 20);
private AnimatedGIFField anmtFldCycle = null;
private LabelField lblMessage = null;
private GIFEncodedImage gifImgCycle;
public PopupLoadingScreen(String text) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL
| VerticalFieldManager.VERTICAL_SCROLLBAR));
Background bg = BackgroundFactory.createSolidTransparentBackground
Color.WHITE, 0);
this.setBackground(bg);
Border border = BorderFactory.createRoundedBorder(new XYEdges(),
Border.STYLE_TRANSPARENT);
this.setBorder(border);
gifImgCycle = (GIFEncodedImage) GIFEncodedImage
.getEncodedImageResource("LoadingSpinner.gif");// greenloadingspinner.gif
anmtFldCycle = new AnimatedGIFField(gifImgCycle, Field.FIELD_HCENTER);
add(anmtFldCycle);
// lblMessage = new LabelField(text, Field.FIELD_HCENTER);
// lblMessage.setFont(Font.getDefault().derive(Font.P
// add(lblMessage);
}
// // This will Override the BACK button press of the PHONE
public boolean keyDown(int keycode, int status) {
if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
return true;
}
return super.keyDown(keycode, status);
}
/**
* This will Override the Close functionality in the Default BB menu on any
* screen
*/
public boolean onClose() {
// UiApplication.getUiApplication().requestBackground
return false;
}
}
/*************************ANIMATED GIF FIELD Class************************************/
package com.test;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
//A field that displays an animated GIF.
public class AnimatedGIFField extends BitmapField {
private GIFEncodedImage _image; // The image to draw.
private int _currentFrame; // The current frame in the animation sequence.
private AnimatorThread _animatorThread;
public AnimatedGIFField(GIFEncodedImage image) {
this(image, 0);
}
public AnimatedGIFField(GIFEncodedImage image, long style) {
// Call super to setup the field with the specified style.
// The image is passed in as well for the field to configure its
// required size.
super(image.getBitmap(), style);
// Store the image.
_image = image;
// Start the animation thread.
_animatorThread = new AnimatorThread(this);
_animatorThread.start();
}
protected void paint(Graphics graphics) {
// Call super.paint. This will draw the first background frame and
// handle any required focus drawing.
super.paint(graphics);
// Don't redraw the background if this is the first frame.
if (_currentFrame != 0) {
// Draw the animation frame.
graphics
.drawImage(_image.getFrameLeft(_currentFrame), _image
.getFrameTop(_currentFrame), _image
.getFrameWidth(_currentFrame), _image
.getFrameHeight(_currentFrame), _image,
_currentFrame, 0, 0);
}
}
// Stop the animation thread when the screen the field is on is
// popped off of the display stack.
protected void onUndisplay() {
_animatorThread.stop();
super.onUndisplay();
}
// A thread to handle the animation.
private class AnimatorThread extends Thread {
private AnimatedGIFField _theField;
private boolean _keepGoing = true;
private int _totalFrames; // The total number of frames in the image.
private int _loopCount; // The number of times the animation has looped
// (completed).
private int _totalLoops; // The number of times the animation should
// loop (set in the image).
public AnimatorThread(AnimatedGIFField theField) {
_theField = theField;
_totalFrames = _image.getFrameCount();
_totalLoops = _image.getIterations();
}
public synchronized void stop() {
_keepGoing = false;
}
public void run() {
while (_keepGoing) {
// Invalidate the field so that it is redrawn.
UiApplication.getUiApplication().invokeAndWait(ne
public void run() {
_theField.invalidate();
}
});
try {
// Sleep for the current frame delay before the next frame
// is drawn.
sleep(_image.getFrameDelay(_currentFrame) * 10);
} catch (InterruptedException iex) {
} // Couldn't sleep.
// Increment the frame.
++_currentFrame;
if (_currentFrame == _totalFrames) {
// Reset back to frame 0 if we have reached the end.
_currentFrame = 0;
++_loopCount;
// Check if the animation should continue.
if (_loopCount == _totalLoops) {
_keepGoing = false;
}
}
}
}
}
}
/*************************************************
This is how i set the Background of my Main class from where i show the Popup screen
Background bg = BackgroundFactory.createSolidTransparentBackground
Color.WHEAT, 0);
getMainManager().setBackground(bg);
For both the popup and the mainscreen class i have set background color as Color.WHITE and the GlobalAlpha is set to "0".
I push the PopupScreen class on click of simple Button. Actually its a Loading Screen with an GIF image in it. The link for the GIF images is given below
One can also try with a different GIF image.
Please suggest if any changes or improvements can be made in this for better performance
Thank you.
Solved! Go to Solution.
08-09-2012 02:38 AM - edited 08-09-2012 02:39 AM
Hi,
By adding the below line of code inside your popupscreen constructor you can get transperent pop up.
setBackground(BackgroundFactory.createSolidTranspa
Color.BLACK, 150));
setBorder(BorderFactory.createSimpleBorder(new XYEdges(),
Border.STYLE_TRANSPARENT));
Here i set translucant background you can change that as per your requirement
08-09-2012 03:02 AM
Ok.Thanks will add it to improve the code.
Thanks
08-09-2012 03:09 AM
If this helpful then like or vote my answer.
08-09-2012 03:26 AM
I had added this full code so that some one may be helpful in development. Thanks any way.
Have a nice day ![]()