Rol*_*and 10
我还没有完成GIF动画,甚至没有意识到JavaFX会有启动和停止它们的方法.如果你想用图像做任何动画,我建议你自己一帧一帧地做.这样您就可以完全控制它,并且您的图像中只能有256种颜色.
我在Mike的博客中阅读了一篇关于使用JavaFX创建Sprite动画的非常好的文章.
这很容易做到.您只需扩展Transition类,向其添加ImageView并实现Transition的Interpolate方法.
编辑:哦,顺便说一句,GIF有一个循环标志,告诉他们要么循环播放,要么不循环播放.换句话说:理论上你可以修改GIF文件的循环属性.仅在理论上,因为我只是尝试指定只播放一次而在JavaFX中它仍然在无限循环中播放,而在FireFox中它播放一次.顺便说一句,JavaFX似乎不支持动画PNG(APNG),它支持超过256种颜色.因此自动图像动画功能非常有限.最好自己做动画.
我希望有人能提出更好的东西,但这里有一个关于如何完全控制你的gif的示例代码.
import java.awt.image.BufferedImage;
import java.net.URISyntaxException;
import javafx.animation.Interpolator;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* Requires GifDecoder from here: http://www.java2s.com/Code/Java/2D-Graphics-GUI/DecodesaGIFfileintooneormoreframes.htm
*/
public class AnimatedGifDemo extends Application {
@Override
public void start(Stage primaryStage) throws URISyntaxException {
HBox root = new HBox();
// TODO: provide gif file, ie exchange banana.gif with your file
Animation ani = new AnimatedGif(getClass().getResource("banana.gif").toExternalForm(), 1000);
ani.setCycleCount(10);
ani.play();
Button btPause = new Button( "Pause");
btPause.setOnAction( e -> ani.pause());
Button btResume = new Button( "Resume");
btResume.setOnAction( e -> ani.play());
root.getChildren().addAll( ani.getView(), btPause, btResume);
Scene scene = new Scene(root, 1600, 900);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public class AnimatedGif extends Animation {
public AnimatedGif( String filename, double durationMs) {
GifDecoder d = new GifDecoder();
d.read( filename);
Image[] sequence = new Image[ d.getFrameCount()];
for( int i=0; i < d.getFrameCount(); i++) {
WritableImage wimg = null;
BufferedImage bimg = d.getFrame(i);
sequence[i] = SwingFXUtils.toFXImage( bimg, wimg);
}
super.init( sequence, durationMs);
}
}
public class Animation extends Transition {
private ImageView imageView;
private int count;
private int lastIndex;
private Image[] sequence;
private Animation() {
}
public Animation( Image[] sequence, double durationMs) {
init( sequence, durationMs);
}
private void init( Image[] sequence, double durationMs) {
this.imageView = new ImageView(sequence[0]);
this.sequence = sequence;
this.count = sequence.length;
setCycleCount(1);
setCycleDuration(Duration.millis(durationMs));
setInterpolator(Interpolator.LINEAR);
}
protected void interpolate(double k) {
final int index = Math.min((int) Math.floor(k * count), count - 1);
if (index != lastIndex) {
imageView.setImage(sequence[index]);
lastIndex = index;
}
}
public ImageView getView() {
return imageView;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它提供暂停/恢复按钮进行测试.你需要的还有Gif解码器代码和动画banana.gif.
