我怎样才能停止JavaFX的GIF动画? [英] How I can stop an animated GIF in JavaFX?

查看:1790
本文介绍了我怎样才能停止JavaFX的GIF动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用GIF动画,但我不知道我怎么能停止循环动画。我的意思是,我想要的GIF只玩1次。

I want to use an animated GIF in my project, but I dont know how I can stop the loop animation. I mean, I want the GIF to play 1 time only.

谢谢!

推荐答案

我没有做GIF动画,甚至没有意识到的JavaFX将对启动和停止它们的方法。如果你想使用图像做任何动画,我宁愿建议你不要用它自己的帧帧。这样,您就可以完全掌控它,你可以拥有的不仅仅是256色以上的图像。

I haven't done GIF animation, wasn't even aware that JavaFX would have methods for starting and stopping them. If you wish to do ANY animation using images, I rather suggest you do it frame by frame yourself. This way you have full control over it and you can have more than just 256 colors in your image.

我读到一篇非常好的文章创建雪碧与动画JavaFX的的迈克的博客。

这是很容易做到的。您只需延长过渡类,添加的ImageView 可它并实现转型的<一个href=\"http://docs.oracle.com/javafx/2/api/javafx/animation/Transition.html#interpolate%28double%29\">Interpolate方法。

It's very easy to do. You simply extend the Transition class, add an ImageView to it and implement the Transition's Interpolate method.

编辑:哦,顺便说一句,GIF格式必须告诉他们无论是在循环播放或没有在循环播放循环标志。换句话说:从理论上讲,你可以修改GIF文件的循环特性。在理论上而已,因为我只是试图与指定要只播放一次,并在JavaFX的它仍然在死循环而发挥在FireFox它播放一次。顺便说一句,JavaFX的似乎不支持动画PNG图片(APNG),这将支持超过256种颜色。因此,自动图像动画功能是非常有限的。最好自己做动画。

oh, and by the way, GIFs have a loop flag which tells them to either play in a loop or not to play in a loop. In other words: In theory you could modify the GIF file's loop property. In theory only, because I just tried with specifying to play only once and in JavaFX it still played in an endless loop while in FireFox it played once. By the way, JavaFX doesn't seem to support animated PNGs (APNG) which would support more than 256 colors. So the automatic image animation capabilities are very limited. Best to do the animation by yourself.

我希望有人想出了更好的东西,但这里的你怎么能拿在你的GIF完全控制一个例子code。

I hope someone comes up with something better, but here's an example code about how you could get full control over your 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;
        }

    }

}

它提供了一个用于测试的暂停/恢复按钮。你除了需要的是的Gif德codeR code 以及动画 banana.gif

这篇关于我怎样才能停止JavaFX的GIF动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆