【发布时间】:2016-06-22 12:08:07
【问题描述】:
我想一张一张地随机显示我的图像。仅当我将持续时间以秒为单位时,下面的代码才会一张一张地显示图像:
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)),
所以,我的代码将显示 image1 firstn image 2 second, image3thirds 等等。
1) 我希望它每次都显示随机图像。
2)不依赖于持续时间。因为如果我将Duration.seconds(3) 放在所有这些上,它将只显示第一个。
代码如下:
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);
}
}
【问题讨论】:
-
“每次”是什么意思?
-
@KWeiss 我想在运行此代码时一张一张地随机显示图像。
-
为什么所有时间点都一样?
-
@James_D 所以这个 'seconds(3)' 不是图像将显示多长时间?
-
顺便说一句,第一个构造函数参数是时间轴中
KeyFrame的效果发生的时间点(或在使用插值器的情况下完全生效)。