In Focus

Xamarin.Forms - Faster Image Loading On Android Using GlideX

In this article, you will learn how to make the image loading process faster on Android using GlideX in Xamarin.Forms.

Introduction

 
Xamarin.Forms - Fast Image Loading On Android Using GlideX
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
GlideX NuGet is used to fasten the image loading in Android.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
 
Now, you need to click "Create a new project".
 
Xamarin.Forms - Fast Image Loading On Android Using GlideX
 
Now, filter by Project Type: Mobile.
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
NuGet Packages
 
Now, add the following NuGet packages.
  • glidex.forms

Add glidex.forms NuGet

 
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search for "glidex.forms" and add the resultant package. Remember to install it for each project (.NET Standard, Android, iOS).
 
Note
Before you initialize Xamarin.Forms in your MainActivity.cs, you must initialize the FormsMaterial.Init().
  1. FormsMaterial.Init();  

Setting up the User Interface

 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:local="clr-namespace:XamarinGlidex"    
  5.              x:Class="XamarinGlidex.MainPage">    
  6.     
  7.     <ScrollView Padding="0">    
  8.         <Grid x:Name="MainGrid" ColumnSpacing="0" RowSpacing="0">    
  9.             <Grid.ColumnDefinitions>    
  10.                 <ColumnDefinition />    
  11.                 <ColumnDefinition />    
  12.                 <ColumnDefinition />    
  13.                 <ColumnDefinition />    
  14.             </Grid.ColumnDefinitions>    
  15.         </Grid>    
  16.         </ScrollView>    
  17. </ContentPage>    

Binding Image

 
MainPage.xaml.cs
  1. using Xamarin.Forms;    
  2.     
  3. namespace XamarinGlidex    
  4. {    
  5.     public partial class MainPage : ContentPage    
  6.     {    
  7.         public MainPage()    
  8.         {    
  9.             InitializeComponent();    
  10.             var uri = new Uri("https://thumbor.forbes.com/thumbor/960x0/https%3A%2F%2Fblogs-images.forbes.com%2Fadrianbridgwater%2Ffiles%2F2016%2F02%2F1monkey.jpg");    
  11.             for (var i = 0; i < 100; i++)    
  12.             {    
  13.                 MainGrid.RowDefinitions.Add(new RowDefinition { Height = 100 });    
  14.     
  15.                 for (var j = 0; j < 4; j++)    
  16.                 {    
  17.                     var image = new Image    
  18.                     {    
  19.                         Source = ImageSource.FromUri(uri)    
  20.                     };    
  21.                     Grid.SetRow(image, i);    
  22.                     Grid.SetColumn(image, j);    
  23.                     MainGrid.Children.Add(image);    
  24.                 }    
  25.             }    
  26.         }    
  27.     }    
  28. }     
Click the "Play" button to try it out.
 
Xamarin.Forms - Fast Image Loading On Android Using GlideX
 
I hope you have understood how to make the loaing of images in Android faster using GlideX in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback.
 
Happy Coding :)