【发布时间】:2018-06-02 15:54:47
【问题描述】:
我正在使用时间轴动画对图像数组进行动画处理。该数组是 299 个图像。它从 0 到 298 个图像迭代一次,然后动画停止。
它应该连续动画但不起作用。我使用时间轴动画为每个图像视图使用 opacityProperty()。一旦一个图像动画完成,它就会转到下一个图像。但是当它达到 298 图像时,我无法连续循环。变量 x 应该变为 0,然后再次开始动画。
public class Animation_Program_version3 extends Application {
Timeline timeline = null;
Group rootGroup = null;
int x = 0;
Image [] images = new Image[299];;
ArrayList imageview = null;
public Animation_Program_version3() {
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 800, 400, Color.BEIGE);
imageview = new ArrayList();
int y = 0;
for(int x = -50; x < 100; x=x+1){
images[y] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y]));
y = y+1;
}
int y1 = 150;
for(int x = 99; x > -50; x=x-1){
images[y1] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y1]));
y1 = y1+1;
}
rootGroup.getChildren().addAll(imageview);
int x = 0;
timeline = new Timeline();
doAnimation();
primaryStage.setScene(scene);
`primaryStage.show(); `
}
<code>
public void doAnimation(){
KeyFrame[] kf = new KeyFrame[images.length];
ImageView im = (ImageView)imageview.get(x);
<code>
im.setImage(images[x]);
kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0));
timeline.getKeyFrames().add(kf[x]);
// When timeline animation is finished it executes the seetOnFinished Event
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if( x == 298){
System.out.println("VALUE OF x:"+x);
x=0; -------> This is where code does not work When it reaches end of array and x initialize to 0 then animation stops.
Collections.reverse(imageview);
doAnimation();
}
/* This if loop works fine animation iterates through 0 to 298 images. */
if( x < 298){
x++;
doAnimation();
}
}
});
timeline.play();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
我已经更正了我的程序,现在我没有收到此错误 java.lang.IllegalArgumentException: Children: duplicate children。
但问题仍然是我看不到屏幕上正在运行的程序。我在场景中添加了根组,但我在屏幕上什么也看不到。我的新程序:
public class Animation_Program_version3 extends Application {
Timeline timeline = null;
Group rootGroup = null;
int x = 0;
Image [] images = new Image[299];;
ArrayList imageview = null;
ImageView im = new ImageView();
public Animation_Program_version3() {
// this.imageview = new TreeSet();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
rootGroup = new Group();
final Scene scene =
new Scene(rootGroup, 800, 400, Color.BEIGE);
//
// final Scene scene =
// new Scene(rootGroup, 800, 400, Color.BEIGE);
// int x = 0;
//Image [] images =
imageview = new ArrayList();
int y = 0;
for(int x = -50; x < 100; x=x+1){
images[y] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y]));
y = y+1;
}
int y1 = 150;
for(int x = 99; x > -50; x=x-1){
images[y1] = new Image("/Image"+x+".jpg", true);
imageview.add(new ImageView(images[y1]));
// imageview[y1] = new ImageView(images[y1]);
y1 = y1+1;
}
//for (int i = 0; i < 299; i++) {
// rootGroup.getChildren().addAll(imageview);
//}
int x = 0;
timeline = new Timeline();
doAnimation();
primaryStage.setScene(scene);
primaryStage.show();
}
public void doAnimation(){
KeyFrame[] kf = new KeyFrame[images.length];
// im = (ImageView)imageview.get(x);
im.setImage(images[x]);
rootGroup.getChildren().setAll(im);
kf[x] = new KeyFrame(Duration.millis(1), new KeyValue(im.opacityProperty(), 0));
timeline.getKeyFrames().add(kf[x]);
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// timeline = null;
if( x == 298){
System.out.println("VALUE OF x:"+x);
x=0;
// Collections.reverse(imageview);
// rootGroup.getChildren().setAll(imageview);
//
doAnimation();
}
if( x < 298){
System.out.println("Inside 298 OF x:"+x);
x++;
// Animation_Program_version3.rootGroup = null;
// Animation_Program_version3.rootGroup = new Group();
doAnimation();
}
}
});
timeline.play();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
您是否收到特定错误?另请重新格式化您的问题。
-
你的代码抛出
java.lang.IllegalArgumentException: Children: duplicate children,是你的问题吗? -
我已经纠正了这个问题,现在你不会得到这个 java.lang.IllegalArgumentException: Children: duplicate children error
-
我的另一个问题是什么时候使用 Group 以及什么时候使用 node 在场景对象中设置。在我的程序中,我没有看到屏幕上出现任何内容。
-
我认为我的动画持续时间是 1 毫秒,因此可能是看不到动画的原因