Reusing same ImageView multiple times in the same scene on JavaFX

后端 未结 3 817
栀梦
栀梦 2021-01-20 19:28

I\'m trying to build a Twitter-style ListView, and I couldn\'t reuse the same ImageView multiple times in the same list. Loading multiple copies seem to be wasteful and caus

相关标签:
3条回答
  • 2021-01-20 20:10

    Do something like this:

    ImageView image = ...
    ImageView src   = ...
    image.setImage(src.getImage());
    
    0 讨论(0)
  • 2021-01-20 20:12

    A scenegraph cannot contain the same Node twice in JavaFX, and there is no way of cloning nodes (as far as I know).

    A workaround would perhaps be to make your map a HashMap store Image instead of ImageView, and change the last row to

    setGraphic(new ImageView(images.get(imageUrl)));
    

    This way you'll at least cache the loading of the actual Image, which should really be the heavy lifting part.

    0 讨论(0)
  • 2021-01-20 20:15

    Caching images is good way.

    Also You can load images in background, it greatly improve performance.

    public Image getImage(String path, boolean backload) {
       image = imageCache.get(path);
       if (image == null) {
          image = new Image(path, backload);
          imageCache.put(path, image);
       }
       return image;
    }
    
    0 讨论(0)
提交回复
热议问题