Animating the Layout

The code for sublayout follows:

protected void sublayout(int width, int height) { super.sublayout(width, height); if (verticalOffset > 0) {

if (animationStart == 0) { // start the animation animationStart = System.currentTimeMillis();

long timeElapsed = System.currentTimeMillis() - animationStart; if (timeElapsed >= animationTime) { verticalOffset = 0;

float percentDone = (float)timeElapsed / (float)animationTime; verticalOffset =

Display.getHeight() - (int)(percentDone * Display.getHeight());

setPosition(0, verticalOffset);

UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { updateLayout();

Notice that there's an initial case where animationStart is zero. This case represents the first frame of the animation, so we just leave verticalOffset where it is.

Speaking of verticalOffset, we'll initialize it in the LoginScreen constructor to the height of the display:

verticalOffset = Display.getHeight(); new Thread(this).start();

And that's it. Now, the login success screen will smoothly scroll up from the bottom of the screen (see Figure 5-31).

Figure 5-31. The login success screen sliding in

All animation follows the same basic pattern, but it can get much more complex. With the same basic technique you can implement motion, fading, and more.

+1 0

Average user rating: 5 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site