【问题标题】:Splash screen too slow in Xamarin AndroidXamarin Android 中的启动画面太慢
【发布时间】:2015-11-18 20:35:42
【问题描述】:

我正在创建一个显示初始屏幕的应用,然后创建主要活动。我正在关注这个看起来很简单的教程:https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

在实施之后,我可以成功看到启动画面,但有时(20 次中有 1 次)使用 S5 我会看到以下屏幕:

接着是(右)飞溅(取自模拟器,但只是为了说明我的观点):

所以我的猜测是,有时 Xamarin 加载应用程序需要很长时间,因此它会延迟显示启动画面。有什么办法可以防止吗?

更新 1 我已按照教程进行操作,但为此删除了睡眠:

Insights.Initialize ("<APP_KEY>", Application.Context);
StartActivity(typeof (MainActivity));

【问题讨论】:

  • 您是否在启动主题中设置了正确的图像并为活动设置了主题,如教程中所述?您的活动似乎没有 windowBackground 图像。您检查过其他版本的 Android 吗?
  • 是的,我已经设置了教程中的所有内容,并且 20 中的 19 看起来不错只是 20 中的 1 显示第一个屏幕不到一秒钟,然后以正确的布局显示第二个屏幕.
  • @FedericoM.Rinaldi,我认为您面临 OOM IN 位图处理。转到输出窗口,请参阅日志,无论如何,几年后设备将陷入地狱,我在我的中遇到了同样的问题>
  • @matthewrdev 感谢您的回复,但我没有使用睡眠,我已经更新了我的问题

标签: android xamarin xamarin.android


【解决方案1】:

该示例在 UI 线程上调用 Thread.Sleep(10000);...这将锁定应用程序并生成 ANR

通过后台睡眠然后触发下一个活动来修复它:

namespace SplashScreen
{
    using System.Threading;

    using Android.App;
    using Android.OS;

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

            Task.Run (() => {
                Thread.Sleep (10000); // Simulate a long loading process on app startup.
                RunOnUiThread (() => { 
                    StartActivity (typeof(Activity1));
                });
            });
        }
    }
}

【讨论】:

  • 感谢您的回答,但我没有使用睡眠,我已经更新了我的问题,但我认为您的回答仍然适用。我会及时通知你
【解决方案2】:

即使这篇文章相当陈旧,我在实现 SplashScreen 时也有类似的经历,并且可以通过更新 SplashScreen 的样式/主题来解决这个问题。 @frederico m rinaldi 有时看到的屏幕通常是使用 Android 的默认 (Holo) 主题创建的。

虽然您没有提供应用于 SplashScreen 的样式(请参阅 accepted answerTheme = @style/Theme.Splash),但这是我的。也许您可以检查它们是否不同。

<style name="Theme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
  <!-- Use a fully opaque color as background. -->
  <item name="android:windowBackground">@android:color/black</item>
  <!-- This removes the title bar seen within the first screen. -->
  <item name="windowNoTitle">true</item>
  <!-- Let the splash screen use the entire screen space by enabling full screen mode -->
  <item name="android:windowFullscreen">true</item>
  <!-- Hide the ActionBar (Might be already defined within the parent theme) -->
  <item name="windowActionBar">false</item>
</style>

您可能注意到我只是使用黑色作为背景,因为我的 SplashScreen 使用自定义布局文件而不是静态图像 (this.SetContentView(Resource.Layout.SplashScreen);)。此外,加载图像 (drawable) 可能需要一些时间,这可能是您可以看到默认主题而不是初始屏幕的主要原因。 此外,由于 Android 支持库功能的Google's internal implementation,我省略了某些属性的android: XML 命名空间。

请注意,为了使用 AppCompat 主题,您的应用程序必须包含 AppCompat support library,并且您的 Activity 必须是 Android.Support.V7.App.AppCompatActivity 的子类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多