Package limn.animation
Class ImageAnimation
java.lang.Object
limn.animation.ImageAnimation
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classHow many turns an animation makes, and which way. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanadvance(double seconds) Moves time on.frame()intimage()booleanbooleanloop()restart()Goes back to the first frame of the first turn.doubleseekFrame(int frame) Shows a frame, from the start of its time.setLoop(ImageAnimation.Loop newLoop) Sets how many turns it makes;ImageAnimation.Loop.FILEunless set.setPlaying(boolean play) Plays, or holds the frame shown.setSpeed(float newSpeed) floatspeed()
-
Constructor Details
-
ImageAnimation
- Parameters:
image- the image to play; a still one is played as one frame
-
-
Method Details
-
image
- Returns:
- the image being played
-
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
Plays, or holds the frame shown. Playing an animation that has finished starts it again from its first frame. -
setSpeed
- Parameters:
newSpeed- how fast: 1 is the image's own times, 2 twice as fast, 0.5 half- Throws:
IllegalArgumentException- ifnewSpeedis not above 0
-
speed
public float speed()- Returns:
- the speed, 1 unless set
-
setLoop
Sets how many turns it makes;ImageAnimation.Loop.FILEunless set. An animation that had finished can play again. -
loop
- Returns:
- how many turns it makes
-
restart
Goes back to the first frame of the first turn. -
seekFrame
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
-