插件开发:如何获取工程中的图片?

获取工程中的图片

在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中好像代码更加精简了一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值