我正在尝试构建一个Twitter风格的ListView,并且我不能在同一列表中多次重用相同的ImageView。加载多个副本似乎很浪费,并且由于UI虚拟化导致滚动速度变慢。有什么变通方法吗?
public class TwitterCell extends ListCell<Object> {
private static HashMap<String, ImageView> images = new HashMap<String, ImageView>();
@Override
protected void updateItem(Object tweet, boolean empty) {
super.updateItem(tweet, empty);
Tweet t = (Tweet) tweet;
if (t != null) {
String message = t.getMessage();
setText(message);
String imageUrl = t.getImageUrl();
if (!images.containsKey(imageUrl)) {
images.put(imageUrl, new ImageView(imageUrl));
}
setGraphic(images.get(imageUrl));
}
}发布于 2013-03-03 00:45:20
一个场景图不能在JavaFX中包含两次相同的Node,并且没有方法克隆节点(据我所知)。
解决方法可能是将地图设置为HashMap存储Image,而不是ImageView,并将最后一行更改为
setGraphic(new ImageView(images.get(imageUrl)));这样,您至少可以缓存实际Image的加载,这实际上应该是繁重的部分。
发布于 2013-03-06 21:44:39
缓存图像是一种很好的方法。
你也可以在后台加载图片,这将极大地提高性能。
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;
}发布于 2015-06-26 01:01:12
这样做:
ImageView image = ...
ImageView src = ...
image.setImage(src.getImage());https://stackoverflow.com/questions/15175022
复制相似问题