我如何在黑莓使用J2ME显示滚动的文字像劳斯莱斯? [英] How can I display scroll text like marque in Blackberry using J2ME?

查看:96
本文介绍了我如何在黑莓使用J2ME显示滚动的文字像劳斯莱斯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示滚动像黑莓滚动字幕文本使用J2ME?该移动从左至右或垂直?
任何帮助会非常多AP preciated。

How can I display scroll like marquee text in Blackberry using J2ME? That moves from left to right or vertically? Any help will be very much appreciated.

推荐答案

它真的很容易显示。
检查下面的code。

its really easy to display. check the code below .

import java.util.Timer;
import java.util.TimerTask;

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;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class MarqueeApp extends UiApplication {

    public MarqueeApp() {
    pushScreen(new MarqueeScreen());
    }

    public static void main(String[] args) {
        (new MarqueeApp()).enterEventDispatcher();
    }
}

class MarqueeScreen extends MainScreen {
    public MarqueeScreen() {
        setTitle(new LabelField("hi"));
        MarqueeLabel testLabel1 = new MarqueeLabel("This is a marquee",
                Field.FOCUSABLE);
        add(testLabel1);

        MarqueeLabel testLabel2 = new MarqueeLabel("This is a long long " +
                "long long long long long long long long long long long " +
                "long long marquee", Field.FOCUSABLE);
        add(testLabel2);
    }
}

class MarqueeLabel extends LabelField {
    int currentChar = 0;
    String currentText = null;
    Font ourFont;
    private Timer _scrollTimer;
    private TimerTask _scrollTimerTask;

    public MarqueeLabel(String text, long style) {
        super(text, style);     
    }

    public void paint(Graphics graphics) {
        currentText = this.getText();
        if (currentChar < currentText.length()) {
            currentText = currentText.substring(currentChar);

        }
        graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, 200);
        super.paint(graphics);
    }

    public void layout(int width, int height) {
        ourFont = this.getFont();
        setExtent(500, ourFont.getHeight());
    }

    protected void onDisplay() {
        startScroll();
    }

    protected void onUnfocus() {
        startScroll();
    }

    private void startScroll() {
        // Start scrolling
        final String fullText = this.getText();
        if (_scrollTimer == null) {
            _scrollTimer = new Timer();
            _scrollTimerTask = new TimerTask() {
                public void run() {
                    currentChar = currentChar + 4;
                    if (currentChar > fullText.length()) {
                        currentChar = 0;
                    }
                    invalidate();
                }
            };
            _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 300);
        }
    }

    protected void onFocus(int direction) {
        if (_scrollTimer != null) {
            _scrollTimerTask.cancel();
            _scrollTimer.cancel();
            _scrollTimer = null;
            _scrollTimerTask = null;
        }
    }

    protected boolean navigationMovement(int dx, int dy, 
        int status, int time) {
        currentText = this.getText();
        int oldCurrentChar = currentChar;
        if (Math.abs(dx) > Math.abs(dy)) {
            // horizontal scroll
            if (dx > 0) {
                currentChar = Math.min(currentText.length() - 1,
                        currentChar + 1);
            } else if (dx < 0) {
                currentChar = Math.max(0, currentChar - 1);
            }
            if (oldCurrentChar != currentChar) {
                this.invalidate();
            }
            return true;
        } else {
            return super.navigationMovement(dx, dy, status, time);
        }
    }
}

这篇关于我如何在黑莓使用J2ME显示滚动的文字像劳斯莱斯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆