javafx简单的PathTransition动画

我正在使用JavaFX构建纸牌游戏,并且很难添加简单的动画.

我有一个带有多个ImageViews的HBox.每个图像的右边距为-80,以使图像彼此重叠.

现在,我想为卡片添加动画效果.我想将其放置在屏幕上(对手的手)并将其移动到没有动画的位置.

我尝试了这个:

// Animation
Path path = new Path();
MoveTo moveTo = new MoveTo(200, 200);
moveTo.setAbsolute(true);
path.getElements().add(moveTo);
LineTo lineTo = new LineTo(0, 0);
path.getElements().add(lineTo);
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.millis(4000));
pathTransition.setPath(path);
pathTransition.setNode(imageView);
pathTransition.setCycleCount(1);
pathTransition.play();

但是它不像我想要的那样工作.我不知道如何处理coordiantes,也找不到解决我的问题的解决方案.

我希望有人可以帮助我解决问题

最佳答案
正确的路径几何形状关键取决于根父级的布局.在下面的示例中,路径从左上角到右下角进行了调整,以使移动的Rectangle保持可见.它将矩形添加到窗格中,“因为除了调整可调整大小的子项的大小以使其首选大小之外,它不会执行布局”.如果您使用更高级的布局,例如StackPane,相应地调整对齐方式:

StackPane root = new StackPane();
root.setAlignment(Pos.TOP_LEFT)
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/** @see https://stackoverflow.com/a/31443755/230513 */
public class AnimationTest extends Application {

    private static final int SIZE = 256;
    private static final int RECT = SIZE / 4;
    private static final int R2 = RECT / 2;

    @Override
    public void start(Stage stage) {
        stage.setTitle("Animation Test");
        Rectangle rect = new Rectangle(0, 0, RECT, RECT);
        rect.setArcHeight(16);
        rect.setArcWidth(16);
        rect.setFill(Color.GOLD);
        Pane root = new Pane();
        root.getChildren().add(rect);
        Scene scene = new Scene(root, SIZE, SIZE);
        stage.setScene(scene);
        stage.show();

        Path path = new Path();
        path.getElements().add(new MoveTo(R2, R2));
        path.getElements().add(new LineTo(SIZE - R2, SIZE - R2));
        PathTransition pathTransition = new PathTransition();
        pathTransition.setDuration(Duration.millis(1000));
        pathTransition.setPath(path);
        pathTransition.setNode(rect);
        pathTransition.setCycleCount(Timeline.INDEFINITE);
        pathTransition.setAutoReverse(true);
        pathTransition.play();
    }

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

}

转载注明原文:javafx简单的PathTransition动画 - 代码日志