我实际上很惊讶地发现,尽管在 Windows 中使用了本机外观,但文件选择器确实没有缩略图视图。我试过你的例子,你走的是正确的路线,但我看到它对于有很多大图像的文件夹有多慢。当然,开销是由于读取文件内容然后解释图像时的 I/O 造成的,这是不可避免的。
更糟糕的是,我发现它FileView.getIcon(File)
被称为很多- 在文件列表显示之前,当您鼠标悬停在图标上时,以及选择更改时。如果我们在加载图像后不缓存它们,我们将一直毫无意义地重新加载图像。
显而易见的解决方案是将所有图像加载到另一个线程或线程池中,一旦我们得到缩小的结果,将其放入临时缓存中,以便再次检索它。
我玩了很多Image
,ImageIcon
我发现ImageIcon
可以随时通过调用来更改 an 的图像setImage(Image)
。这对我们来说意味着,在 内getIcon(File)
,我们可以立即返回一个空白或默认图标,但保留对它的引用,将其传递给工作线程,该工作线程将在后台加载图像并在完成后设置图标的图像(唯一的问题是我们必须打电话repaint()
来查看变化)。
对于这个示例,我使用ExecutorService
缓存线程池(这是获取所有图像的最快方式,但使用大量 I/O)来处理图像加载任务。我还使用 aWeakHashMap
作为缓存,以确保我们只在需要时保留缓存的图标。您可以使用另一种地图,但您必须管理您持有的图标数量,以避免内存不足。
package guitest;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileView;
public class ThumbnailFileChooser extends JFileChooser {
/** All preview icons will be this width and height */
private static final int ICON_SIZE = 16;
/** This blank icon will be used while previews are loading */
private static final Image LOADING_IMAGE = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_INT_ARGB);
/** Edit this to determine what file types will be previewed. */
private final Pattern imageFilePattern = Pattern.compile(".+?\\.(png|jpe?g|gif|tiff?)$", Pattern.CASE_INSENSITIVE);
/** Use a weak hash map to cache images until the next garbage collection (saves memory) */
private final Map imageCache = new WeakHashMap();
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFileChooser chooser = new ThumbnailFileChooser();
chooser.showOpenDialog(null);
System.exit(1);
}
public ThumbnailFileChooser() {
super();
}
// --- Override the other constructors as needed ---
{
// This initializer block is always executed after any constructor call.
setFileView(new ThumbnailView());
}
private class ThumbnailView extends FileView {
/** This thread pool is where the thumnnail icon loaders run */
private final ExecutorService executor = Executors.newCachedThreadPool();
public Icon getIcon(File file) {
if (!imageFilePattern.matcher(file.getName()).matches()) {
return null;
}
// Our cache makes browsing back and forth lightning-fast! :D
synchronized (imageCache) {
ImageIcon icon = imageCache.get(file);
if (icon == null) {
// Create a new icon with the default image
icon = new ImageIcon(LOADING_IMAGE);
// Add to the cache
imageCache.put(file, icon);
// Submit a new task to load the image and update the icon
executor.submit(new ThumbnailIconLoader(icon, file));
}
return icon;
}
}
}
private class ThumbnailIconLoader implements Runnable {
private final ImageIcon icon;
private final File file;
public ThumbnailIconLoader(ImageIcon i, File f) {
icon = i;
file = f;
}
public void run() {
System.out.println("Loading image: " + file);
// Load and scale the image down, then replace the icon's old image with the new one.
ImageIcon newIcon = new ImageIcon(file.getAbsolutePath());
Image img = newIcon.getImage().getScaledInstance(ICON_SIZE, ICON_SIZE, Image.SCALE_SMOOTH);
icon.setImage(img);
// Repaint the dialog so we see the new icon.
SwingUtilities.invokeLater(new Runnable() {public void run() {repaint();}});
}
}
}
已知的问题:
1)缩放时我们不保持图像的纵横比。这样做可能会导致图标尺寸奇怪,从而破坏列表视图的对齐方式。解决方案可能是创建一个BufferedImage
16x16 的新图像,并在其顶部渲染缩放图像,居中。如果你愿意,你可以实现它!
2) 如果文件不是图像或已损坏,则根本不会显示任何图标。看起来程序只在渲染图像时检测到这个错误,而不是在我们加载或缩放它时,所以我们无法提前检测到这个。但是,如果我们修复问题 1,我们可能会检测到它。