Xamarin Android:动画启动画面

时间:2016-08-02 12:38:46

标签: animation xamarin xamarin.android splash-screen

我想创建一个动画启动画面,但是我收到了这个错误:

  

Android.Content.Res.Resources + NotFoundException:File   res / drawable / splash_screen.xml来自可绘制资源ID#0x7f0200bc

当我将Imageview的src更改为@ drawable / logo2时,我的代码有效,但后来我得到了静态splas屏幕。

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();
        }
    }
}

资源/值/ 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>

资源/绘制对象/ 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>

资源/动画/ 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>

1 个答案:

答案 0 :(得分:1)

你做错了一些事。

您需要使用布局而不是可绘制的基础。在布局中,您需要找到图像并为其设置适当的动画。

本教程最后有一个示例 - Xamarin.Forms (Android): Workaround For Splash Screen With Logo, Custom Background and Animation

基本步骤是:

  1. 创建Spalsh布局
  2. 创建drawabale
  3. 制作动画
  4. 在您的活动中将它们连接在一起(请注意,如果您向“OnCreate”方法添加动画,则动画将无法启动。)。