我一直在学习JavaFX。
我制作的一个样本是图像查看器 显示文件夹中的所有图像。
然而,应用程序运行速度非常慢,在任务管理器中需要900-1000MB内存。
将图像逐个添加到数组中效果更好,但仍然不如它应该的那么快。
理想情况下,我希望它能显示至少200-300张图像
任何保持记忆力的方法,也许不会让所有东西都爬行
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
import java.net.MalformedURLException;
import java.util.ArrayList;
public class Main extends Application {
ArrayList<File> files = new ArrayList<>();
final ScrollPane sp = new ScrollPane();
final VBox vb = new VBox();
final Label fileName = new Label();
/*final File [] imageNames = new File[]{
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 01.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 02.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 03.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 04.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 05.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 06.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 07.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 08.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 09.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 10.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 11.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 12.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 13.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 14.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 15.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 16.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 17.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 18.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 19.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 20.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 21.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 22.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 23.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 24.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 25.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 26.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 27.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 28.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 29.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 30.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 31.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 32.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 33.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 34.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 35.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 36.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 37.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 38.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 39.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 40.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 41.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 42.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 43.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 44.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 45.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 46.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 47.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 48.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 49.jpg"),
new File("C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images\\08-05-10 01 50.jpg")
};
final Image[] images = new Image[imageNames.length];
final ImageView[] pics = new ImageView[imageNames.length];
*/
@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 600, 400);
stage.setScene(scene);
stage.setTitle("Scroll Pane");
box.getChildren().addAll(sp, fileName);
VBox.setVgrow(sp, Priority.ALWAYS);
/*
for (int i = 0; i < imageNames.length; i++) {
try {
images[i] = new Image(imageNames[i].toURI().toURL().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
pics[i] = new ImageView(images[i]);
// pics[i].setFitWidth(100);
pics[i].setPreserveRatio(true);
vb.getChildren().add(pics[i]);
}*/
initialize();
//sp.setVmax(440);
//sp.setPrefSize(100, 100);
sp.setContent(vb);
stage.show();
}
void initialize(){
getImages(new File( "C:\\Users\\user\\IdeaProjects\\untitled\\src\\sample\\images"));
for(File file: files){
addImage(file);
}
files.clear();
}
void addImage(File f){
// load the image
Image image = null;
try {
//FileInputStream inputStream=new FileInputStream(f);
image = new Image(f.toURI().toURL().toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
ImageView iv = new ImageView();
iv.setImage(image);
//iv.setFitWidth(100);
iv.setPreserveRatio(true);
iv.setSmooth(true);
iv.setCache(true);
vb.getChildren().add(iv);
}
void getImages(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
getImages(new File(dir, children[i]));
}
}else
{
files.add(dir);
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
你的影像有多大?例如,如果一个图像大约10 MB,那么100个图像将解释您的内存使用情况...... 我用大约200张图片测试了它,占用了大约1.9 GB的空间。 当然,如果你加载它们的原始大小,它将会很慢。 但您可以在加载期间指定图像大小:
void addImage(File f) {
// load the image
Image image = null;
try {
// much better memory footprint
image = new Image(f.toURI().toURL().toString(),400,400,true, true, true);
//image = new Image(f.toURI().toURL().toString()); // bad idea with many big images
} catch (MalformedURLException e) {
e.printStackTrace();
}
ImageView iv = new ImageView();
iv.setImage(image);
iv.setPreserveRatio(true);
iv.setSmooth(true);
iv.setCache(true);
vb.getChildren().add(iv);
}