我已经为场景放置了一些图像视图,所有图像视图都指向不同的图像 视口。
生成fxml:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="80.0" layoutY="91.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../Pictures/sas.png" />
</image>
<viewport>
<Rectangle2D height="50.0" minY="50.0" width="50.0" />
</viewport>
</ImageView>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="308.0" layoutY="91.0" pickOnBounds="true" preserveRatio="true">
<viewport>
<Rectangle2D height="50.0" minX="50.0" minY="50.0" width="50.0" />
</viewport>
<image>
<Image url="@../../../../../Pictures/sas.png" />
</image>
</ImageView>
<ImageView layoutX="225.0" layoutY="250.0">
<image>
<Image url="@../../../../../Pictures/sas.png" />
</image>
</ImageView>
</children>
问题是,这会在内存中创建3个相同图像的实例吗? 如果是,那么什么是避免它的最佳方法,url(&#34;&#34;)在style属性,css类中?我想避免为每个单独的图标创建css类!
是否值得使用单个大型图像用于多个图标&amp; UI元素,而不是每天的小图像,以及年龄?
答案 0 :(得分:1)
快速测试显示ImageView
s在内存中不引用相同的Image
。
为此,您可以按照自己的描述使用css,也可以使用Image
块在FXML中定义<fx:define>
一次,并通过其fx:id
属性引用它:
<fx:define>
<Image url="@../../../../../Pictures/sas.png" fx:id="sasImage" />
</fx:define>
<ImageView image="$sasImage" fitHeight="150.0" fitWidth="200.0" layoutX="80.0" layoutY="91.0" pickOnBounds="true" preserveRatio="true">
<viewport>
<Rectangle2D height="50.0" minY="50.0" width="50.0" />
</viewport>
</ImageView>
<ImageView image="$sasImage" fitHeight="150.0" fitWidth="200.0" layoutX="308.0" layoutY="91.0" pickOnBounds="true" preserveRatio="true">
<viewport>
<Rectangle2D height="50.0" minX="50.0" minY="50.0" width="50.0" />
</viewport>
</ImageView>
<ImageView image="$sasImage" layoutX="225.0" layoutY="250.0">
</ImageView>