Class IconMorph

java.lang.Object
com.codename1.ui.Component
com.codename1.components.IconMorph
All Implemented Interfaces:
Animation, Editable, StyleListener

public class IconMorph extends Component

A tappable icon that morphs between two material glyphs -- menu and back arrow, play and pause, add and close. The old glyph turns and fades out as the new one turns and fades in.

IconMorph play = new IconMorph(FontImage.MATERIAL_PLAY_ARROW, FontImage.MATERIAL_PAUSE);
play.addActionListener(e -> Log.p(play.isMorphed() ? "playing" : "paused"));

A tap toggles the state and then notifies listeners. The rotation needs affine transforms; where the platform lacks them the glyphs only cross-fade. The UIID is IconMorph, and the glyph takes its foreground color.

  • Constructor Details

    • IconMorph

      public IconMorph(char from, char to)

      An icon showing from until it is toggled.

      Parameters
      • from: the first glyph, such as FontImage.MATERIAL_MENU

      • to: the glyph shown once morphed, such as FontImage.MATERIAL_ARROW_BACK

  • Method Details

    • isMorphed

      public boolean isMorphed()

      Whether the icon shows its second glyph.

      Returns

      true once morphed

    • setMorphed

      public void setMorphed(boolean morphed, boolean animate)

      Shows one glyph or the other without notifying listeners.

      Parameters
      • morphed: true for the second glyph

      • animate: true to play the morph, false to switch at once

    • toggle

      public void toggle()
      Flips to the other glyph with the animation, without notifying listeners.
    • setDuration

      public void setDuration(int millis)

      Sets how long a morph takes.

      Parameters
      • millis: the duration, 0 to switch at once
    • setRotation

      public void setRotation(int degrees)

      Sets how far the glyphs turn during a morph.

      Parameters
      • degrees: the turn, 0 for a plain cross-fade
    • addActionListener

      public void addActionListener(ActionListener l)

      Listens for the user toggling the icon.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Stops listening.

      Parameters
      • l: the listener
    • 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: Component

      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 Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component