如何做RCP里的图片资源管理
这里我写了一中读取资源图片管理的方法,这个方法的好处就是,加载过的图片把它放在MAP里,下次再用的时候不用重新加载。
1.在这个plugin的最顶层目录下创建icons文件,然后把图片放在这个文件夹里。
2.在这个plugin里的Activator(默认是这个类名,一般自己会改成其他和PLUGIN相关的名字)里写static方法,这方法能根据图片所在地路径名字取得图片
public class Activator extends AbstractUIPlugin {
......
public static Image getImage(String path) {
        return getImage(getDefault().getImageRegistry(), path.toString());
    }
public static Image getImage(ImageRegistry registry, String path){
        try{
            Image img= registry.get(path);
            if(img==null){
                ImageDescriptor desc= Activator.imageDescriptorFromPlugin(Activator.class.getPackage().getName(), path);
                registry.put(path, desc);
                img= registry.get(path);
            }
            return img;
        }catch(MissingResourceException e){
            e.printStackTrace();
        }
        return null;
        
}
......
}注意图片路径可以为/icons/xxx.gif,也可以为icons/xxx.gif.因为内部代码会自动转化。如果不用这种办法取得资源路径,那么只能用/icons/xxx.gif绝对路径。OYEA~
3.然后就可以在别的类里根据图片路径读出IMG。
例如。
class ImgShows{
......
public Img getImg(String path){
Image img=Activator.getImage(path);
}
......
}4.如果想把这个图放在CLabel里。最好这么写。这里我用formLayout.
Image img=Activator.getImage(imgPath.toString());
        CLabel icon = new CLabel(headComposite, SWT.NONE);
        icon.setBackground(img);
        final FormData iconFormData = new FormData();
        iconFormData.top = new FormAttachment(LAYOUT.ZERO_PERCENT, LAYOUT.MARGIN_TOP_COMPOSITE);
        iconFormData.left = new FormAttachment(LAYOUT.ZERO_PERCENT, LAYOUT.MARGIN_LEFT_COMPOSITE);
        iconFormData.width=img.getBounds().width;
        iconFormData.height=img.getBounds().height;
        icon.setLayoutData(iconFormData);这样的图大小工整。
相关推荐
  Chenliaoyuan    2020-06-11  
   moyigg    2020-04-20  
   Chenliaoyuan    2020-04-14  
   Donven    2019-09-07  
   startXUEBA    2018-03-24  
   88971730    2011-08-18  
   jackiebobo    2011-04-08  
   ccgsteel    2019-06-30  
   aSuncat    2018-03-24  
 <div class="panel-body" style="height: 300px; width: 100%; overflow: auto;" data-platform-scroll="&q
  道北吴彦祖    2017-01-15  
   乔乔    2016-07-20  
   goodyatou    2016-03-27  
 <link rel="apple-touch-icon-precomposed" sizes="114x114" href="icons/apple-touch-icon-114x114-precom
  道北吴彦祖    2015-09-29  
   Pig00    2019-06-27  
 