一 eclipse Image 的分类
1. org.eclipse.swt.graphics.Image, 代表一个重量级的对象,它在内存中处理数据, 它持有一个底层OS资源的一个句柄, 如果你不需要它时, 一定要手动关闭。
2. org.eclipse.jface.resource.ImageDescriptor, 一个轻量级对象, 它调用createImage() 方法生成Image.
3. org.eclipse.jface.resource.ImageRegistry, 如果某一些Image对象被频繁使用,则我们可以把它共享出来。 还有一种方式是 LabelProvider.
二. 在Plug.xml 中定义Image
在plug.xml中定义Image的优点
a. 所有的创建,销毁工作有Platform 来完成,用户不必操心.
b. 如果更换图片也很容易, 只要在xml中重新配置即可.
c. 懒加载机制, 图片只有在真正需要的时候才会被加载.
三、在custon ui 中添加Image
public class MyAction extends Action { private static ImageDescriptor image; static { URL url = null; try { url = new URL(MyPlugin.getInstance().getDescriptor().getInstallURL(), "images/my_action.gif"); } catch (MalformedURLException e) { } image = ImageDescriptor.createFromURL(url); }
三.五 Viewer 和 LabelProvider
public class FruitLabelProvider extends LabelProvider {
private Image appleImage = new Image(…);
private Image kiwiImage = new Image(…);
public Image getImage(Object object) {
if (object.getClass() == Apple.class) {
return appleImage;
}
if (object.getClass() == Kiwi.class) {
return kiwiImage;
}
return null;
}
public String getLabel(Object o) {
//return appropriate labels for the various fruits
}
public void dispose() {
appleImage.dispose();
appleImage = null;
kiwiImage.dispose();
kiwiImage = null;
}
}
这些创建的Image必须要在dispose() 方法中销毁。
四. ImageRegistry
ImageRegistry 是用于频繁使用的Image的, Image的创建和销毁有 插件来管理, 当plug-in shutdown时
, ImageRegistry也跟做销毁Image。
org.eclipse.ui.plugin.AbstractUIPlugin.getImageRegistry() 方法得到
五,Using Global Images Provided by Other Plug-ins
PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
Important: Images provided by these APIs are shared, which means you must not dispose of them. Again, following the general rule, since you didn't create the image, it's not your responsibility to dispose of it.