我想使用GWT的clientBundle功能只使用GWTCanvas加载1个由多个精灵组成的图像.我最初的想法是将ImageResource转换为ImageElement,但显然这似乎不起作用:
public interface Bundle implements ClientBundle{
public static Bundle INSTANCE = GWT.create(Bundle .class);
@Source("/img/tile1.png")
public ImageResource tile1()
}
final GWTCanvas canvas = new GWTCanvas(400,400);
canvas.drawImage(ImageElement.as(new Image(Bundle.INSTANCE.tile1()).getElement()), 0, 0);
Run Code Online (Sandbox Code Playgroud)
我尝试首先将图像添加到RootPanel(强制加载),但这似乎也不起作用.也许时间不正确.有没有人知道如何使用GWTCanvas绘制imageResource?
这有效:(GWT 2.4)
// load:
SafeUri uri= imageResource.getSafeUri();
ImageElement imgElement= ImageElement.as((new Image(uri)).getElement());
// render
context.drawImage(imgElement, 0, 0);
Run Code Online (Sandbox Code Playgroud)
以您想要的方式使用 ClientBundled 图像是不可能的。组合成一张大图像后显示为背景图像,这是基于浏览器仅显示图像的一部分的特性。GWT 将此称为“剪辑”模式。因此,当您尝试获取该图像的元素时,实际的 src 标记为空,因为该图像是背景图像。如果您想在画布上显示图像,它必须是图像的实际链接。