簡體   English   中英

我的啟動畫面結束得太早了。 如何在 Android 上手動隱藏啟動畫面?

[英]My splash screen ends too early. How can I manually hide a splash screen on Android?

我為我的 Android 游戲創建了一個閃屏主題。 但問題是我的啟動畫面在 OnCreate(Bundle bundle) 完全執行后自動結束,這導致在執行 Draw(GameTime gameTime) 之前出現黑屏。 我在 Draw(GameTime gameTime) 中繪制我的游戲精靈。 因此,在執行 Draw(GameTime gameTime) 之前一直是黑屏。

當我的游戲代碼已經達到 Draw(GameTime gameTime) 時,是否可以手動結束啟動畫面? 我不希望在 OnCreate(Bundle bundle) 完全執行時啟動畫面已經結束。

splash_screen_svg.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="320dp"
android:height="480dp"
android:viewportWidth="84.666664"
android:viewportHeight="127">
<path
  android:pathData="M0.529,0.529h83.608v125.942h-83.608z"
  android:strokeWidth="1.05833"
  android:fillColor="#00ffff"
  android:strokeColor="#000000"/>
<path
  android:pathData="M21.431,32.015h41.804v62.971h-41.804z"
  android:strokeWidth="0.529167"
  android:fillColor="#ff0000"
  android:strokeColor="#000000"/>
</vector>

colors.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <color name="splash_background">#eff1f7</color>
</resources>

字符串.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">splashapp</string>
</resources>

styles.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
 <style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="android:windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="android:windowActionBar">false</item>
     <item name="android:windowFullscreen">true</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="android:colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="android:colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
    <item name="android:colorAccent">#FF4081</item>
    <!-- You can also set colorControlNormal, colorControlActivated
     colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
<style name="SplashTheme" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen_svg</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>

活動1.cs:

namespace splashapp
{
[Activity(
    Label = "@string/app_name",
    MainLauncher = true,
    Theme = "@style/SplashTheme",
    Icon = "@drawable/icon",
    AlwaysRetainTaskState = true,
    LaunchMode = LaunchMode.SingleInstance,
    ScreenOrientation = ScreenOrientation.FullUser,
    ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize
)]
public class Activity1 : AndroidGameActivity
{
    private Game1 _game;
    private View _view;

    protected override void OnCreate(Bundle bundle)
    {
        base.SetTheme(Resource.Style.MainTheme);
        base.OnCreate(bundle);
        _game = new Game1();
        _view = _game.Services.GetService(typeof(View)) as View;
        SetContentView(_view);
        _game.Run();
    }
}
}

游戲1.cs:

namespace splashapp
{
public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        
        // TODO: use this.Content to load your game content here
    }

    protected override void Update(GameTime gameTime)
    {
        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}
}

我的解決方案圖片

是否可以使用我的 splash_screen_svg.xml 或 svg 文件?

在可繪制文件夾中創建一個 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>
  <bitmap
    android:src="@drawable/splash_logo"
    android:tileMode="disabled"
    android:gravity="center"/>
  </item>
</layer-list>

在 Resources/values/colors.xml 中添加特殊顏色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  ...
  <color name="splash_background">#FFFFFF</color>
</resources>

在 Resources/values/styles.xml 中創建主題

 <style name="Theme_SplashScreen" parent ="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowActionBar">true</item>

在 Activity_SplashScreen 活動中加載啟動畫面。

 [Activity(Label = "Activity_SplashScreen", Theme = "@style/Theme_SplashScreen", MainLauncher = true)]
public class Activity_SplashScreen : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
    }
    protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }
    // Simulates background work that happens behind the splash screen
    async void SimulateStartup()
    {             
        await Task.Delay(3000); // Simulate a bit of startup work.            
        StartActivity(new Intent(Application.Context, typeof(Activity_Screen)));
    }
}

Activity_Screen 活動:

 [Activity(Label = "Activity_Screen")]
public class Activity_Screen : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.layout_Screen);
        Toast.MakeText(this, "Welcome to MainActivity", ToastLength.Long).Show();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM