Create Drawable Animation In Xamarin With Visual Studio 2015


In my previous article on “Animated Splash screen”, I explained how three types of animation can be created in Android and gave an example of View Animation. In this article, I will discuss how we can do Drawable animation in Xamarin Android using Visual Studio 2015.

Following are the steps for how to create Drawable Animation in Xamarin.

Step 1 Create new Project for Android Application


I have selected “Blank App(Android)” template for this article.

Step 2 Adding Images in Project

In this article, I have taken 9 images. Each image contains a character of string “Xamarin”. I will display characters one by one in ImageView. We need to add images under Resource->drawable folder as shown in below image.


Step 3 Create XML file for Drawable Animation

We need to create “Anim” folder under Resource folder and then add new XML file under “Anim” folder. Here, I have created an “animate_xamarin.xml” file for drawable animation. In this file, we need to add images in <animation-list> tag. Here I have used android:oneshot attribute set to false. If we give value as true to said attribute then it will run only one time. I want to start animation when the user clicks on ImageView and if animation is running then animation will stop. I have take ImageView in Main.xml. Code snippet of Main.axml and animate_xamarin.xml is as follows:


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.     <ImageView  
  7.         android:id="@+id/animate_imageview"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@anim/animate_xamarin"  
  11.         android:background="@android:color/white" />  
  12. </LinearLayout>   


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="" android:oneshot="false">  
  3.   <item android:drawable="@drawable/xamarin_0"  
  4.         android:duration="200"  />  
  5.   <item android:drawable="@drawable/xamarin_1"  
  6.         android:duration="200"  />  
  7.   <item android:drawable="@drawable/xamarin_2"  
  8.         android:duration="200"  />  
  9.   <item android:drawable="@drawable/xamarin_3"  
  10.         android:duration="200"  />  
  11.   <item android:drawable="@drawable/xamarin_4"  
  12.         android:duration="200" />  
  13.   <item android:drawable="@drawable/xamarin_5"  
  14.         android:duration="200" />  
  15.   <item android:drawable="@drawable/xamarin_6"  
  16.         android:duration="200" />  
  17.   <item android:drawable="@drawable/xamarin_7"  
  18.         android:duration="200" />  
  19.   <item android:drawable="@drawable/xamarin_8"  
  20.         android:duration="200"  />  
  21. </animation-list>  

In the above, “animate_xamarin.xml” consists of  android:duration tag to set animation duration.

Step 4 Apply Animation in MainActivity.cs

The AnimationDrawable class is the basis for loading drawable resources one after another to create an animation. The drawable resources will be loaded one after another, according to the instructions in the resource file Resource/Anim/animate_xamarin.xml.

This animations are applied to ImageView in MainActivity class using AnimationDrawable base class. When user clicks on ImageView, animation will start and when user again clicks on ImageView, the animation will stop using Start() and Stop() method. Code snippet of MainActivity.cs as shown below:


  1. public class MainActivity : Activity  
  2. {  
  3.         ImageView imageView;  
  4.         AnimationDrawable animation;  
  5.         protected override void OnCreate(Bundle bundle)  
  6.         {  
  7.             base.OnCreate(bundle);  
  8.             // Set our view from the "main" layout resource  
  9.             SetContentView (Resource.Layout.Main);  
  10.             imageView = FindViewById<ImageView>(Resource.Id.animate_imageview);  
  11.             animation = (AnimationDrawable)imageView.Drawable;  
  12.             imageView.Clickable = true;  
  13.             imageView.Click+= ImageView_Click;  
  14.         }  
  16.         private void ImageView_Click(object sender, System.EventArgs e)  
  17.         {  
  18.             if(!animation.IsRunning)  
  19.             {  
  20.                 animation.Start();  
  21.             }  
  22.             else  
  23.             {  
  24.                 animation.Stop();  
  25.             }  
  26.         }  
  27. }  




In this article, we learned how to create Frame or Drawable Animation in Xamarin with Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all