Introduction
In mobile applications, you will find many applications with animated splash screens. In Android, we can do animation by View Animation, Property Animation or Drawable Animation.
- View Animation
These animations refer to specific Views and can perform simple animation on the contents of the View. Because of it is simplicity, this type of animation is still useful for alpha, rotations, scale and translating animations.
- Property Animation
Property animation enables an application to animate almost anything. Property animations can be used to change any property of any object, even if that object is not visible on the screen.
- Drawable Animation
This is a special Drawable resource, which is used to apply a very simple animation effect to the layouts.
In this article, I will use View animation to design animated splash screen.
The steps are given below in regards to animated splash screen.
Step 1
Create new project for an Android Application
I have selected “Blank App(Android)” template for this article.
Step 2
Splash screen, MainActivity layout and View animation
I have used two layouts “Main.axml” and “SplashScreen.axml”. “Main.axml” layout is for MainActivity and “SplashScreen.axml” layout is for splash screen. In “SplashScreen.axml”, I have taken one ImageView and TextView. I am performing View animation on ImageView and for performing an animation, I have created XML View file “view_animation.xml” under Resource->Anim folder. The code snippet of Main.axml, SplashScreen.axml and view_animation.xml is shown below.
SplashScreen.axml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
- p1:minWidth="25px"
- p1:minHeight="25px"
- p1:layout_width="match_parent"
- p1:layout_height="match_parent"
- p1:background="@android:color/white"
- p1:id="@+id/relativeLayout1">
- <TextView
- p1:text="Welcome Readers"
- p1:layout_width="wrap_content"
- p1:layout_height="wrap_content"
- p1:textSize="45dp"
- p1:textColor="@android:color/black"
- p1:layout_alignParentBottom="true"
- p1:layout_centerHorizontal="true"
- p1:id="@+id/textView1" />
- <ImageView
- p1:layout_width="wrap_content"
- p1:layout_height="wrap_content"
- p1:id="@+id/imageView"
- p1:layout_centerVertical="true"
- p1:layout_centerHorizontal="true"
- p1:src="@drawable/maxresdefault" />
- </RelativeLayout>
Main.axml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:minWidth="25px"
- android:minHeight="25px">
- <TextView
- android:text="Main Activity Started"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/textView1" />
- </LinearLayout>
view_animation.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/linear_interpolator">
- <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="1.0"
- android:toXScale="1.4"
- android:fromYScale="1.0"
- android:toYScale="0.6"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillEnabled="true"
- android:fillAfter="false"
- android:duration="2000" />
- <set android:interpolator="@android:anim/accelerate_interpolator">
- <scale android:fromXScale="1.4"
- android:toXScale="0.0"
- android:fromYScale="0.6"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillEnabled="true"
- android:fillBefore="false"
- android:fillAfter="true"
- android:startOffset="2000"
- android:duration="400" />
-
- <rotate android:fromDegrees="0"
- android:toDegrees="-45"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillEnabled="true"
- android:fillBefore="false"
- android:fillAfter="true"
- android:startOffset="2000"
- android:duration="400" />
- </set>
-
- </set>
View_animation.xml consists of scale and rotate type animations and all the animations are performed simultaneously. The first scale animation performed stretches the image horizontally and shrinks it vertically, followed by rotating the image at 45 degrees counter-clockwise and shrinking until it disappears from the screen.
Screenshot of layouts
These animations are applied to imageview in SplashScreenDemo class, using Animation.View.Animations and then we start the animation using StartAnimation() method. The code snippet of SplashScreenDemo.cs is shown below.
SplashScreenDemo.cs
- public class SplashScreenDemo : Activity
- {
- ImageView imageView;
- Animation view_animation;
- TextView textview;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- RequestWindowFeature(Android.Views.WindowFeatures.NoTitle);
- SetContentView (Resource.Layout.SplashScreen);
- imageView = (ImageView)FindViewById(Resource.Id.imageView);
- textview = (TextView)FindViewById(Resource.Id.textView1);
- view_animation = AnimationUtils.LoadAnimation(this,Resource.Animation.view_animation);
- textview.SetTextColor(Color.Red);
- imageView.StartAnimation(view_animation);
- view_animation.AnimationEnd += Rotate_AnimationEnd;
-
- }
-
- private void Rotate_AnimationEnd(object sender, Animation.AnimationEndEventArgs e)
- {
- Finish();
- StartActivity(typeof(MainActivity));
- }
- }
The code snippet given above uses RequestWindowFeature() method to hide titlebar of SplashScreen layout. Using LoadAnimation() method, set view_animation.xml for View Animation and StartAnimation() method to start animation on ImageView. When animation ends, it will trigger Animation.AnimationEnd method, so I am terminating Splash Screen, using Finish() method and Starting MainActivity, using StartActivity() method.
MainActivity.cs
- public class MainActivity : Activity
- {
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- SetContentView(Resource.Layout.Main);
- Toast.MakeText(this, "Welcome to MainActivity", ToastLength.Long).Show();
- }
-
- }
Output
Summary
In this article, we learned how to create animated splash screens in Xamarin with Visual Studio 2015.