2014-10-29 50 views
0

我已將幾個圖像視圖放置到場景中,全都指向具有不同 視口的相同圖像。JavaFX Scene Builder,具有相同圖像的多個ImageView

生成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(「」)在style屬性中,css類?我想避免爲每個圖標創建CSS類!

它是否值得使用單個大圖像爲多個圖標& UI元素,而不是每天在這一天的小圖像&年齡?

回答

1

快速測試顯示ImageView s在內存中未引用相同的Image

爲了這樣做,因爲你描述你可以使用的CSS,或可以在FXML與<fx:define>塊定義一次Image並通過其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> 
+0

啊,我回答FXML術語:我不確定SceneBuilder是否支持''。 – 2014-10-29 16:32:22

+0

問題是使場景構建器無法讀取文件,這在大型項目中可能很乏味。所以堅持CSS可能是正確的方法。 – 2014-11-01 13:14:24

+0

css方法的一個缺點是它沒有在'ImageView'上設置'image'屬性。因此,如果您需要稍後用'ImageView.getImage()'在控制器中檢索圖像,您將無法; ''方法允許這樣做。在SceneBuilder中爲''提供支持可能值得[功能請求](https://javafx-jira.kenai.com/secure/Dashboard.jspa)。 – 2014-11-01 14:23:28

相關問題