在 GWT 中缩放图像
更改
Image image = new Image(myImageResource);
image.setHeight(newHeight);
image.setWidth(newWidth);
image.setPixelSize(newWidth, newHeight);
这是因为 GWT 实现了其 Image 小部件,通过设置 HTML
元素的 background-image
作为图像,使用 CSS。
如何调整实际图像的大小?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我看到此博客条目,它解决了使用 GWT DataResource 而不是 ImageResource 会出现问题。事实证明,相同的技术实际上适用于 ImageResource,如果您按如下方式使用它:
要保持宽高比,请按如下方式计算:
从 GWT 2.4 开始,使用(请参阅 此处):
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:
To keep aspect ratio calculate it like:
As of GWT 2.4, use (see here):
如果我们想在 UIbinder 中做同样的事情。从外部资源,然后:
例如,我们在资源
中在 UiBinder 模板中声明
元素:以及以下:
在较旧的 GWT 版本中:
但现在 - 已弃用。
不要使用:
因为它不会缩放图像
If we want to do the same in UIbinder. From an external resource then :
For example we have in recource
In UiBinder template declare the
<ui:with>
element:and below:
in older GWT version:
but now - Deprecated.
Do not use:
becouse it doesn't scale images
尝试使用图像加载器小部件 http://code.google.com/p/gwt-image -装载机
FitImage 类提供了您正在寻找的内容。
PS:还应用问题中的补丁,因为我已经修复了一些小错误
Try the image loader widget http://code.google.com/p/gwt-image-loader
The FitImage class provides what you are looking for.
PS: apply also patches from issues, as there are some minor bugs which I have fixed
我的最终解决方案是在图像上添加一个加载处理程序,以根据加载图像的尺寸(即尊重比率)调整其大小。
其中 MAX_IMAGE_WIDTH 和 MAX_IMAGE_HEIGHT 是确定图像允许的最大尺寸的常量。
my final solution was to add a load handler on the image to resize it according to the dimensions of the loaded image (i.e. respectnig the ratio).
where
MAX_IMAGE_WIDTH
andMAX_IMAGE_HEIGHT
are constants that determine the maximum allowed size of the image.我编写了一个接受 ImageResource 对象的类,您可以设置所需的图像像素大小。它使用CSSbackground-Position和CSSbackground-size来达到目的:
ScalableImage类的源代码是:
您可以按如下方式使用此类:
如果您将此类与PushButton一起使用,则必须添加PushButton到 RootPanel,因为否则不会调用 onLoad() 函数。
示例源代码如下所示:
I wrote a class which accepts a ImageResource object, and you can set the wanted Pixel size of the Image. It uses CSS background-Position and CSS background-size to achive the aim:
The source code of the class ScalableImage is:
You can use this class as followed:
If you use this class together with a PushButton, than you have to add the PushButton to the RootPanel, because otherwise the onLoad() function is not called.
An example source code would look like:
以下是我如何使用 canvas 元素通过 HTML5 缩放图像。
http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5布兰登
·唐纳尔森
http://gwt-examples.googlecode.com
http://c.gawkat.com
Here is how I use the canvas element to scale images using HTML5.
http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5
Brandon Donnelson
http://gwt-examples.googlecode.com
http://c.gawkat.com
根据我的所有测试,只有当您的捆绑包中有一张图像时,safeURI 才起作用...所以使用它毫无用处,因为您有一个 Bundle 的 cache.png,所以它没有任何优化!
From all my test, safeURI work only if you have ONE image in your bundle...So useless to use it because you have one cache.png by Bundle, so it is optimize nothing !