If you missed my last post about Xamarin.Forms performance on Android, make sure to check that out here
From my experience making image-heavy Xamarin.Forms apps in the past, Android was a huge pain-point. Android.Graphics.Bitmap is somewhat of a troublemaker, and to make matters worse, Xamarin.Forms API design makes it tough to handle them properly. The Xamarin.Forms team will improve this as time goes on, I'm sure, but it would require breaking public API changes--so it's not easy.
Google has some complex documentation on handling Android.Graphics.Bitmap here. At the top, they are recommending to use a Java library named Glide to simplify working with images in your Android apps.
Looking at Glide's documentation, look at how beautifully you can load image, crop it as a circle, and include a loading indicator during the download:
Glide also covers loading the different types of images that Xamarin.Forms supports, such as:
First I wrote a quick binding project for Glide, where I only bound the important Glide APIs. There were quite a bit of fixups needed to bind every possible public API of Glide, but I didn't care to do that. I decided to make glidex its own self-contained NuGet, because it seemed like it would also be useful for "classic" Xamarin.Android apps.
For the Xamarin.Forms implementation, I knew I would have to just throw IImageSourceHandler out the window and not use it. I also knew I would have to make two of my own custom renderers for Image and ImageCell. I started with the source for the "fast" image renderer from Xamarin.Forms as a starting point. It didn't take too much code to get things working, and it seemed to work rather well!
For comparison, here is a table of peak memory used via the different sample pages I've written:
|Page||Loaded by||Peak Memory Usage|
NOTE: I believe these numbers are in bytes. I restarted the app (release mode) before recording the numbers for each page. Pages with ListViews I scrolled up and down a few times.
To be fair, I did manufacture these samples to explicitly show problems with images in Xamarin.Forms on Android. The goal was to show how using Glide could completely mitigate the issues.
I just released the 1.0.0 version of glidex.forms on NuGet, take it for a spin!
All you need in your app is the glidex.forms NuGet package, and add the following one-line to your existing apps:
As always, file issues and send me PRs on Github!