Creating A Frame Animation In Xamarin Android Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Frame Animation is called for animating the images in apps.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, given below, need to be followed in order to create the frame animation in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. Or click (Ctrl+Shift+N). The project needs to be clicked after opening all the types of projects in Visual Studio. 



Step 2

Select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android). Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Visual Studio

Step 3

Next, go to the Solution Explorer and select Resource-->Layout-->double click to open main.axml page. 

Visual Studio

Step 4

After opening the main.axml file, open the main page designer.  If you want to write the code, use Source panel, otherwise open Designer section.

Visual Studio

Next, go to the Source panel and delete the code of default "hello world" button, followed by deleting the C# button from MainActivity.cs page.

Step 5

Now, go to the toolbox window and scroll down. You will see all the tools and controls. You need to drag and drop the ImageView.

Visual Studio

Step 6

Now, go to the properties window. You need to edit the ImageView Id Value and src Value (EX: android:id="@+id/animated_android" android:src="@anim/animate_android" ).



Step 7

In this step, go to the Main.axml page Source Panel. Note down the ImageView Id value and source value.

Visual Studio

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">  
  2.     <ImageView android:id="@+id/animated_android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@anim/animate_android" />  
  3. </LinearLayout>  
Step 8

In this step, add the Images form your local system. Go to Solution Explorer-->Resource-->Drawable-->Right Click-->Add-->Exsisting Item (Shift+Alt+A).

Visual Studio

Step 9

Now, you can choose the needed images. Then, click Add.

Visual Studio

Step 10

In this step, add one folder named Anim.

Go to Solution Explorer-->Resource-->Right click-->Add-->New Folder.

Visual Studio

Step 11

In this step, add one file called animate_android.xml.

Go to Solution Explorer-->Resource-->Anim-->Right Click-->Add-->New Item (Ctrl+shift+A).

Visual Studio

Step 12

Now, select the XML file and give it a name as animate_android.xml and click Add.

Visual Studio

Step 13

After creating the animate_android.xml file, write the following code.

animate_android.xml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">  
  3.     <item android:drawable="@drawable/im1" android:duration="100" />  
  4.     <item android:drawable="@drawable/im2" android:duration="100" />  
  5.     <item android:drawable="@drawable/im3" android:duration="100" />  
  6.     <item android:drawable="@drawable/im4" android:duration="100" />  
  7.     <item android:drawable="@drawable/im5" android:duration="100" />  
  8.     <item android:drawable="@drawable/im6" android:duration="100" />  
  9.     <item android:drawable="@drawable/im7" android:duration="100" />  
  10. </animation-list>  
Visual Studio

Step 14

Next, go to the MainActivity.cs page and write the following two namespaces.

using Android.Graphics.Drawables;
using Android.Views.Animations;


Visual Studio

Step 15

Now, go to the MainActivity.cs page. Create one method OnWindowFocusChanged() and write animation code.

MainActivity.cs
  1. public override void OnWindowFocusChanged(bool hasFocus) {  
  2.     if (hasFocus) {  
  3.         ImageView imageView = FindViewById < ImageView > (Resource.Id.animated_android);  
  4.         AnimationDrawable animation = (AnimationDrawable) imageView.Drawable;  
  5.         animation.Start();  
  6.     }  
  7. }  
Visual Studio

Step 16

If you have Android Virtual device, run the app on it. Otherwise, connect your Android phone and run the app in that.

Simply connect your phone and go to Visual Studio. The connected phone will show up in the Run menu

(Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Visual Studio

Output

After a few seconds, the app will start running on your phone.

You will see that the Frame Animation is working successfully.

Visual Studio

Visual Studio

Visual Studio

Summary

So, this was the process of creating the Frame Animation in Xamarin Android app, using Visual Studio 2015.

 

Next Recommended Readings