0

我正在为黑莓设备构建一个应用程序,它将在主屏幕上显示一个代码。我进行了很多搜索,但找不到解决方案。

我曾尝试在固定间隔后更新背景图像,但这会使手机非常慢。我不是主题开发人员,但尝试过但又失败了。我想从我的应用程序中更新代码的文本。

请帮助我,我真的被困在这里。提前致谢

4

1 回答 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;
        }

}

快乐编码:)

于 2011-07-05T12:26:46.210 回答