Reputation: 185
I am creating an app in Xamarin Forms and try to imitate a FloatingActionButton
, so I created a round Button
that shall have a centered icon. Unfortunately my icon overlaps the Button
by far and I have not found any way to properly rescale it. I tried to work around by making the Button
that big so it fits to the icon and scale the whole thing down, but it loses it’s quality by that. Is there a way to only resize the image?
The Button
could simply look like this, the problem seems to be something general:
<Button x:Name="button" ImageSource="image.png" />
Upvotes: 0
Views: 200
Reputation: 3216
There are 2 scenarios.
You can force it this way using a Frame
:
<Frame CornerRadius="50"
HeightRequest="100"
WidthRequest="100"
Padding="0"
IsClippedToBounds="True">
<Image Source="image.png"
HorizontalOptions="Center"
VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="MyTappedEvent" />
<!-- or <TapGestureRecognizer Command="{Binding MyCommand}" /> -->
</Image.GestureRecognizers>
</Image>
</Frame>
Just replace HeightRequest
, WidthRequest
and CornerRadius
with your values. You have to make sure that CornerRadius
is half the size or WirthRequest
/HeightRequest
to make it looks like a circle.
Then just use the Tapped
event for basic usage or the Command
of the TapGestureRecognizer
if you use MVVM pattern.
png
image is already rounded and transparentThen you can just use the TapGestureRecognizer directly on your image so it behaves like a button:
<Image Source="image.png" HeightRequest="100" WidthRequest="100">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding AddCommand}" />
</Image.GestureRecognizers>
</Image>
Hope it helps,
Happy coding!
Upvotes: 3