Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

Creating Simple Animations with Java ME

From Wiki
Jump to: navigation, search
Tomi_ (Talk | contribs)
m (Tomi -)
Tomi_ (Talk | contribs)
m (Tomi -)
Line 1: Line 1:
 
[[Category:Draft]]
 
[[Category:Draft]]
{{Abstract|This article explains how to create simple animations with Java ME. The code introduced in this article is compatible with most Java ME devices including Series 40 phones and Nokia Asha software platform 1.0.}}
+
{{Abstract|This article explains how to ... }}
  
 
{{ArticleMetaData <!-- v1.3 -->
 
{{ArticleMetaData <!-- v1.3 -->
|sourcecode= [[Media:CustomCategoryBarDemo-2013-06-27.zip]]
+
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.wgt]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.wgt]]) -->
 
|devices= <!-- Devices tested against - e.g. Nokia Lumia 928, Nokia Asha 501) -->
 
|devices= <!-- Devices tested against - e.g. Nokia Lumia 928, Nokia Asha 501) -->
|sdk=Nokia Asha SDK 1.0
+
|sdk= <!-- SDK(s) built and tested against (e.g. Windows Phone 8.0 SDK) -->
|platform=Series 40, Nokia Asha software platfrom 1.0 and later
+
|platform= <!-- Compatible platforms - e.g. Nokia Asha Platform 1.0 and later -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
 
|signing=<!-- Special Signing requirements -->
 
|signing=<!-- Special Signing requirements -->
Line 25: Line 25:
 
== Introduction ==
 
== Introduction ==
  
The standard LCDUI framework has no out-of-the-box animations. [http://www.developer.nokia.com/Resources/Library/LWUIT/#!index.html LWUIT] does provide them, but in case you want to stick with the LCDUI you have no choice but to implement them yourself. Nokia UI API provides the [http://www.developer.nokia.com/Resources/Library/Java/_zip/GUID-237420DE-CCBE-4A74-A129-572E0708D428/com/nokia/mid/ui/frameanimator/package-summary.html FrameAnimator package], but that is not generic and has dependency to the user interface (UI) components. This article studies implementing a simple, generic, UI independent animation framework that is both efficient and easy to use. The full source code is available here: [[File:CustomCategoryBarDemo-2013-06-27.zip]]
+
The standard LCDUI framework has no out-of-the-box animations. [http://www.developer.nokia.com/Resources/Library/LWUIT/#!index.html LWUIT] does provide them, but in case you want to stick with the LCDUI you have no choice but to implement them yourself. Nokia UI API provides the [http://www.developer.nokia.com/Resources/Library/Java/_zip/GUID-237420DE-CCBE-4A74-A129-572E0708D428/com/nokia/mid/ui/frameanimator/package-summary.html FrameAnimator package], but that is not generic and has dependency to the user interface (UI) components. This article studies implementing a simple, generic, UI independent animation framework that is both efficient and easy to use.
  
 
== Implementation ==
 
== Implementation ==

Revision as of 13:50, 27 June 2013

This article explains how to ...

Article Metadata
CompatibilityArticle
Created: (28 Jun 2013)
Last edited: Tomi_ (27 Jun 2013)

Contents

Introduction

The standard LCDUI framework has no out-of-the-box animations. LWUIT does provide them, but in case you want to stick with the LCDUI you have no choice but to implement them yourself. Nokia UI API provides the FrameAnimator package, but that is not generic and has dependency to the user interface (UI) components. This article studies implementing a simple, generic, UI independent animation framework that is both efficient and easy to use.

Implementation

Architecture

Animation-fw-architecture.png

Easing curves

In addition to the duration and the starting and ending point, easing curves play a vital part in defining the behavior of an animation. The simplest easing curve, linear curve, moves the object you are animating from the source to the destination point with steady speed where-as in-out-quad easing curves makes the animation more vivid.

Implementation-wise the approach here is to calculate the values for the animation beforehand. The easing curve implementation is responsible for calculating the values. The abstract base class EasingCurve implements everything but the calculate() method. The calculate() method of the linear easing curve is as follows:

public class LinearEasingCurve extends EasingCurve {
public boolean calculate(int from, int to, int duration) {
_duration = duration;
_steps = _duration / IntAnimation.UPDATE_INTERVAL;
_values = new int[_steps];
_currentStepIndex = 0;
final int stepLength = (to - from) / (_steps - 1);
 
_values[0] = from;
_values[_steps - 1] = to;
 
for (int i = 1; i < _steps - 1; ++i) {
_values[i] = from + stepLength * i;
}
 
return true;
}
}

Currently, this light animation framework implements three easing curves: linear, in-out-quad and out-in-quad.

Usage

Using the animation package is easy. First have your class, that handles the animating, derive from AnimationListener. Create the IntAnimation instance where convenient - straightforward place to do it is in the constructor. Remember to set the listener so that you can receive the animated values and changes in the animation states.

public class MyCanvas
extends Canvas
implements AnimationListener
{
// Constants
...
private static final int ANIMATION_DURATION = 500;
private static final int EASING_CURVE = IntAnimation.EASING_CURVE_INOUTQUAD;
 
// Members
...
private IntAnimation _animation = null;
...
 
/**
* Constructor.
*/

public MyCanvas() {
super();
...
_animation = new IntAnimation();
_animation.setListener(this);
...

Implement the methods required by the AnimationListener interface:

    /**
* @see AnimationListener#onAnimatedValueChanged(int)
*/

public void onAnimatedValueChanged(int value) {
// Handle the value
...
repaint();
}
 
/**
* @see AnimationListener#onAnimationStateChanged(int)
*/

public void onAnimationStateChanged(int state) {
switch (state) {
case IntAnimation.STATE_RUNNING:
...
break;
case IntAnimation.STATE_STOPPED:
...
break;
case IntAnimation.STATE_FINISHED:
...
break;
default:
break;
}
}

Then just let it rip:

boolean started = _animation.start(from, to, ANIMATION_DURATION, EASING_CURVE))
 
if (!started) {
// Failed to start the animation! Handle this situation gracefully - maybe by just jumping to the "to" point.
}

Summary

It's not hard to do animations with Java ME. However, it requires work and when you decide to implement the animations, make sure you do it in a way that you can re-use your stuff later in your other projects. The study, explained in this article, produced an animation package that is quite generic and you are free to use it and expand it with your own easing curves. Further improvements to this package would include implementing a dedicated thread for animations. The current implementation is basically based on just fire-and-forget principle.

290 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×