我看到了这篇博客文章,它通过使用GWT DataResource而非ImageResource解决了问题。结果发现,如果按照以下方式使用ImageResource,则相同的技术实际上也能够奏效:
Image image = new Image(myImageResource.getURL());
image.setPixelSize(getLength(), getHeight());
保持宽高比的计算方法如下:
Image image = new Image(myImageResource.getURL());
image.setPixelSize(newWidth, myImageResource.getHeight() * newWidth / myImageResource.getWidth());
从 GWT 2.4 开始使用(请参见这里):
Image image = new Image(myImageResource.getSafeUri());
image.setPixelSize(newWidth, myImageResource.getHeight() * newWidth / myImageResource.getWidth());
例如,我们在资源中有
@Source("images/logo.png")
ImageResource getLogo();
在 UiBinder 模板中声明 <ui:with>
元素:<ui:with field='res' type='com.myapp.client.Resources'/>
以下是:
<g:Image url='{res.getLogo.getSafeUri.asString}' pixelSize="Width, Height" />
在旧版 GWT 中:
<g:Image url='{res.getLogo.getURL}' pixelSize="Width, Height" />
但现在已被弃用。
不要使用:
<g:Image resource='{res.getLogo}' pixelSize="Width, Height" />
因为它不能缩放图片
我的最终解决方案是在图像上添加一个加载处理程序,根据加载的图像尺寸进行调整大小(即保持比例)。
image.addLoadHandler(new LoadHandler() {
@Override
public void onLoad(LoadEvent event) {
Element element = event.getRelativeElement();
if (element == image.getElement()) {
int originalHeight = image.getOffsetHeight();
int originalWidth = image.getOffsetWidth();
if (originalHeight > originalWidth) {
image.setHeight(MAX_IMAGE_HEIGHT + "px");
} else {
image.setWidth(MAX_IMAGE_WIDTH + "px");
}
}
}
});
其中MAX_IMAGE_WIDTH
和MAX_IMAGE_HEIGHT
是常量,用于确定图像允许的最大尺寸。
package de.tu_freiberg.informatik.vonwenckstern.client;
// class is written by Michael von Wenckstern, and is also used in ist diploma Thesis
// (this is only for my super visor, that he does not think I copied it from stackoverflow
// without mention the source) - this class is free to use for every body
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Image;
public class ScalableImage extends Image {
private int width;
private int height;
private ImageResource res;
public ScalableImage(ImageResource res, int width, int height) {
this.setUrl(res.getSafeUri());
this.res = res;
this.width = width;
this.height = height;
}
@Override
public void onLoad() {
int widthOfBigImage = this.getOffsetWidth();
int heightOfBigImage = this.getOffsetHeight();
double scaleX = width / res.getWidth();
double scaleY = height / res.getHeight();
this.setResource(res);
DOM.setStyleAttribute(getElement(), "backgroundPosition", Integer.toString((int) (res.getLeft() * -1 * scaleX))+"px "+
Integer.toString((int) (res.getTop() * -1 * scaleY))+"px ");
DOM.setStyleAttribute(getElement(), "backgroundSize", Integer.toString((int) (widthOfBigImage * scaleX))+"px "+
Integer.toString((int) (heightOfBigImage * scaleY))+"px ");
this.setPixelSize((int) (res.getWidth()* scaleX), (int) (res.getHeight() * scaleY));
}
}
rootPanel.add(new ScalableImage(Images.Util.getImages().img0(), 60, 60));
for(ImageResource imRes: latexIconsClientBundle) {
ScalableImage im = new ScalableImage(imRes, 60, 60);
RootPanel.get().add(im); // PushButton class does not fire onAttach event, so we need to attach the image to the RootPanel
PushButton btn = new PushButton(im);
btn.setPixelSize(60, 60);
if(++col > 9) {
row++;
col = 0;
}
this.setWidget(row, col, btn);
}
ImageResource
,例如为带有图标的某个Tree
组件使用一些com.sencha.gxt.data.shared.IconProvider
,则此方法无效。 - Andreas Covidiot以下是我如何使用HTML5的画布元素来缩放图像。
http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5
布兰登·唐纳森 http://gwt-examples.googlecode.com http://c.gawkat.com
从我的所有测试来看,只有在您的捆绑包中有一个图像时,safeURI 才有效...因此使用它是无用的,因为您的每个捆绑包都有一个 cache.png,所以它并没有优化任何内容!
ImageResource
的目的,不是吗?这是否意味着每次调用此方法时都要从服务器加载图像? - Marconius