Fast Images on Android

0
102

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

public partial class ImagePage : ContentPage
{
    public ImagePage()
    {
        InitializeComponent();
        var uri = new Uri(“https://avatars3.githubusercontent.com/u/7909888?s=460&v=4”);
        for (var i = 0; i < 100; i++)
        {
            MainGrid.RowDefinitions.Add(new RowDefinition { Height = 100 });
            for (var j = 0; j < 4; j++)
            {
                var image = new Image
                {
                    Source = ImageSource.FromUri(uri)
                };
                Grid.SetRow(image, i);
                Grid.SetColumn(image, j);
                MainGrid.Children.Add(image);
            }
        }
    }
}
And with the following XAML UI ImagePage.xaml
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>
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 code represents just a collection of the same images as 400 plus images. And this will result in an ugly lag when scrolling up and down. 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 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
Glide has a nice, fluent API for loading bitmaps in Java:
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:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;
        base.OnCreate(savedInstanceState);
        Xamarin.Forms.Forms.Init(this, savedInstanceState);
        // Add this line, that’s it!
        Android.Glide.Forms.Init();
        LoadApplication(new App());
    }
}
And behold! smooth scrolling!
References : Xamarin Blogs

Comments

comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here