GlideX For Fast Images on Android

Avatar

Jonathan

Getting good image performance on Android has traditionally been a difficult task. Google has some documentation on the subject, which unfortunately mentions some complex topics:

  • Cache and recycle bitmaps in memory
  • Java classes named LruCache and MutableSet<SoftReference<Bitmap>>?
  • Specific cases for older Android versions

This is not the type of problem developers want to be focusing on–they just want to build their app!

To explain the problem further, let’s imagine a Xamarin.Forms page with lots of images (MainPage.xaml.cs):

With the following MainPage.xaml:

Note, you would not really want to implement a screen like this in a Xamarin.Forms application. ListView or CollectionView are much better options!

This example code presents 400 images of a strange man in a ScrollView (non-virtualized):

Especially in an emulator, the performance while scrolling this ScrollView is not the best. The Xamarin.Forms team has done a great job improving the performance of a page like this in recent releases–but is there a way to do better?

Glide: an image library for Android focused on smooth scrolling

Google’s response to this complexity problem is to use a Java library named Glide:

Glide has a nice, fluent API for loading bitmaps in Java:

Glide handles a lot of details for you:

  • Downloads images from URLs
  • Handles device rotation
  • Handles in-memory and on-disk caching
  • Can crop, place a loading indicator, etc.

So if Glide is written in Java, how do we use it in Xamarin.Forms?

To unlock the power of Glide, I created “GlideX”: a simple library to swap-in Glide’s image loading implementation in-place of what Xamarin.Forms ships in the box. This began as an experiment, but perhaps one day could be the default for Xamarin.Forms?

How do I use GlideX in my Xamarin.Forms app?

This part is pretty easy:

  • Add the glidex.forms NuGet package to the Android project in your solution.
  • Add one line of code to initialize GlideX.

In your MainActivity.cs:

And behold! smooth scrolling!

For a complete example, see my HelloGlide sample on Github.

Future plans

Try out GlideX today, and let us know how it works in your apps!

For a complete demo, check our episode about GlideX on the Xamarin Show:

Avatar
Jonathan Peppers

Follow Jonathan   

1 Comments
Avatar
Markus Schaber 2019-04-11 03:00:30
Hi, Jonathan, the two GIFs which should show the screenshots just show up as broken image on my side. :-(