在幻灯片中一一显示随机图像的时间轴 [英] Timeline to Display Random images one by one in a slideshow

查看:17
本文介绍了在幻灯片中一一显示随机图像的时间轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一张一张地随机显示我的图片.下面的代码仅当我将持续时间以秒为单位显示时才一张一张地显示图像:

I want to display my images randomly one by one. The code below displays images one by one only if i put the duration time in seconds like:

 new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image1)),
 new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image2)),
 new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
 new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), image4)),

因此,我的代码将首先显示图像 1,然后显示图像 2,然后显示图像 3,以此类推.

So, my code will display image1 firstn image 2 second, image3 thirds and so on.

1) 我希望它每次都显示随机图像.
2) 不依赖持续时间.因为如果我将 Duration.seconds(3) 放在所有这些中,它将只显示第一个.

1) I want it to display random images every time.
2) Not to depend on duration time . Because if i put Duration.seconds(3) to all of them it will display just the first one.

代码如下:

package imagedisplayy;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
 *
 * @author D
 */
public class ImageDisplayy extends Application {

    @Override
    public void start(Stage primaryStage) {
        Image image1 = new Image("file:lib/1.jpg");
        Image image2 = new Image("file:lib/2.jpg");
        Image image3 = new Image("file:lib/3.jpg");
        Image image4 = new Image("file:lib/4.jpg");
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(

                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image1)),
                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),  
                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image4)),   
                new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), null))
                );
        timeline.play();
        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

推荐答案

这是一个基于数组的解决方案,它循环显示随机选择的图像.数组中的所有图像将以随机顺序显示一次,然后数组将被打乱,允许所有图像以不同的随机顺序再次显示.

Here is an array based solution that cycles displaying a random choice of images. All images in the array will be displayed once in random order, then the array will be shuffled, allowing all images to be displayed again in a different random order.

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.*;
import java.util.stream.Collectors;

public class ImageDisplay extends Application {

    private List<Image> images;
    private Iterator<Image> imageIterator;

    @Override
    public void start(Stage stage) {
        images = Arrays.stream(IMAGE_LOCS)
                .map(Image::new)
                .collect(Collectors.toList());
        Collections.shuffle(images);
        imageIterator = images.iterator();
        ImageView imageView = new ImageView();

        Timeline timeline = new Timeline(
                new KeyFrame(
                        Duration.ZERO,
                        e -> {
                            imageView.setImage(imageIterator.next());
                            System.out.println(
                                 "Displaying " + imageView.getImage().impl_getUrl()
                            );
                        }
                ),
                new KeyFrame(Duration.seconds(1))
        );
        timeline.setCycleCount(images.size());
        timeline.setOnFinished(event -> {
            Collections.shuffle(images);
            imageIterator = images.iterator();
            timeline.playFromStart();
        });
        timeline.play();

        StackPane layout = new StackPane(imageView);
        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    // image license: linkware - backlink to http://www.fasticon.com
    private static final String[] IMAGE_LOCS = {
        "http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Blue-Fish-icon.png",
        "http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Red-Fish-icon.png",
        "http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Yellow-Fish-icon.png",
        "http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Green-Fish-icon.png"
    };
}

因为我们每次都进行 shuffle,所以最后显示的图像有可能成为下一个循环中的第一张图像(连续显示两次).如果你不想在循环之间改变顺序,只需删除时间完成播放时执行的shuffle命令.

Because we shuffle each time, it is possible for the last image displayed to be shuffled to be the first image on the next cycle (displaying it twice in a row). If you don't want the order to change between cycles, just remove the shuffle command executed when the time completes playing.

注意,为了简单起见,代码使用 impl_getUrl() 将显示的图像 url 的日志输出到控制台 - 不建议在生产代码中使用 impl 方法.

Note, for simplicity, the code uses impl_getUrl() to output a log of displayed image urls to the console - use of impl methods is not recommended in production code.

这篇关于在幻灯片中一一显示随机图像的时间轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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