动态css背景图像在gwt

时间:2014-06-24 21:13:48

标签: css image gwt gwt-bootstrap

我有一个包含一些优惠的页面,我想为每个优惠显示一张照片作为缩略图。缩略图图像具有固定的宽度和高度,因此我需要调整大小并裁剪原始图像。我想使用background-size: cover

我的问题是我正在使用GWT-Bootstrap,我需要拥有动态图像源。示例代码:

<b:Thumbnail size="3">
    <b:Image ui:field="image" />
    <b:Paragraph ui:field="text" />
    <b:Button ui:field="button" />
</b:Thumbnail>
每个优惠的

@UiField Image image;

private void renderOffer(final Offer offer) {
    String photo = offer.getPHOTO();
        image.setUrl("images?img=" + photo);
    }
            ...
}

servlet返回图片:

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String img = request.getParameter("img");
    ServletContext sc = getServletContext();

    String mimeType = sc.getMimeType(img);
    response.setContentType(mimeType);

    Properties parameters = ParametersLoader.getParameters();
    File file = new File(parameters.getProperty("imageUrlPath") + img);
    response.setContentLength((int)file.length());

    FileInputStream in = new FileInputStream(file);
    OutputStream out = response.getOutputStream();

    byte[] buf = new byte[1024];
    int count = 0;
    while ((count = in.read(buf)) >= 0) {
        out.write(buf, 0, count);
    }
    in.close();
    out.close();
}

如何将<b:Image ui:field="image" />替换为div css,如:

.myDiv {
  background-image:  url(somehow received dynamic url?);
  background-size:   cover;  
  background-repeat: no-repeat;
}

1 个答案:

答案 0 :(得分:1)

您可以使用Label代替Image

<b:Label ui:field="image" styleName="myDiv" />

在你的CSS中:

.myDiv {
  background-size:   cover;  
  background-repeat: no-repeat;
}

然后在你的代码中:

image.getElement().getStyle().setBackgroundImage("images?img=" + photo);