A type of ContentView for Xamarin.Forms that resizes itself based on the provided aspect ratio.
Sometimes, when dealing with video views, I got extremely frustrated, because there was no practical way to keep the video view exactly in the size ratio of the video itself without letterboxing or stretching.
I could set the WidthRequest
and HeightRequest
properties according to the size ratio of the video, but then I wouldn't have the option to stretch the video across the entire screen width or height using the HorizontalOptions
or VerticalOptions
.
This is how the AspectView was conceived. It's a container for other ContentViews and always keeps the size ratio that you have set, which means you can box your video view, images or anything else inside of it, provide a size ratio and avoid letterboxing and stretching.
This looks suspiciously similar like letterboxing, however it's not. In the case of letterboxing, the background would be black while we can actually see the Page background here, which means that we could add other controls and maintain the video's aspect without letterboxing.
Simply install the package from nuget.org and then use it as follows:
<Grid>
<forms:AspectView
AspectRatio="0.5625"
HorizontalOptions="Fill"
VerticalOptions="Center">
<videoPlayer:VideoPlayer
Source="myvideo.mp4" />
</forms:AspectView>
</Grid>
If you want to use a 16:9 ratio for videos, you would use 0.5625, like in the example.
The AspectView control uses the HorizontalOptions
and VerticalOptions
in combination with the AspectRatio
property and only works when either the HorizontalOptions
or the VerticalOptions
property is set to Fill
, but not both at the same time.
Note: The AspectRatio
must have a value greater than 0.0
and by default is set to 1.0
, which means square.