Xamarin Forms Image fill width with proper aspect

后端 未结 11 2184
我寻月下人不归
我寻月下人不归 2020-12-09 08:05

Here is xaml (an image in stacklayout). Everything is logical: I set Aspect to AspectFit and HorizontalOptions to FillAndExpand. Width of image must fill the width of stackl

11条回答
  •  借酒劲吻你
    2020-12-09 08:54

    I had a similar problem. I wanted to fill the width and the height of a StackLayout when possible but without cropping the actual image. The answers here have helped me find the correct way. Here is what has worked for me:

                
                    
                
    

    Maybe it will be of use for someone.

    Clarification:

    HorizontalOptions and VerticalOptions are with the value FillAndExpand, so the height and the width of the Grid are adjusted to fill the parent, in this case that is the Stacklayout. The image is a child of the Grid, so it transforms according to the parent (we haven't given any additional options to the image).

    The aspect of the image is set to AspectFit, so that the image will fit the view (in this case the Grid) and also preserve its ratios.

    In this way, if the image is in portrait mode but too narrow, it will fill the Grid's (StackLayout's) height but won't fill the width in order to preserve its ratios and to not be cropped from above or under.

    If the image is in landscape mode, it will fill the Grid's (StackLayout's) width but won't fill the height in order to preserve its ratios and to not be cropped from left or right.

提交回复
热议问题