根据条件显示不同的闪屏

时间:2016-08-03 20:50:23

标签: android ios xamarin xamarin.ios xamarin.android

我想要做的是显示白天图片的闪屏,如果它在某些时间之间,那么是夜间图片,如果它不在这些时间之间。首先,我想知道这是否可能。我在这个项目中使用Xamarin Forms,但即使拥有iOS或Android的代码也会有所帮助。对于Android,我现在的代码只显示一个闪屏。我只是不确定是否有任何改变。

Styles.axml

<style name="Theme.Splash" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash</item>
    <item name="android:windowNoTitle">true</item>
</style>

MainActivity.cs

[Activity(Theme = "@style/Theme.Splash", //Indicates the theme to use for this activity
         MainLauncher = true, //Set it as boot activity
         NoHistory = true)] //Doesn't place it in back stack
public class SplashActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        System.Threading.Thread.Sleep(3000); //Let's wait awhile...
        this.StartActivity(typeof(MainActivity));
    }
}

2 个答案:

答案 0 :(得分:1)

首先,我强烈建议您按照this tutorial以正确的方式创建启动画面。我不知道这会如何转化为Xamarin(因为我没有使用过平台),但应该适用相同的概念。

根据您发布的答案,您似乎只是想根据是白天还是晚上来更改启动画面。这可以通过nightnotnight resource qualifiers轻松完成。

例如,如果您为启动画面使用drawable,则可以创建drawable-night文件夹。然后,将您日间启动画面的drawable放在drawable文件夹中,并在drawable-night文件夹中放置夜间启动画面的drawable(确保两个drawable具有相同的名称)。

答案 1 :(得分:0)

如果有人有兴趣或将来有这个问题,这就是我最终在Android上做的事情(尚未看过iOS)。我认为它与我可以得到的基于时间的2个不同的闪屏很接近。它最初加载黑屏,然后根据一天中的时间,加载白天或夜间图片:

在styles.xml中

<style name="Theme.Splash" parent="android:Theme">  
    <item name="android:windowBackground">@drawable/splash</item>  <!--Daytime--> 
    <item name="android:windowNoTitle">true</item>
</style>

<style name="Theme.Splash2" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash2</item>  <!--Nighttime-->
    <item name="android:windowNoTitle">true</item>
</style>

<style name="Theme.Splash3" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash3</item>  <!--All black-->
    <item name="android:windowNoTitle">true</item>
</style>

在MainActivity.cs

 [Activity(Theme = "@style/Theme.Splash3", MainLauncher = true, NoHistory = true)]
public class SplashActivity3 : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        System.Threading.Thread.Sleep(1);

        if ((System.DateTime.Now.Hour >= 6) && (System.DateTime.Now.Hour <= 18))
        {
            StartActivity(typeof(SplashActivity1));
        }

        else
        {
            StartActivity(typeof(SplashActivity2));

        }
    }


    [Activity(Theme = "@style/Theme.Splash", NoHistory = true)]
    public class SplashActivity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();


        }
    }

    [Activity(Theme = "@style/Theme.Splash2", NoHistory = true)]
    public class SplashActivity2 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();

        }
    }
相关问题