我正在为Blackberry设备构建一个应用程序,它将在主屏幕上显示一个自动收报机。我搜索了很多但找不到解决方案。
我已尝试在固定间隔后更新背景图像,但这会使手机变得非常慢。我不是主题开发人员,但尝试过它并再次失败。我想从我的应用程序中更新自动收报机的文本。
请帮助我,我真的被困在这里。提前致谢
答案 0 :(得分:0)
答案 1 :(得分:0)
下面的代码可以让您更深入地了解Ticker 只需在下面的类中创建一个对象并传递所需的参数即可。这将使你的工作完成。我猜:)
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;
}
}
快乐编码:)