将ImageView调整为所有可用空间

时间:2018-05-18 17:34:54

标签: java javafx fxml image-resizing

我过去一周看过很多不同的答案,但我找不到任何答案。目前我在ImageView上有一个奇怪的“类”:

package com.grimlytwisted.programs.screens.editor;

import com.grimlytwisted.programs.Systats;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.net.URL;
import java.util.ResourceBundle;

public class Editor implements Initializable {

    @FXML private ImageView imageDisplay;
    @FXML private Label nameOfImage;
    @FXML private Label sizeOfImage;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.setDefaultImage();
    }

    private void setDefaultImage() {

        System.out.println(Systats.getScreenHeight());
        imageDisplay.autosize();
        imageDisplay.setImage(new Image("images/dirt.jpg", imageDisplay.getFitWidth(), imageDisplay.getFitHeight(), true, false));
    }
}

...如果它对我所有的FXML文件都有帮助:

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

<?import javafx.geometry.Insets?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="Editor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.grimlytwisted.programs.screens.editor.Editor">
   <bottom>
      <HBox id="bottomContainer" stylesheets="@styles/coloring.css" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="nameOfImage" text="nameOfImage" />
            <Region HBox.hgrow="ALWAYS" />
            <Label fx:id="sizeOfImage" text="sizeOfImage" />
         </children>
         <opaqueInsets>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
         </opaqueInsets>
         <padding>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
         </padding>
      </HBox>
   </bottom>
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </children>
      </VBox>
   </top>
   <left>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1">
            <content>
              <AnchorPane id="tabPane" fx:id="tabPane" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" stylesheets="@styles/coloring.css" />
            </content>
          </Tab>
          <Tab text="Untitled Tab 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </left>
   <center>
      <ImageView id="imageDisplay" fx:id="imageDisplay" cache="true" cacheHint="SPEED" depthTest="ENABLE" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" smooth="false">
         <viewport>
            <Rectangle2D />
         </viewport>
      </ImageView>
   </center>
</BorderPane>

最后,图像导致了这个: Image Overflow

所以它溢出到我的菜单和我的BottomContainer区域,似乎只是在宽度方面受限制。它显然没有高度的边界,我需要它只有宽度/高度,没有“拉伸”。

我目前想要保留像素比率,并在达到最大值之前尽可能多地缩放图像。

1 个答案:

答案 0 :(得分:2)

ImageView换成Pane并将其fitWidth / fitHeight属性绑定到此Pane的大小。另外,使ImageView不受管理并设置Pane的首选大小:

<center>
    <Pane fx:id="container" prefWidth="100" prefHeight="100">
        <children>
            <ImageView id="imageDisplay" fx:id="imageDisplay" cache="true" cacheHint="SPEED" depthTest="ENABLE" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true"
                       fitWidth="${container.width}" fitHeight="${container.height}"
                       smooth="false" managed="false"/>
        </children>
    <Pane>
</center>
private void setDefaultImage() {
    imageDisplay.setImage(new Image("images/dirt.jpg"));
}