如何在GWT中预取图像? [英] How to prefetch image in GWT?

查看:142
本文介绍了如何在GWT中预取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过以下代码:

  RootPanel root = RootPanel.get(root); 
root.clear();
final FlowPanel p = new FlowPanel();
root.add(p);
for(int i = 0; i <20; ++ i){
String url =/ thumb /+ i;
final Image img = new Image(url);
img.addLoadHandler(new LoadHandler(){
@Override
public void onLoad(LoadEvent event){
p.add(img);
}
});
Image.prefetch(url);

但它不适用于我。我错过了什么?

解决方案

只有在图像附加到DOM时才调用图像加载处理程序。因此,您必须在loadHandler外部将图像添加到DOM:

  p.add(img); 
img.addLoadHandler(new LoadHandler(){
@Override
public void onLoad(LoadEvent event){
//做一些东西,图像被加载
}
}


I tried the following code:

RootPanel root = RootPanel.get("root");
root.clear();
final FlowPanel p = new FlowPanel();
root.add(p);
for (int i=0; i<20; ++i) {
    String url = "/thumb/"+i;
    final Image img = new Image(url);
    img.addLoadHandler(new LoadHandler() {
        @Override
        public void onLoad(LoadEvent event) {
        p.add(img);
    }
});
Image.prefetch(url);

But it does not work for me. Did I missed something?

解决方案

Image load handler is called only in the case, when image is attached to the DOM. So you have to add image to the DOM outside the loadHandler:

p.add(img);
img.addLoadHandler(new LoadHandler() {
    @Override
    public void onLoad(LoadEvent event) {
        //do some stuff, image is loaded
    }
}

这篇关于如何在GWT中预取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆