Image使用代价昂贵,JFace包提供了一个轻量级的ImageDescriptor类.
An image descriptor is an object that knows how to create an SWT image.It does not hold onto images or cache them, but rather just creates them on demand.
ImageDescriptor类是抽象类,不能用new,有2种常用static方法,createFromFile,createFromURL.
1:ImageDescriptor temp = ImageDescriptor.createFromFile(abc.class,"icons/temp.gif")
//在类abc目录下的icon目录下的图片temp.gif
//没有真正加载这个图像
2://绝对目录
URL url = new URL("file:\\c:\\icons\\temp.gif");
ImageDescriptor temp = ImageDescriptor.createFromURL(url)
//项目主目录
URL url = new URL("file:icons/temp.gif");
ImageDescriptor temp = ImageDescriptor.createFromURL(url)
3: Eclipse自带图像调用
ImageDescriptor temp = WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_HOME_NAV);
4: 得到image
Image image = temp.creatImage();
图像注册表ImageRegistry类:高速缓存图像和图像描述符
ImageRegistry image_registry = new ImageRegistry();
URL url = new URL("file:icons/folder.gif");
ImageDescriptor imaged = ImageDescriptor.createFromURL(url);
image_registry.put("folder",imaged);
//取出image
Image image = image_registry.get("folder");
//取出描述符
ImageDescriptor imageDescriptor = image_registry.getDescriptor("folder");
//删除图像
image_registry.remove("folder");
**项目开发初期,建立一个图像注册表
**网上教程