JavaFX图像库类似于Picasso或Glide

问题描述 投票:0回答:1

我正在寻找一个允许我以与Picasso或Glide for Android / Java相同的方式加载图像的库,我正在使用JavaFX 2在桌面应用程序中工作,图像加载正在堵塞主线程。我考虑过从头开始创建自定义功能,但我认为它们是一套成熟的工具,可以避免额外的工作。我曾尝试在其线程中加载图像,但它无法正常工作。

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    final String thumbnail = data.getSong().getThumbnail();

    Runnable runnable = () -> {
        Image image = new Image(thumbnail);
        imageView.setImage(image);
    };
    runnable.run();
}
java caching javafx picasso glade
1个回答
0
投票

对于重复图像图像加载,我建议使用javafx动画工具之一。 PauseTransition是一个选择:

import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SlideShow extends Application{

    private static int counter =0;
    private ImageView iv;
    private Image[] images;
    private Button swapImage;

    private final String[] urls = {
            "https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/160/lg/57/tropical-fish_1f420.png",
            "https://www.shareicon.net/data/128x128/2015/03/28/14104_animal_256x256.png",
            "https://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/apps/gnome-fish.png",
            "http://www.iconsalot.com/asset/icons/freepik/pet-shop-13/128/010-fish-2-icon.png"
    };

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        images = new Image[urls.length];
        for(int i=0; i< urls.length; i++) {
            images[i] = new Image(urls[i], true);
        }

        iv = new ImageView(images[counter++]);
        swapImage = new Button("Start");
        swapImage.setOnAction(e->update());
        BorderPane root = new BorderPane(iv);
        root.setBottom(new StackPane(swapImage));
        Scene scene = new Scene(root,200,200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void update() {

        swapImage.setDisable(true);
        PauseTransition pause = new PauseTransition(Duration.seconds(1));
        pause.setOnFinished(event ->{
            swapImage();
            pause.play();
        });
        pause.play();   
    }

    private void swapImage() {
        counter = counter % images.length;
        iv.setImage(images[counter++]);
    }

    public static void main(String[] args)  {
        Application.launch(args);
    }
}

或者你可以通过修改Timeline来使用update()

private void update() {

    swapImage.setDisable(true);
    KeyFrame keyFrame = new KeyFrame(
            Duration.seconds(1),
            event -> swapImage()
    );

    Timeline timeline = new Timeline();
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.getKeyFrames().add(keyFrame);
    timeline.play();
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.