在JavaFX中基于gif动画设置ImageView

时间:2016-11-10 02:59:41

标签: java javafx imageview gif fxml

我对编程很新,最近遇到了一个我没有找到在线解决方案的问题。我在FXML中创建了一个ImageView,并给它一个" gif1"的FXid。我正在使用此stackoverflow帖子中的代码,但我尝试修改它以便它符合我的需要。我将整个代码放在一个单独的java文件中,我称之为#34; Gif.java"。通常," Gif.java"使用hbox和其他不适用的方法来设置ImageView。我删除了那些因为我将使用现有的FXML文档,因此不需要创建新文档。在我的代码中,我打电话给" Gif.java"并期望返回ImageView。但是,当我将ImageView(gif1)设置为返回的ImageView时,它不会在屏幕上更新。

以下是gif帖子的链接:How I can stop an animated GIF in JavaFX?

这是我用来更新gif1的代码:

@FXML
    private void setUp(){
        Gif newGif = new Gif("rightrock.gif");  // Creates object newGif and passes through path to gif
        gif1 = newGif.returnImage2();  // Sets gif1 to returned ImageView
    }

以下是Gif.java中的返回码:

public class Gif{
    private Animation ani;
    private ImageView test;
    public Gif(String image) {
        // TODO: provide gif file, ie exchange banana.gif with your file
        ani = new AnimatedGif(getClass().getResource(image).toExternalForm(), 1000);
    ani.setCycleCount(1);
    ani.play();

    Button btPause = new Button( "Pause");
    btPause.setOnAction( e -> ani.pause());

    Button btResume = new Button( "Resume");
    btResume.setOnAction( e -> ani.play());
}

public void returnImage(ImageView x){
    test = x;  // Sets ImageView test to parameter x
}

public ImageView returnImage2() {
    return test;  // Return instance field test
}

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() {
        returnImage(imageView);  // This runs returnImage with the ImageView that I want to set
        return imageView;
    }

}

非常感谢任何帮助!

0 个答案:

没有答案