Class ImageAnimation

java.lang.Object
limn.animation.ImageAnimation

public final class ImageAnimation extends Object
Plays an animated Image: which frame is shown now, given the time that has passed.

It is the clockwork and nothing else: it draws nothing and asks for no frame. Whoever owns it hands it the time (advance(double)) and draws frame(); an ImageView does that for a picture, and a widget of an application's own does it from a scene's ticker:


 ImageAnimation walking = new ImageAnimation(Images.fromSheet(sheet, 8, 1, 90));
 scene.addTicker(seconds -> {
     if (walking.advance(seconds)) {
         invalidate();
     }
     return scene() != null;
 });
 // in onPaint: canvas.drawImage(walking.frame(), x, y, width, height);
 

A still image is an animation of one frame that is always finished. Not thread-safe: the UI thread's, like the widget that owns it.

  • Constructor Details

    • ImageAnimation

      public ImageAnimation(Image image)
      Parameters:
      image - the image to play; a still one is played as one frame
  • Method Details

    • image

      public Image image()
      Returns:
      the image being played
    • frame

      public Image frame()
      Returns:
      the frame to draw now. For a long animation that is decoded as it plays this is the last frame that was ready, which is the frame of the moment unless the decoder is behind
    • frameIndex

      public int frameIndex()
      Returns:
      which frame is shown, from 0
    • isPlaying

      public boolean isPlaying()
      Returns:
      whether time moves it: not held by setPlaying(boolean), and not at its end
    • isFinished

      public boolean isFinished()
      Returns:
      whether its last turn has ended; never, for one that turns without end
    • setPlaying

      public ImageAnimation setPlaying(boolean play)
      Plays, or holds the frame shown. Playing an animation that has finished starts it again from its first frame.
    • setSpeed

      public ImageAnimation setSpeed(float newSpeed)
      Parameters:
      newSpeed - how fast: 1 is the image's own times, 2 twice as fast, 0.5 half
      Throws:
      IllegalArgumentException - if newSpeed is not above 0
    • speed

      public float speed()
      Returns:
      the speed, 1 unless set
    • setLoop

      public ImageAnimation setLoop(ImageAnimation.Loop newLoop)
      Sets how many turns it makes; ImageAnimation.Loop.FILE unless set. An animation that had finished can play again.
    • loop

      public ImageAnimation.Loop loop()
      Returns:
      how many turns it makes
    • restart

      public ImageAnimation restart()
      Goes back to the first frame of the first turn.
    • seekFrame

      public ImageAnimation seekFrame(int frame)
      Shows a frame, from the start of its time. The turns already made are kept.
      Throws:
      IndexOutOfBoundsException - if there is no such frame
    • advance

      public boolean advance(double seconds)
      Moves time on.
      Parameters:
      seconds - how much time has passed, as a scene's ticker hands it
      Returns:
      whether the frame to draw changed
    • secondsUntilNextFrame

      public double secondsUntilNextFrame()
      Returns:
      how long until the frame changes, in seconds at the speed set; infinite when nothing will change it