JavaFx ImageView 不会在绑定到 VBox 的 fitWidthProperty 上调整大小

问题描述 投票:0回答:3

我正在尝试重新调整图像大小,以便它适合(缩放)由

SplitPane
分配的空间(解决方法建议:该图像在运行程序时会发生变化,它应该从文本生成)。我当前使用的 ImageView 更改了
SplitPane
Divider
的位置。分隔线永远不会导致
ImageView
减小到超过图像的大小。

Two images showing this behavior.

ImageView
fitWidthProperty 链接到
VBox
并没有给我带来我期望的调整大小行为。增加
ImageView
的大小确实有效(在没有绑定的情况下它是固定的),但图像永远不会增长,它只会在其周围产生边框。

我的MWE:

sample.fxml

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<SplitPane orientation="HORIZONTAL" xmlns:fx="http://javafx.com/fxml">
    <TextArea fx:id="textArea"/>
    <VBox fx:id="vBox" alignment="CENTER">
        <HBox fx:id="hBox" alignment="CENTER">
            <ImageView fx:id="imageView"/>
        </HBox>
    </VBox>
</SplitPane>

Controller.java

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Controller {
    @FXML
    private TextArea textArea = new TextArea();
    @FXML
    private VBox vBox = new VBox();
    @FXML
    private HBox hBox = new HBox();
    @FXML
    private ImageView imageView = new ImageView();

    public Controller() {
        imageView.fitWidthProperty().bind(vBox.widthProperty());
        imageView.fitHeightProperty().bind(hBox.heightProperty());
    }

    public void start() {
        textArea.setText("text");

        Image image = new Image("https://www.google.nl/images/srpr/logo11w.png");
        imageView.setImage(image);
    }
}

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Controller controller = new Controller();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
        fxmlLoader.setController(controller);

        final Parent root = fxmlLoader.load();
        primaryStage.setTitle("MWE");
        final Scene scene = new Scene(root, 640, 480);
        primaryStage.setScene(scene);
        primaryStage.show();

        controller.start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
javafx imageview java-8 fxml splitpane
3个回答
2
投票

您可以尝试将 ImageView 的

fitWidthProperty()
绑定到 SplitPane 的分隔符位置属性。

与此类似的绑定应该可以工作:

imageView.fitWidthProperty().bind(Bindings.subtract(1, 
               splitPane.getDividers().get(0).positionProperty())
                                      .multiply(splitPane.widthProperty());

1
投票

这是我给你的样品!

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<StackPane fx:controller="sample.Controller"
       xmlns:fx="http://javafx.com/fxml" alignment="center">

<SplitPane fx:id="splitpane">
    <VBox fx:id="left" prefWidth="300" prefHeight="200" style="-fx-background-color: antiquewhite">
        <ImageView fx:id="image1" preserveRatio="true" pickOnBounds="true">
            <image>
                <Image url="@/sample/test.jpg"/>
            </image>
        </ImageView>
    </VBox>
    <VBox fx:id="right" prefWidth="300" prefHeight="300" style="-fx-background-color: aquamarine">
        <ImageView fx:id="image2" preserveRatio="true" pickOnBounds="true">
            <image>
                <Image url="@/sample/test.jpg"/>
            </image>
        </ImageView>
    </VBox>
</SplitPane>

package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.SplitPane;
import javafx.scene.image.ImageView;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    @FXML
    ImageView image1;
    @FXML
    ImageView image2;
    @FXML
    SplitPane splitpane;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
   splitpane.getDividers().get(0).positionProperty().addListener((observable, oldValue, newValue) -> {
        System.out.println(newValue);
        int w = 600;
        double w1 = w * newValue.doubleValue();
        double w2 = w - w1;
        image1.setFitWidth(w1);
        image2.setFitWidth(w2);
    });
   }
}


0
投票

问题在于 SplitPane 文档

当节点的最小尺寸达到时,向左/顶部移动的分隔线将停止 达到了。

由于某种原因,SplitPane 专门使用其子级的 minHeight(double) 和 minWidth(double) 函数来获取这些值(而不是本身的最小设置)。所以我的看法是创建一个 ImageView 并覆盖这些函数

ImageView imageView = new ImageView() {
    @Override public double minHeight(double width) { return 80; }; //some hard stop value
    @Override public double minWidth(double height) { return 80; };
};

然后将该 ImageView 放入 StackPane 中,将其放入 SplitPane 中,然后绑定 fitWidth 和 fitHeight 属性

SplitPane...
StackPane imagePane = new StackPane(imageView);
splitPane.getItems().add(imagePane);
imageView.fitWidthProperty().bind(imagePane.widthProperty());
imageView.fitHeightProperty().bind(imagePane.heightProperty());
imageView.setPreserveRatio(true);
© www.soinside.com 2019 - 2024. All rights reserved.