Gwt图像原始尺寸

问题描述 投票:2回答:3

我使用Image对象通过调用setPixelSize()方法来调整图像大小,以将png图像加载为缩略图。我还需要在某些时候将图像的原始大小检索为整数。如何获得图像的原始尺寸(宽度,高度)?

确定,我找到了一种解决方法:我使用了一个虚拟容器(SimplePanel),并且在不缩放的情况下加载图像,以保存其实际尺寸,然后从父容器中删除该容器并丢弃新的Image对象。我不知道这是否是一个好的解决方法,但是它可以工作。虽然我想知道是否还有另一种方法...

解决方法的问题:我有一个下拉列表,可以从中选择逻辑文件夹(其中包含图像)。当我选择一个新文件夹,并且新的图像集被加载到显示器上时,则宽度为0,高度为0。

private void getTrueSize(String fullUrl) {
        Image trueImage = new Image();
        this.tstCon.add(trueImage);
        trueImage.setUrl(fullUrl);
        this.trueHeight = trueImage.getHeight();
        this.trueWidth = trueImage.getWidth();
        //this.tstCon.remove(trueImage);
        //trueImage = null;
        GWT.log("Image [" + this.imgTitle + "] -> height=" + this.trueHeight + " -> width=" + this.trueWidth);//
    }
gwt gwt2
3个回答
1
投票
public class Thumb extends Image { public Thumb(){ super(); } protected void onAttach(){ Window.alert(getOffsetHeight()+" "+getOffsetWidth()); //this should give you the original value setPixelSize(10,10); // this should be the visible value super.onAttach(); } }

如果这不起作用,请尝试实现onLoad()而不是onAttach(),因为onLoad()将在将图像添加到DOM后被调用,以便它一定可以工作。


0
投票

0
投票
public Panel() { image = new Image(); image.addLoadHandler(this::onLoad); } private void onLoad(@SuppressWarnings("unused") LoadEvent event) { origImageWidth = getNaturalWidth(image); origImageHeight = getNaturalHeight(image); } private static int getNaturalHeight(Image img) { return img.getElement().getPropertyInt("naturalHeight"); //$NON-NLS-1$ } private static int getNaturalWidth(Image img) { return img.getElement().getPropertyInt("naturalWidth"); //$NON-NLS-1$ }
© www.soinside.com 2019 - 2024. All rights reserved.