我正在構建一個黑莓設備的應用程序,它將在主屏幕上顯示一個代碼。我搜查了很多,但找不到解決方案。黑莓主屏上的代碼
我已經嘗試更新固定間隔後的背景圖像,但這會使手機非常慢。我不是主題開發者,但嘗試過並失敗了。我想從我的應用程序中更新股票代碼的文本。
請幫助我,我真的被困在這裏。在此先感謝
我正在構建一個黑莓設備的應用程序,它將在主屏幕上顯示一個代碼。我搜查了很多,但找不到解決方案。黑莓主屏上的代碼
我已經嘗試更新固定間隔後的背景圖像,但這會使手機非常慢。我不是主題開發者,但嘗試過並失敗了。我想從我的應用程序中更新股票代碼的文本。
請幫助我,我真的被困在這裏。在此先感謝
下面的代碼可以給你更多洞悉代碼 只需在下面的類中創建一個對象並傳遞所需的參數即可。這會讓你的工作完成。我猜這是
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Display;
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.Graphics;
public class TickerField extends Field {
String text;
static final int screenWidth = Display.getWidth();
int offset;
private Timer timer = new Timer();
final int delay = 30;
private static int fontH = Font.getDefault().getHeight();
private int w;
private int h;
private int bgColor = Color.WHITE;
public TickerField(String text, int width, int height) {
this.text = text;
w = width;
h = height;
offset = w;
final int textWidth = Font.getDefault().getAdvance(text);
//schedule and start timertask
TimerTask timerTask = new TimerTask() {
public void run() {
offset--;
if (offset + textWidth == 0) {
offset = screenWidth;
}
invalidate();
}
};
timer.scheduleAtFixedRate(timerTask, delay, delay);
}
public TickerField(String text) {
this(text, screenWidth, fontH);
}
public TickerField(int width, int height) {
this("", width, height);
}
public TickerField() {
this("", screenWidth, fontH);
}
//set ticker text
public void setText(String text) {
this.text = text;
}
//get ticker text
public String getText() {
return text;
}
// implement layout to give specific arrangement to this field
// Invoke Math.min() to return the smaller of the user specified w and h,
// and the preferred width and height of the field.
protected void layout(int width, int height) {
width = Math.min(w, getPreferredWidth());
height = Math.min(h, getPreferredHeight());
setExtent(width, height);
}
// Implement the paint() to redraw the field with different
// offset controlled by timer task.That will give the
// ticker effect.
protected void paint(Graphics graphics) {
graphics.drawText(text, offset, 0);
}
public void setBgColor(int bgColor) {
this.bgColor = bgColor;
}
//Implement the paintBackground() method to change the
//background color of the field.
protected void paintBackground(Graphics g) {
g.setBackgroundColor(bgColor);
g.clear();
super.paintBackground(g);
}
// Implement getPreferredWidth() and getPreferredHeight(), using the
// screenWidth and font height to make sure that the ticker does not exceed
// the dimensions of the
// component.
public int getPreferredWidth() {
return screenWidth;
}
public int getPreferredHeight() {
return fontH;
}
}
編碼快樂:)
感謝您的答覆。但我不想讓我的應用程序完整顯示屏幕。我只是希望它在設備的主屏幕背景上添加股票。我想要一些類似BreakingNews應用程序的東西。看到[this](http://appworld.blackberry.com/webstore/content/41242?lang=en)和[this](http://crackberry.com/breakingnews-blackberry-enters-open-beta-get- Scrolling-news-widget-your-home-screen)爲BreakingNews的屏幕截圖。 –