JavaFX ImageView Transition

时间:2015-04-09 22:30:44

标签: javafx imageview

我试图创建图库并使用一些图像动画。问题出在ImageView上。我希望play() RotateTransition从某种方法中随时调用此方法,但它根本不起作用。线程应该存在一些问题,但即使从新线程调用它也没有发生任何事情。是否有任何解决方案如何一般地使用ImageViewTransitions

public class ImageGallery extends ImageView{
    RotateTransition rt;

    public ImageGallery() {
        setImage(new Image("/img/01.jpg"));
        setPreserveRatio(true);

        rt = new RotateTransition(Duration.millis(800), this);
        rt.setByAngle(90);

        //this works but not what I need 
        //fitWidthProperty().addListener(e -> rt.play()); 
    }

    public void rotateRight(){
        rt.play(); //nothing

        //run later is not working too 
        //Platform.runLater(new ViewTransition(this));
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

根据问题中的用户评论,添加MCVE

<强> Main.java

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        ImageGallery gallery = new ImageGallery();
        VBox box= new VBox(gallery);
        box.setAlignment(Pos.CENTER);
        Scene scene = new Scene(box, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
        gallery.rotateRight();
    }

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

}

<强> ImageGallery.java

import javafx.animation.RotateTransition;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;

public class ImageGallery extends ImageView{
    RotateTransition rt;

    public ImageGallery() {
        setImage(new Image("http://jaxenter.com/wp-content/uploads/2013/03/javafx.1.png"));
        setPreserveRatio(true);
        rt = new RotateTransition(Duration.millis(800), this);
        rt.setByAngle(90);
    }

    public void rotateRight(){
        rt.play();
    }
}