在GWT中缩放图像 [英] Scaling an Image in GWT
问题描述
更改图片 GWT中的Widget改变图像元素的大小,但不会重新缩放屏幕上的图像。因此,以下内容不起作用:
Image image = new Image(myImageResource);
image.setHeight(newHeight);
image.setWidth(newWidth);
image.setPixelSize(newWidth,newHeight);
这是因为GWT实现了它的图片小部件通过设置 如何获取实际图片的大小? 我看到这个博客条目,它通过使用GWT DataResource而不是ImageResource来解决问题。事实证明,相同的技术将实际上与ImageResource一起工作,如果您使用它如下: myImageResource.getURL()); background-image $ b
image.setPixelSize(getLength(),getHeight());
保持宽高比计算如下:
Image image = new Image(myImageResource.getURL());
image.setPixelSize(newWidth,myImageResource.getHeight()* newWidth / myImageResource.getWidth());
从GWT 2.4开始,使用(参见 here ):
Image image = new Image(myImageResource.getSafeUri());
image.setPixelSize(newWidth,myImageResource.getHeight()* newWidth / myImageResource.getWidth());
Changing the size of an Image Widget in GWT changes the size of the image element, but does not rescale the image on the screen. Therefore, the following will not work:
Image image = new Image(myImageResource);
image.setHeight(newHeight);
image.setWidth(newWidth);
image.setPixelSize(newWidth, newHeight);
This is because GWT implements its Image widget by setting the background-image
of the HTML <img... />
element as the image, using CSS.
How does one get the actual image to resize?
I saw this blog entry, which solves the problem by using a GWT DataResource instead of ImageResource. It turns out that the same technique will actually work with ImageResource, if you use it as follows:
Image image = new Image(myImageResource.getURL());
image.setPixelSize(getLength(), getHeight());
To keep aspect ratio calculate it like:
Image image = new Image(myImageResource.getURL());
image.setPixelSize(newWidth, myImageResource.getHeight() * newWidth / myImageResource.getWidth());
As of GWT 2.4, use (see here):
Image image = new Image(myImageResource.getSafeUri());
image.setPixelSize(newWidth, myImageResource.getHeight() * newWidth / myImageResource.getWidth());
这篇关于在GWT中缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!