Lazy loading of images in scrollpane
Christian Krampenschiesser
krampenschiesser at gmail.com
Mon Jul 13 18:19:10 UTC 2015
Hi all,
I am trying around since some time to load images when they are visible in
a scollpane.
However the only solution I got so far is quite complicated and done via
checking the bounds in the scene.
However sometimes the calculated bounds of the item in the scene are way
out of the scene.
I am really looking for a simpler solution then my current one.
My tests are fine so far with small amounts of thumbnails. however with big
scenes (~3000 thumbnails) everything goes wrong :(
But if there is none maybe someone can point out my wrong bounding box
calculations and why they are wrong. It seems I didn't understand
everything correctly:
protected SimpleObjectProperty<GalleryItem> item = new
SimpleObjectProperty<>();//contains, path and loads thumbnail + real
image
....
button.localToSceneTransformProperty().addListener(new
ChangeListener<Transform>() {//button has an imageview which displays
the thumbnail
@Override
public void changed(ObservableValue<? extends Transform> observable,
Transform oldValue, Transform newValue) {
Point2D point = button.localToScene(button.getLayoutX(),
button.getLayoutY());
Scene scene = button.getScene();
boolean isLayoutedCorrectly = button.getWidth() > 100 &&
button.getHeight() > 100;//hack to execute after layouting, otherwise
all items are visible
if (scene != null && button.getParent() != null &&
isLayoutedCorrectly && armed) {//armed is set 100ms after adding all
buttons to a flowpane in a scrollpane
BoundingBox sceneBox = new BoundingBox(scene.getX(),
scene.getY(), scene.getWidth(), scene.getHeight());
BoundingBox buttonBox = new BoundingBox(point.getX(),
point.getY(), button.getWidth(), button.getHeight());
if (sceneBox.intersects(buttonBox)) {//visible
if (imageView.getImage() == null) {
log.debug("Showing {}", button.getText());
Image image = item.get().loadThumbNail();
if (image != null) {
imageView.setImage(image);
imageView.setFitHeight(image.getHeight());
imageView.setFitWidth(image.getWidth());
}
}
}
}
}
});
Thanks!
Christian
More information about the openjfx-dev
mailing list