eclipse Image的使用

原文请看: http://www.eclipse.org/articles/Article-Using%20Images%20In%20Eclipse/Using%20Images%20In%20Eclipse.html

 

一 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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值