获取工程中的图片
在eclipse采用osgi前一般是:在每一个工程中都有一个类似javaweb开发一样的启动类,叫xxxPlugin;
在eclipse采用osgi后此类一般叫做:xxxActivator;
下面是这些类的关系:
可以很明显看出,不管是xxxPlugin还是xxxActivator都是继承自AbstractUIPlugin类,而AbstractUIPlugin类又是继承Plugin的,所以都是有一定关系的;下面我放上的是eclipse采用osgi先后获取图片的方法实例。
**
一、eclipse采用osgi前
1、先放一段在xxxPlugin中获取图片的代码
/**
* getImage
*
* @param path
* @return
*/
public static Image getImage(String path)
{
ImageRegistry registry = PLUGIN.getImageRegistry();
Image image = registry.get(path);
if (image == null)
{
ImageDescriptor id = getImageDescriptor(path);
if (id == null)
{
return null;
}
registry.put(path, id);
image = registry.get(path);
}
return image;
}
2、直接获取
Image PROPERTY = xxxPlugin.getImage("/icons/js_property.png");
图片位置:
如果说在xxxPlugin中没有getImage的方法,那么一般是采用了一个工具类(UIUtils)去直接调用;
1、工具类中的代码:
public static Image getImage(AbstractUIPlugin plugin, String path)
{
ImageRegistry registry = plugin.getImageRegistry();
Image image = registry.get(path);
if (image == null)
{
ImageDescriptor id = getImageDescriptor(plugin.getBundle().getSymbolicName(), path);
if (id == null)
{
return null;
}
registry.put(path, id);
image = registry.get(path);
}
return image;
}
2、开发中的引用
private static final String DEFAULT_IMAGE = "icons/proposal.png";
Image image = UIUtils.getImage(xxxEditorPlugin.getDefault(), DEFAULT_IMAGE);
二、eclipse采用osgi后
1、下面是xxxActivator中获取图片的代码
public static Image getImage(String key) {
Image img = getDefault().getImageRegistry().get(key);
if (img == null) {
getDefault().getImageRegistry().put(key,
getImageDescriptor(key).createImage());
img = getDefault().getImageRegistry().get(key);
}
return img;
}
2、开发中的引用
Image image= Activator.getImage("icons/error.png");
对比中发现,似乎差别并不是很大,就是xxxActivator中好像代码更加精简了一些。