【问题标题】:ImageView Sprite Animation with JavaFX带有 JavaFX 的 ImageView Sprite 动画
【发布时间】:2019-03-19 01:49:27
【问题描述】:

我正在尝试使用 JavaFX 在登录窗口(舞台)内创建一个精灵动画,一旦用户获得登录名和密码信息,该动画就会播放。我尝试使用 Michael Heinrichs 在他关于 sprite animations with JavaFX 的博客文章中发布的类,但我无法使其工作,主要是因为我不明白如何在不使用 start 方法的情况下在 ImageView 中创建此动画(这不会在我的情况下也不起作用)。

这是我在登录阶段的 FXML 文件中的代码:

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="crud.controller.MainController">
    <padding>
        <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
    </padding>
    <children>
        <ImageView fx:id="loginAnimationImageView" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.columnSpan="2" GridPane.rowIndex="0">
            <image>
                <Image url="@../assets/shop-sprite.png" />
            </image>
        <viewport>
            <Rectangle2D height="130.0" width="131.0" />
        </viewport>
        </ImageView>
        <Label text="Usuario" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
        <TextField fx:id="loginTextField" onAction="#onEnterTextFieldAction" GridPane.columnIndex="1" GridPane.rowIndex="2" />
        <Label text="Contraseña" GridPane.columnIndex="0" GridPane.rowIndex="3" />
        <PasswordField fx:id="passwordTextField" onAction="#onEnterPasswordFieldAction" GridPane.columnIndex="1" GridPane.rowIndex="3" />
        <Button fx:id="loginButton" onAction="#loginButtonAction" text="Ingresar" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="5" />
    </children>
</GridPane>

在控制器类中,我尝试创建一个启动方法,但首先,我不知道如何在舞台自行打开的同时启动它,其次,当我尝试从按钮调用该方法时,它会尝试渲染图像(修改图像所需的空间)但它什么也不显示。

代码如下:

@FXML private void launchAnimation() {
    loginAnimationImageView = new ImageView(IMAGE);
    loginAnimationImageView.setViewport(new Rectangle2D(OFFSET_X, OFFSET_Y, WIDTH, HEIGHT));

    final Animation animacion = new SpriteAnimation(
            loginAnimationImageView,
            Duration.millis(1000.0),
            COUNT, COLUMNS,
            OFFSET_X, OFFSET_Y,
            WIDTH, HEIGHT
    );

    animacion.setCycleCount(Animation.INDEFINITE);
    animacion.play();
}

最后,这里是常量声明:

private static final Image IMAGE = new Image("https://i.cloudup.com/1VA2FCnqY6-2000x2000.png");
private static final int COLUMNS  = 4;
private static final int COUNT    = 12;
private static final int OFFSET_X = 0;
private static final int OFFSET_Y = 0;
private static final int WIDTH    = 131;
private static final int HEIGHT   = 125;

这就是登录阶段现在的样子,我想为我拥有的徽标制作动画:

你们能帮我理解如何在这种情况下使用图像视图吗?

有什么改变我可以使用 ImageView 来创建这种效果还是我只是在浪费时间?

【问题讨论】:

标签: java animation javafx-2


【解决方案1】:

我做到了,我可以使用 this sprite image 使这个简单的动画与 JavaFX 一起工作,我想与你分享,以防你想完成类似的事情

This is the result (YouTube)

首先,我使用此代码为登录阶段创建了 FXML 文件(请记住,您必须为控制器类和图像文件设置正确的位置)

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="package.Controller">
    <padding>
        <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
    </padding>
    <children>
        <ImageView fx:id="loginAnimationImageView" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.columnSpan="2" GridPane.rowIndex="0">
            <image>
                <Image url="@route/to/shop-sprite.png" />
            </image>
        <viewport>
            <Rectangle2D minX="0" minY="0" height="130.0" width="130.0" />
        </viewport>
        </ImageView>
        <Label text="Usuario" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
        <TextField fx:id="loginTextField" onAction="#onEnterTextFieldAction" GridPane.columnIndex="1" GridPane.rowIndex="2" />
        <Label text="Contraseña" GridPane.columnIndex="0" GridPane.rowIndex="3" />
        <PasswordField fx:id="passwordTextField" onAction="#onEnterPasswordFieldAction" GridPane.columnIndex="1" GridPane.rowIndex="3" />
        <Button fx:id="loginButton" onAction="#loginButtonAction" text="Ingresar" GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="5" />
    </children>
</GridPane>

现在我们必须使用我在上面的问题中链接的 Michael Heinrichs 创建的动画类来设置我们的控制器。

注意:一旦验证了登录名和密码信息,此动画将通过登录按钮启动,因此您必须在自己的应用程序中设置代码来完成此操作。

所以这里是控制器的代码:

public class MainController {
    private static final int COLUMNS  = 4;
    private static final int COUNT    = 12;
    private static final int OFFSET_X = 0;
    private static final int OFFSET_Y = 0;
    private static final int WIDTH    = 130;
    private static final int HEIGHT   = 130;
    @FXML private ImageView loginAnimationImageView;
    @FXML private TextField loginTextField;
    @FXML private PasswordField passwordTextField;
    @FXML private Button loginButton;

    @FXML protected void onEnterTextFieldAction(ActionEvent actionEvent) {
}

    @FXML protected void onEnterPasswordFieldAction(ActionEvent actionEvent) {
}

    @FXML protected void loginButtonAction(ActionEvent actionEvent) {
        final Animation animation = new SpriteAnimation(
                loginAnimationImageView,
                Duration.millis(400.0),
                COUNT, COLUMNS,
                OFFSET_X, OFFSET_Y,
                WIDTH, HEIGHT
        );

        animation.setCycleCount(1);
        animation.play();
    }
}

就是这样,您无需为任何 JavaFX 应用程序创建这个简单的动画。尽情享受吧!

【讨论】:

  • SpriteAnimation 使用 ImageView setViewport() 来处理不同的 sprite,经过一些实验我发现使用单独的 Image (setImage()) 与 setViewport 相比具有更好的性能 >100%。
猜你喜欢
  • 2023-04-04
  • 2018-01-16
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2019-05-29
  • 1970-01-01
相关资源
最近更新 更多