I've create a layout that lists a List of objects in a RecyclerView
. I would like to add the option for the user to save a certain object, but I have no idea how to achieve something like the layout bellow. I need a button that can be toggled, and that if the user clicks it, it will fill the heart button with red, otherwise, it will be empty. Thank you.
Layout
If you want that heart to fill from nothing to full using some kind of animation, I'd suggest looking into SkiaSharp (https://blog.xamarin.com/deep-dive-skiasharp-xamarin-forms/) and then using the Animation Class (https://xamarinhelp.com/custom-animations-in-xamarin-forms/) to animate the fill height.
In a nutshell, you have to create a custom view in which you add a SKCanvasView:
In your view.xaml.cs you add a new function
Inside of this function you'll need to write some code to draw your heart. I'd suggest you draw an outline first and then the inside, which is supposed to fill up. You probably want to add a bindable property which holds the value of the amount to fill up, maybe as a percentage. Use this property to calculate the height of your inner part within your OnPaintSurface code.
Finally when you want your icon to fill up, you create an Animation, which then alters the property created earlier:
Notice that you need to invalidate the PrimaryCanvas surface. This will result in calling OnPaintSurface again, which will use your changed property and therefore increase the height of your filling while redrawing the canvas.
You can define a heart background with
vector
andpath
:Here, I have posted my demo on github, you can see a button with
selector
background which allow you change the button's background with the button's state, and another button with the SVG background.Update:
I have add animation on the button.