In Blackberry we can just use a custom button where it will have all the hover effects and also the image label just below the button Image.
import java.util.Timer;
import java.util.TimerTask;
import onepoint.blackberry.gfk.utility.all.FontDetails;
import onepoint.blackberry.gfk.utility.all.ImageResigeUtility;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class ScrollHomeButton extends Field implements DrawStyle {
private Bitmap _currentPicture;
private Bitmap _onPicture;
private Bitmap _offPicture;
private int width;
private int height;
private int text_off_sel, text_on_sel, textcolor;
private String labeldata;
private int backgroundColour;
private int highlightColour;
private int colorvalue;
FontDetails fontd;
String text;
final int screenWidth = Display.getWidth() / 3;
int offset = screenWidth;
Timer timer = new Timer();
final int delay = 30;
private int fontsize;
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public ScrollHomeButton(String onImage, String offImage, String label,
int width, int height, int fontsize1, int off_sel, int on_sel,
int offcolor, int oncolor) {
super();
_offPicture = Bitmap.getBitmapResource(onImage);
_onPicture = Bitmap.getBitmapResource(offImage);
_currentPicture = _offPicture;
labeldata = label;
this.width = width;
this.height = height;
this.fontsize = fontsize1;
this.backgroundColour = off_sel;
this.highlightColour = on_sel;
this.text_off_sel = offcolor;
this.text_on_sel = oncolor;
this.colorvalue = off_sel;
textcolor = offcolor;
fontd = new FontDetails();
this.setFont(fontd.retrieveFont(fontsize));
this.text = label;
final int width_ = Font.getDefault().getAdvance(text);
TimerTask timerTask = new TimerTask() {
public void run() {
offset--;
if (offset + width_ == 0) {
offset = screenWidth;
}
invalidate();
}
};
timer.scheduleAtFixedRate(timerTask, delay, delay);
}
public int getPreferredHeight() {
return height;
}
public int getPreferredWidth() {
return width;
}
public boolean isFocusable() {
return true;
}
// Override function to switch picture
protected void onFocus(int direction) {
colorvalue = highlightColour;
_currentPicture = _onPicture;
textcolor = text_on_sel;
invalidate();
}
// Override function to switch picture
protected void onUnfocus() {
colorvalue = backgroundColour;
_currentPicture = _offPicture;
textcolor = text_off_sel;
invalidate();
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
// update the fieldchange
protected void fieldChangeNotify(int context) {
try {
this.getChangeListener().fieldChanged(this, context);
} catch (Exception exception) {
}
}
//
protected void paint(Graphics graphics) {
graphics.clear();
graphics.setColor(colorvalue);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.drawBitmap(
(getPreferredWidth() - _currentPicture.getWidth()) / 2,
(getPreferredHeight() - _currentPicture.getHeight()) / 8,
_currentPicture.getWidth(), _currentPicture.getHeight(),
_currentPicture, 0, 0);
graphics.setColor(Color.WHITE);
String label = labeldata;
int x = (getPreferredWidth() - getFont().getAdvance(label)) >> 1;
int y = (getPreferredHeight() - getFont().getHeight()) >> 1;
graphics.setColor(textcolor);
if (text.length() > 10) {
// graphics.setFont(fontd.retrieveFont(fontsize));
graphics.drawText(text, offset, y + getFont().getHeight() - 2);
} else {
// graphics.setFont(fontd.retrieveFont(fontsize));
graphics.drawText(label, x, y + getFont().getHeight() - 2);
}
Bitmap temp = Bitmap.getBitmapResource("menu_div.png");
ImageResigeUtility imgsrc = new ImageResigeUtility();
Bitmap div = imgsrc.createBitmap("menu_div.png", temp.getWidth(),
height);
graphics.drawBitmap(0, 0, div.getWidth() - 1, height, div, 0, 0);
graphics.drawBitmap(this.getWidth() - 1, 0, div.getWidth() - 1, height,
div, 0, 0);
}
// Listen for navigation Click
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
}
Good work..but where is the class FontDetails ?
ReplyDeleteNever mind ..I've found it.
ReplyDelete