Class AnimatedNumber

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.Label
com.codename1.components.AnimatedNumber
All Implemented Interfaces:
Animation, Editable, StyleListener, IconHolder, TextHolder

public class AnimatedNumber extends Label

A label showing a number that counts up or down to each new value.

AnimatedNumber total = new AnimatedNumber();
total.setPrefix("$");
total.setDecimals(2);
total.setValue(1249.5);

setValue counts from the number shown to the new one, fast at first and slowing at the end, over setDuration milliseconds; setValueNow jumps. The number is written with the given decimals and a prefix and suffix, such as a currency sign or a unit. The count runs while the label is on a showing form.

  • Constructor Details

    • AnimatedNumber

      public AnimatedNumber()
      A label showing 0.
  • Method Details

    • setValue

      public void setValue(double value)

      Counts to a value.

      Parameters
      • value: the value to reach
    • setValueNow

      public void setValueNow(double value)

      Shows a value at once.

      Parameters
      • value: the value
    • getValue

      public double getValue()
      The value counted to.
    • getDisplayedValue

      public double getDisplayedValue()
      The value shown now, part way through a count.
    • isCounting

      public boolean isCounting()
      True while counting.
    • setDecimals

      public void setDecimals(int decimals)

      Sets the decimals shown; 0 by default.

      Parameters
      • decimals: digits after the point
    • setPrefix

      public void setPrefix(String prefix)

      Sets the text before the number.

      Parameters
      • prefix: the text, such as a currency sign
    • setSuffix

      public void setSuffix(String suffix)

      Sets the text after the number.

      Parameters
      • suffix: the text, such as a unit
    • setDuration

      public void setDuration(int millis)

      Sets how long a count takes; 800 milliseconds by default.

      Parameters
      • millis: the duration
    • format

      public static String format(double value, int decimals)

      Writes a number with a number of decimals, rounding half away from 0.

      Parameters
      • value: the number
      • decimals: digits after the point
      Returns

      the number as text

    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • animate

      public boolean animate()
      Description copied from class: Label

      Allows the animation to reduce "repaint" calls when it returns false. It is called once for every frame. Frames are defined by the com.codename1.ui.Display class.

      Returns

      true if a repaint is desired or false if no repaint is necessary

      Specified by:
      animate in interface Animation
      Overrides:
      animate in class Label