You are currently viewing How to Display Animated GIF in a Xamarin based Android Application?
Xamarin

How to Display Animated GIF in a Xamarin based Android Application?

5
(2)

How to Display Animated GIF in a Xamarin based Android Application?

Related: Display Animated GIF in a Xamarin based Android Application

Android doesn’t provide any support for GIF animation in the ImageView. This post gives you complete information about displaying GIF in a Xamarin based Android application.

Note: This example is based on Xamarin based Android Application using C# language. If you are looking for a native java based solution, I like to suggest StackOverflow post( https://stackoverflow.com/questions/6533942/adding-gif-image-in-an-imageview-in-android)

Please follow the following steps to display animated GIF in an Android application.

Step 1: Install “Refractored.GifImageView” Nuget package into your Xamarin based Android project.

Link: https://www.nuget.org/packages/Refractored.GifImageView

Step 2: Add the following code into your Main Android Layout file (XML) or in the layout where you want to display GIF image.

<com.felipecsl.gifimageview.library.GifImageView
    android:id="@+id/myGIFImage"
    android:layout_gravity="center"
    android:scaleType="fitCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Step 3: Add GIF file to your Drawable folder ( here loading.gif)

Step 4: Update your Activity class.

Add using statement:

using Felipecsl.GifImageViewLibrary;

Find the GIFImageView and load animation

GifImageView myGIFImage;   
protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    myGIFImage= FindViewById<GifImageView>(Resource.Id.myGIFImage);
    // From Drawable
                Stream input = Resources.OpenRawResource(Resource.Drawable.loading);
                
                // You should convert the "input" into Byte Array 
                byte[] bytes = ConvertByteArray(input);
                
                myGIFImage.SetBytes(bytes);
                myGIFImage.StartAnimation();

}
private byte[] ConvertByteArray(Stream input)
                {
                    byte[] buffer = new byte[16 * 1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                            ms.Write(buffer, 0, read);
                        return ms.ToArray();
                    }
                }

You can stop animation using the following code

myGIFImage.StopAnimation();

and start again using 

 myGIFImage.StartAnimation();

That all done. You can also find more details about the same here (https://github.com/Jignesh-Darji/GifImageView-Xamarin.Android)

Read more Xamarin related posts: https://jigneshdarji.com/category/xamarin/

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?