Image performance! Fast loading images! in your app is always a must task to give a better user experience. But in Android apps it has been a tedious task to achieve. Google also has documentation on ‘Managing Bitmap Memory’ which helps to achieve this task but its relatively very complex.
The documentation outlines :
- Cache and recycle bitmaps in memory
- Java classes named LruCache and MutableSet<SoftReference<Bitmap>>?
This is not a problem that I think the developer should not concern as they want to build the app.
Let’s say that a Xamarin. Forms have the page which has lots of images to load- ImagePage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version=“1.0” encoding=“utf-8” ?>
<ContentPage xmlns=“http://xamarin.com/schemas/2014/forms”
xmlns:x=“http://schemas.microsoft.com/winfx/2009/xaml”
x:Class=“HelloGlide.ImagePage”>
<ScrollView Padding=“0”>
<Grid x:Name=“MainGrid” ColumnSpacing=“0” RowSpacing=“0”>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ScrollView>
</ContentPage>
|
Google’s response to this complex problem is to use a Java library named Glide:
1
2
3
4
5
6
|
Note: For most cases, we recommend that you use the Glide library to
fetch, decode, and display bitmaps in your app. Glide abstracts out
most of the complexity in handling these and other tasks related to
working with bitmaps and other images on Android. For information
about using and downloading Glide, visit the Glide repository on
GitHub: https://github.com/bumptech/glide
|
2
3
4
5
6
|
GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
|
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: