Search code examples
animationxamarinxamarin.androidsplash-screen

Xamarin Android: Animated Splash Screen


I want to create an animated splash screen, but i got this error:

Android.Content.Res.Resources+NotFoundException: File res/drawable/splash_screen.xml from drawable resource ID #0x7f0200bc

My code works when i change the src of the Imageview to @drawable/logo2, but then i get a static splas screen.

SplashActivity.cs

[Activity(Theme = "@style/MyTheme", MainLauncher = true, NoHistory = true,     ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashActivity : Activity
{
    static readonly string TAG = "X:" + typeof(SplashActivity).Name;

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

        // Create your application here
    }

    protected override void OnResume()
    {
        base.OnResume();

        Task startupWork = new Task(() =>
            {
                Task.Delay(5000); // Simulate a bit of startup work.
            });

        startupWork.ContinueWith(t =>
            {
                StartActivity(typeof(MainActivity));
            }, TaskScheduler.FromCurrentSynchronizationContext());

        startupWork.Start();
    }

    public override void OnWindowFocusChanged(bool hasFocus)
    {
        if (hasFocus)
        {
            ImageView imageView = FindViewById<ImageView>(Resource.Id.animated_android);
            AnimationDrawable animation = (AnimationDrawable)imageView.Drawable;
            animation.Start();
        }
    }
}

Resources/Values/Style.xml

<resources>
    <style name="MyTheme" parent="Theme.AppCompat.Light">
     <item name="android:windowBackground">@drawable/splash_screen</item>
     <item name="android:windowNoTitle">true</item>
   </style>
</resources>

Resources/Drawable/splash_screen.xml

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/splash_background"/>
  </item>
  <item>
     <ImageView android:id="@+id/animated_android"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@anim/animate_android"
      />
  </item>
</layer-list>

Resources/Anim/animate_android.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
            android:oneshot="false">
  <item android:drawable="@drawable/logo2"
    android:duration="100" />
  <item android:drawable="@drawable/Move"
    android:duration="100" />
  <item android:drawable="@drawable/logo"
    android:duration="100" />
  <item android:drawable="@drawable/icon"
    android:duration="100" />
  <item android:drawable="@drawable/About"
    android:duration="100" />
</animation-list>

Solution

  • You are doing a few things wrong.

    You need to use a Layout and not drawable as base. In the Layout you need to find the image and set the appropriate animation to it.

    This tutorial has a sample at the end to do it - Xamarin.Forms (Android): Workaround For Splash Screen With Logo, Custom Background and Animation

    The basic steps are :

    1. Create the Spalsh layout
    2. Create a drawabale
    3. Create the animation
    4. Wire them up all together in your activity (Note that if you add animation to the "OnCreate" method, the animation will not start.).