On the BlackBerry to achieve text scrolling effect

Source: Internet
Author: User
Tags abs gettext

Bo Master Doll also wrote several BlackBerry development of small documents, quite practical, thanks.

Original:

http://doll.javaeye.com/blog/569457

LabelField TestLabel = new LabelField (
"I swear to God up in the sky I'll be your shelter if you cry cause your are my everything."
field.focusable) {
int Currentchar = 0;
String currenttext = null;
Font Ourfont;
Private Timer _scrolltimer;
Private TimerTask _scrolltimertask;

protected void Paint (Graphics Graphics) {
Currenttext = This.gettext ();
if (Currentchar < Currenttext.length ()) {
Currenttext = currenttext.substring (Currentchar);
}
Graphics.drawtext (currenttext, 0, 0, drawstyle.ellipsis, Display
. getwidth ());
}

protected void layout (int width, int height) {
Ourfont = This.getfont ();
SetExtent (Display.getwidth (), 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, 500, 500);
}
}

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);
}
}
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.