[英]javafx simple PathTransition animation
i'm using JavaFX to build a cardgame and i struggle to add simple animations. 我正在使用JavaFX构建纸牌游戏,并且很难添加简单的动画。
I have a HBox with multiple ImageViews in them. 我有一个带有多个ImageViews的HBox。 Every Image has a right margin of -80 to get the images to overlap each other.
每个图像的右边距为-80,以使图像彼此重叠。
Now i want to animate the card when it's added. 现在,我想为卡片添加动画效果。 I want to place it somewhere on the screen (opponent player hand) and move it to the position it would've been without animation.
我想将其放置在屏幕上(对手的手)并将其移动到没有动画的位置。
I tried this: 我尝试了这个:
// 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();
But it doesn't work like i want it to. 但是它不像我想要的那样工作。 I have no clue how to handle the coordiantes and can't find a solution for my problem.
我不知道如何处理coordiantes,也找不到解决我的问题的解决方案。
I hope someone can help me with my problem 我希望有人可以帮助我解决我的问题
The correct Path
geometry hinges critically on the layout of the root Parent
. 正确的
Path
几何关键取决于根Parent
的布局。 In the example below, the Path
goes from the top-left to the bottom-right, adjusted to keep the moving Rectangle
in view. 在下面的示例中,
Path
从左上角到右下角进行了调整,以使移动的Rectangle
保持Rectangle
。 It adds the rectangle to a Pane
, "since it does not perform layout beyond resizing resizable children to their preferred sizes." 它将矩形添加到
Pane
,“因为除了调整可调整大小的子项的大小以使其首选大小之外,它不会执行布局”。 If you use a more advanced layout, eg StackPane
, adjust the alignment accordingly: 如果使用更高级的布局,例如
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 http://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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.