Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch
Accepted Solution

Animated LabelField

Is there an easy way to create a LabelFiled with sliding effect of the text - I want the text to slide from left to right. I want to use this approach to show longer texts in a single line label field.

----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
New Contributor
Posts: 8
Registered: ‎10-07-2011
My Device: no device - Simulator Torch 9800
My Carrier: Developper

Re: Animated LabelField

Use a custom LabelField and a Timer to have something like :

 

public class MyLabelField extends LabelField {
	private static int TIME_TO_CHANGE = 1000; //time in millisecond
	private static int CHAR_TO_DISPLAY = 10;
	private int currentIndex = 0;
	private Sting _label;

 
	public FCLabelField(Object text) {
		super(((String)text).substring(currentIndex, CHAR_TO_DISPLAY)); 
		_label = text;
		final Timer timer = new Timer();
		final TimerTask task = new TimerTask() {
			public void run() {
				final UiApplication uia = UiApplication.getUiApplication();
				final Object eventLock = uia.getEventLock();
				synchronized (eventLock) {
					currentIndex = (currentIndex + CHAR_TO_DISPLAY) % _label.length();
					int currentLastIndex = (currentIndex + CHAR_TO_DISPLAY) % _label.length();
					setText(_label.substring(currentIndex, currentLastIndex);
				}
			}
		};
		timer.schedule(task, timeToChange, timeToChange);
	}
}
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Animated LabelField

Thanks but your approach sets the text and makes different parts of it appear at intervals.
I need it to slide smoothly from one side to the other - somehting with the drawText() method of the graphics object perhaps.
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
New Contributor
Posts: 8
Registered: ‎10-07-2011
My Device: no device - Simulator Torch 9800
My Carrier: Developper

Re: Animated LabelField

Or just set the text with a difference of one character 

Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Animated LabelField

Yeah I tried that and if you set the difference to be 1 char and decrease the thread sleep time it looks better - still not as good as I want it but I will try to go with that
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Animated LabelField

Instead of using setText, just invalidate the Field in the Timer.  Then override the paint Field in the label and drawText in there. 

 

You can certainly use the approach to more 1 character at a time.  But I think that if you wanted to move a certain number of pixels, then just set you 'x' position -ve, and the text will be drawn off screen and will not display until an onscreen pixel is written to.  Pretty sure this will work, though I have never tested it.

 

Another option is to put you text into a scrolling HorizontalFieldManager, and the scroll the Field manager in the Timer

 

And yet another option is to write the text into a Bitmap, then, in the paint, move the displayed window along the Bitmap.

 

But the lowest impact, therefore most likely to perform best, approach is the paint/drawText method I outlined first - give that a try and let us know how you get on. 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Animated LabelField

Some links illustrating Peter's first approach can be found in this thread:

Ticker (movable text) in application

The code under those links was also created by Peter, and even though he claims that it's not "production quality", it is a very good starting point, illustrating the technique clearly and without any unrelated code.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Animated LabelField

This approach with redrawing seems to work best of all I tried. Not as smooth as I wanted initially but still satisfying and for now I don't see performance issues.
----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy