简体   繁体   English

如何在Vaadin中设置图像组件的大小并保持纵横比

[英]How to set the size of an Image component in Vaadin and keep the aspect ratio

I have an Image component which I'd like to define the size of 500px by 500px. 我有一个Image组件,我想定义500px到500px的大小。 The problem I have is that my image isn't a square but I'd like to keep the aspect ratio when pushing in the StreamResource to my Image component. 我的问题是我的图像不是正方形,但我想在将StreamResource推送到我的图像组件时保持纵横比。 How can I do this other than resizing the image manually? 除了手动调整图像大小外,我该怎么做? In other words is there something within the Image component that allows me to automatically re-size the image without forcing it to become a square? 换句话说,Image组件中是否有某些东西允许我自动重新调整图像大小而不强制它成为正方形?

The solution I ended up with was to resize the image before sending it to the UI. 我最终得到的解决方案是在将图像发送到UI之前调整图像大小。 In other words I do some calculations where if the image is larger then 500, I then look to see if it's the height or width, and then use whichever is the largest as the baseline. 换句话说,我做了一些计算,如果图像大于500,我会查看它是高度还是宽度,然后使用最大的基线作为基线。 I then multiple the aspect ratio to the other dimension and convert the image accordingly. 然后我将纵横比复用到另一个维度并相应地转换图像。

So for example if I have an image that's 800x600 I will then assume use 800 as the baseline, meaning that 500/800 meaning that the image has to be reduced to 62.5% of it's size. 因此,例如,如果我的图像是800x600,那么我将假设使用800作为基线,这意味着500/800意味着图像必须减少到其尺寸的62.5%。 I then reduce 600 by 62.5%, as in 600 * 0.625 = 375. Therefore I resize the image using my image library to 500x375. 然后我将600减少62.5%,如600 * 0.625 = 375.因此,我使用我的图像库将图像调整为500x375。 And if the image was 600x800 I would then resize it to 375x500. 如果图像是600x800,我会将其调整为375x500。 In other words I pre-process the image and rely on anything within the GUI to manage this for me. 换句话说,我预处理图像并依赖GUI中的任何内容来为我管理。

Using css you can enforce a max width and height to achieve your desired look. 使用css,您可以强制执行最大宽度和高度,以实现所需的外观。

First edit your mytheme.scss file and add a css entry. 首先编辑mytheme.scss文件并添加一个css条目。 Should look something like this 应该看起来像这样

@mixin mytheme {
  @include valo;

  .maxSize500 {
    max-width: 500px;
    max-height: 500px;
  }

}

To apply this rule via code use addStyleName and set the size to undefined. 要通过代码应用此规则,请使用addStyleName并将大小设置为undefined。 Ex: 例如:

myImageComponent.setSizeUndefined(); // this line may not be needed in your project (it wasn't in mine)
myImageComponent.addStyleName("maxSize500");

Now build, re-run your project, and you should be g2g. 现在构建,重新运行你的项目,你应该是g2g。

hths! HTHS!

I've run into this same issue. 我遇到了同样的问题。 I've had to build my own logic to resize the height and width of the image(s) based on the pixel width of browser. 我必须建立自己的逻辑,根据浏览器的像素宽度调整图像的高度和宽度。 I do not believe there is anything out of the box in Vaadin that can handle this. 我不相信Vaadin有任何开箱即用的东西可以解决这个问题。

Something like this: 像这样的东西:

    if (browserWidth <= 1250){
        height = x;
        width = x;
    }else if (browserWidth <= 1500){
        height = x;
        width = x;
    }else if (browserWidth <= 1750){
        height = x;
        width = x;
    }else if (browserWidth <= 2000){
        height = x;
        width = x;
    }else {
        height = x;
        width = x;
    }

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM