Aspect ratio of image in xamarin form - xamarin.forms

I am trying to get an image on the bottom right corner. I can get it easily but the problem now is to change the size of the image. I want to preserve the ratio of image while setting either height or width.
<StackLayout Grid.Column="0">
<Image Source="party.png"
HeightRequest="100"
VerticalOptions="EndAndExpand"
Aspect="AspectFit"/>
</StackLayout>
With the xaml above, I set height and width is being changed accordingly. But, when I add HorizontalOptions="EndAndExpand" in the image i lost the aspect ratio.
How can I put image at bottom right with preserving the aspect ratio?

You can use HorizontalOptions="End" , then image will at bottom right with preserving the aspect ratio .
<StackLayout Grid.Column="0" BackgroundColor="LightBlue">
<Image Source="Saimiri_sciureus.jpg"
HeightRequest="100"
VerticalOptions="EndAndExpand"
HorizontalOptions="End"
Aspect="AspectFit" />
</StackLayout>
The effect :

You can try:
<StackLayout Grid.Column="0" BackgroundColor="LightBlue">
<Image Source="party.png"
HeightRequest="*"
VerticalOptions="EndAndExpand"
HorizontalOptions="EndAndExpand"
Aspect="AspectFit" />
And that will keep the aspect ratio.

Related

adjusting size of each image in carousel view

I am using observable collection and displaying my images in carousel view. I need to adjust them by size..for example the first the biggest and then the images should go smaller. Is there a way to achieve this?
<IndicatorView x:Name="indicatorView" IndicatorSize="14"
MaximumVisible="6" IndicatorsShape="Circle"
Margin="0,20,0,30"
IndicatorColor="LightGray"
SelectedIndicatorColor="DarkGray"
HorizontalOptions="Center">
</IndicatorView>
<!--Pictures-->
<CarouselView Margin="0,0,0,20" HorizontalScrollBarVisibility="Never" x:Name="image"
ItemsSource="{Binding ImageResults, Mode=TwoWay}"
BackgroundColor="{DynamicResource PictureBackground}"
IndicatorView="indicatorView" >
<CarouselView.ItemTemplate >
<DataTemplate>
<Image Source="{Binding FieldVisualData}" WidthRequest="530" HeightRequest="334" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
I find that Image uses the WidthRequest and HeightRequest.
There is a easy way to make the size of the images in CarouselView consistent, you could set AspectFit for each Image to show the same effects.
For example:
<Image Source="{Binding FieldVisualData}"
WidthRequest="530"
HeightRequest="334"
Aspect="AspectFit"/>

how to set the size of a absolutelayout?

I want to make a absolutelayout in the corner of the screen,and set the size of it:widhtrequest=60 and heightrequest =60
inside the absolute is a frame and label.
but it seems the absolutelayout is autosized according the Image size inside it.
How can I size it?
You can refer to the following code:
<AbsoluteLayout x:Name="absLayout">
<AbsoluteLayout x:Name="box" BackgroundColor="Blue" WidthRequest="60" HeightRequest="60" >
<Image BackgroundColor="Olive" AbsoluteLayout.LayoutBounds="0,0, 50, 50" Source="test.jpg"
AbsoluteLayout.LayoutFlags="PositionProportional" />
</AbsoluteLayout>
</AbsoluteLayout>

Xamarin.Forms - How to position an element on top of another element?

I have an image slider and I want to position a search box on top of the image slider.
Here is my code:
<StackLayout HeightRequest="200" >
<cv:CarouselView Margin="0" ItemsSource="{Binding Zoos}" x:Name="CarouselZoos">
<cv:CarouselView.ItemTemplate>
<DataTemplate>
<Image Grid.RowSpan="1" Grid.Row="0" Aspect="AspectFill" Source="{Binding ImageUrl}"/>
</DataTemplate>
</cv:CarouselView.ItemTemplate>
</cv:CarouselView>
</StackLayout>
<StackLayout>
<SearchBar FontAttributes="Bold"
FontFamily="Raleway.ttf#Raleway"
FontSize="11"
Placeholder="SEARCH FOR MEDICINE AND PRODUCTS"
HorizontalOptions="Center" VerticalOptions="CenterAndExpand"
Focused="SearchBar_Focused"/>
</StackLayout>
The easiest way to do this is to wrap your element into a grid and placing the second one into the same grid.
The grid layout will put all elements inside the same cell over each other with the last one being added laying topmost.
<Grid>
<Image ... />
<Label VerticalOptions="Center" HorizontalOptions="Center" />
</Grid>
This example would put the label in the mid over the image.
When using interactive elements, keep in mind that they have to be the last ones added in order to be clickable.
So for instance, if you had
<Grid>
<Button ... />
<Image ... />
</Grid>
the button would end up being overlayed by the image and therefore not be clickable.

Xamarin.Forms.FlexLayout - how do left justify last row

I'm using the FlexLayout with images. My last row contains an odd number of images. How can I left justify the last row images? Currently they appear centered.
<FlexLayout x:Name="ThumbnailLayout"
AlignItems="Start"
JustifyContent="SpaceEvenly"
Wrap="Wrap">
</FlexLayout>
See attachment..
thx]1
It seems like you should be able to do that, however I ran into the same issue. I solved my situation by setting the FlexLayout.Basis attribute on the ImageButton inside of the FlexLayout, setting JustifyContent="Start" and giving it a HeightRequest.
<FlexLayout x:Name="photos" JustifyContent="Start" Wrap="Wrap">
<BindableLayout.ItemTemplate>
<DataTemplate>
<ImageButton
FlexLayout.Basis="33.33%"
x:Name="imageCell"
Aspect="AspectFill"
Clicked="ImageClicked"
Source="{Binding ThumbnailSource}"
>
<ImageButton.HeightRequest>
<OnIdiom x:TypeArguments="x:Double" Phone="110" Tablet="180"/>
</ImageButton.HeightRequest>
</ImageButton>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>

How to add any number in notification icon in Xamarin.Forms

I want to create a notification icon and showing all unread message as count, How to achieve this. This tool is best for this.
I'm sharing the one I build.
It's picture with a badge on top of it with Xamaring controls only.
Just fine tune the Height and Width of the "badge" on the Frame and the content on the Label.
Position the X-Axis of the badge with the ConstraintExpression: "Constant=14"
<RelativeLayout>
<Image Source="picture.png" WidthRequest="28" HeightRequest="28" />
<Frame BackgroundColor="#FF0000" OutlineColor="White"
RelativeLayout.HeightConstraint="22"
RelativeLayout.WidthConstraint="22"
CornerRadius="11"
HorizontalOptions="Center"
Padding="0"
IsClippedToBounds="True"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0, Constant=14}"
RelativeLayout.YConstraint="-6">
<StackLayout WidthRequest="22">
<Label Padding="2" Text="+5" RelativeLayout.WidthConstraint="22" TextColor="White" FontSize="12" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
</StackLayout>
</Frame>
</RelativeLayout>
Picture of the above code on my app (the "+5" badge):
First, you need to have a BadgeView, to represent the notification counter.
In order to achieve this, you can use the BadgeView NuGet Package
After adding to your solution, you can add in XAML a badge indicator:
<badge:BadgeView Text="{Binding BadgeNumber}" BadgeColor="{Binding BadgeColor}" VerticalOptions="Center" HorizontalOptions="End" />
The next step, is putting all together, so you need to have a Image (your bell), and pack the image with the Badge View. Since you want to overlay both elements, you can use a Grid.
<Grid>
<Image Source="bell.png" HorizontalOptions="Center" VerticalOptions="Center"
<badge:BadgeView Text="{Binding BadgeNumber}" BadgeColor="{Binding BadgeColor}" VerticalOptions="End" HorizontalOptions="Start" />
</Grid>
Here you are setting the image, to be in the center, and the badge to be in the Top Left Corner. After this, you should manipulate the Badge Position with Margins, in order to put the badge in the correct position that you want.

Resources