我们如何将动画应用于图像的随机实例? 我已经尝试过了,但是它只将动画应用于一个图像实例。
public class TimeLines extends Application {
AnchorPane root;
Image img;
private final Random random = new Random();
private final double shs = 10.0; // Star Hand Size
ImageView imgView;
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
Image img=new Image(new FileInputStream("plus.png"));
root = new AnchorPane();
for(int cont = 0 ; cont < 15 ; cont++)
{
imgView=new ImageView();
imgView.setImage(img);
imgView.setTranslateX( Math.random() * 800 );
imgView.setTranslateY( Math.random() * 400 );
root.getChildren().add(imgView);
play();
}
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private void play() {
final Timeline timeline = new Timeline();
timeline.setCycleCount(2);
timeline.setAutoReverse(true);
final KeyValue kFade = new KeyValue(imgView.opacityProperty(), 0);
final KeyFrame kf = new KeyFrame(Duration.millis(1000), kFade);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(e -> {
play(); });
timeline.play();}
public static void main(String[] args) {
launch(args);
}
}
现在是完整的代码;
它仅将动画应用于一个图像实例,但我想对所有对象进行随机动画
答案 0 :(得分:1)
我的想法可能对这种情况有用:
将所有 ImageViews 存储在列表中
创建一个具有列表大小的 Random
创建一种方法,以将动画应用于由索引的 ImageView Random 返回的值
示例代码:
//create list to store all ImageViews
private static List<ImageView> imageViewList = new LinkedList<>();
//add ImageView in list in each iteration (each image created)
for(int cont = 0 ; cont < 15 ; cont++){
imgView=new ImageView();
imgView.setImage(img);
imgView.setTranslateX( Math.random() * 800 );
imgView.setTranslateY( Math.random() * 400 );
root.getChildren().add(imgView);
//new line added to your code
imageViewList.add(imgView);
}
//create random object
Random rand = new Random();
// Obtain a number between [0 - (listSize-1)].
//int n = rand.nextInt(imageViewList.size()-1);
//animate a random image, put it in a loop to continue animation
//use while(true){...} for infinite loop
animateImageViewIn(rand.nextInt(imageViewList.size()-1));
//create method to animate ImageView in given index in list
animateImageViewIn(int index){
//your animation code
//...
//apply animation to the random ImageView
final KeyValue kFade = new KeyValue(imageVewList.get(index).opacityProperty(), 0);
//continue your code
//...
}
希望有帮助!
答案 1 :(得分:1)
您只获得一个过渡的原因是因为这行imgView=new ImageView();
,它仅在创建列表并将所有这些都添加到该列表然后设置关键帧的情况下才设置最后一个的过渡对于所有人来说,它都应该起作用
工作示例
public class Main extends Application {
private AnchorPane root;
private ArrayList<ImageView> imageViewArrayList = new ArrayList<>();
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
Image img=new Image(new FileInputStream("plus.png"));
root = new AnchorPane();
for(int cont = 0 ; cont < 15 ; cont++)
{
ImageView imgView=new ImageView();
imgView.setImage(img);
imgView.setTranslateX( Math.random() * 800 );
imgView.setTranslateY( Math.random() * 400 );
imageViewArrayList.add(imgView);
root.getChildren().add(imgView);
//play();//I don't think you need this
}
new Timer().schedule(//Will keep infinitely firing every second
new TimerTask() {
@Override
public void run() {
play();
}
},
0,
1000);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private void play() {
final Timeline timeline = new Timeline();
timeline.setCycleCount(2);
timeline.setAutoReverse(true);
//For animating All
//for (ImageView imageView : imageViewArrayList) {
// final KeyValue kFade = new KeyValue(imageView.opacityProperty(), 0);
// timeline.getKeyFrames().add(new KeyFrame(Duration.millis(1000), kFade));
//}
timeline.getKeyFrames().clear();//Remove Last
int randomIndex = (int) (Math.random() * imageViewArrayList.size());//Pick random Index
timeline.getKeyFrames().add(//Add random index
new KeyFrame(Duration.millis(1000),
new KeyValue(imageViewArrayList.get(randomIndex).opacityProperty(), 0)));
//timeline.setOnFinished(e -> play());//I don't think you need this
timeline.play();
}
public static void main(String[] args) { launch(args); }
}