簡體   English   中英

在 Maui 的 Splash 中創建動畫(設置 MainActivity OnCreate 和其他東西)?

[英]Creating animated in Splash in Maui (setting MainActivity OnCreate and other things)?

這個問題是從一篇Android的文章開始的,我試圖在Maui實現。 主要是關於實現自定義 Android 資源 XML 文件,但我在OnCreate上實現了東西。 因此,這就是開始的地方,但隨后升級了其他潛在的解決方案。

然后@user2153142 提供了 Xamarin 樣本,解決了這個問題。 因此,這就是公認的答案,並且對這個問題進行了一些修改以顯示它。 就像它是如何開始的,它是如何進行的。

所以老問題是這樣的:

我正在按照https://www.droidcon.com/2022/10/04/splash-screen-in-android/上的教程在毛伊島設置動畫 Android 啟動畫面。

但是有一個“第5步”我不知道如何設置實際的animation。在代碼中它被描述為

class SplashScreenActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        startActivity(Intent(this@SplashScreenActivity, MainActivity::class.java))
        finish()
    }
}

我在想它可能是這樣的

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

        //So, passing this intent doesn't work here at all now. Argh! How should this be done?
        StartActivity(new Android.Content.Intent(this.SplashScreen, typeof(MainActivity)));
        Finish();
    }
}

在 .NET Maui 中,但我沒有意識到如何創建意圖,因此我可以傳遞MainActivity以及其中的SplashScreenActivity作為參數。 該活動基本上是默認活動。

我在別處了解到SplashScreenActivity應該設置為MainLauncher = true並且在 .NET Maui 中它自動生成了一個部分到AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
    android:allowBackup="true"
    android:icon="@mipmap/appicon"
    android:supportsRtl="true">

    <!--
      This activity is generated by SplasScreenActivity,
      the contents may differ as they depend on that activity
      definition. This can be verified by looking at
    \obj\Debug\net7.0-android\android\manifest\AndroidManifest.xml
    -->
    <!--
    <activity
            android:name=".SplashScreenActivity"
            android:exported="true"
            android:theme="@style/SplashTheme">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    -->

  </application>  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="33" />
</manifest>

似乎不能在 styles.xml 中設置<item name="postSplashScreenTheme">@style/MainTheme</item> styles.xml聲明主題,因為它不能在描述 Splash 的 styles 中設置Parent="Theme.SplashScreen"主題(可能值可以從某處復制?)。

在同一教程中,還有一種更高級的設置活動的方法。 可能會更好,來自教程

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Enable support for Splash Screen API for
        // proper Android 12+ support
        installSplashScreen()
        
        setContentView(R.layout.activity_main)
    }
}

但是 .NET Maui 好像沒有InstallSplashScreen 我也不知道如何在 .NET 中調用setContentView(R.layout.activity_main) (但是有SetContentView function)。

也有其他教程可能允許人們繞過其中一些問題。 一個位於https://www.truiton.com/2022/10/implementing-the-splash-activity-in-android-the-right-way/ ,但AppCompatActivity函數似乎也會出現類似的問題。

但它變得復雜了。 提供的答案接近價格,Maui 基於 Android(和舊的)Xamarin 位構建。 或者我相信它所做的。

以下是我所做的一些修改:

.csproj中有

 <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
    <PackageReference Include="Xamarin.AndroidX.Core.SplashScreen" Version="1.0.0" />
  </ItemGroup>

 <MauiIcon Condition="$(TargetFramework.Contains('-android')) != true" Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
    <MauiIcon Include="Resources\AppIcon\appiconfg.svg" />

    <!-- Splash Screen -->
    <MauiSplashScreen Condition="$(TargetFramework.Contains('-android')) != true" Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

(也許它們都應該是有條件的,但這就是現在的情況並且有效。)

資源布局如下: 在此處輸入圖像描述

AndroidManifest.xml是這樣定義的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="28" android:targetSdkVersion="33" />
</manifest>

我認為將 SDK 定為 31 歲以上很重要。

styles.xml定義如下

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="AppTheme.Starting" parent="Theme.SplashScreen.IconBackground">
    <item name="windowSplashScreenAnimatedIcon">@mipmap/appicon_foreground</item>
    <item name="android:windowSplashScreenAnimatedIcon">@drawable/animated_face</item>
    <item name="windowSplashScreenBackground">?android:colorBackground</item>
    <item name="postSplashScreenTheme">@style/AppTheme</item>
  </style>

  <style name="AppTheme" parent="Maui.SplashTheme">
    <item name="android:windowSplashScreenBackground">@color/colorPrimary</item>
    <item name="android:windowLightStatusBar">true</item>
  </style>
</resources>

這里AppTheme是主主題, AppTheme.Starting是“splash theme”。 在我看來,這意味着將這些動作鏈接在一起。 請注意,與答案中的評論不同,我認為這是它在MainActivity.cs中設置的內容並且它似乎有效。 設置AppTheme沒有,只有應用程序背景顯示。 這可能是因為沒有設置windowSplashScreenAnimatedIcon 我沒試過,也許吧。 它也沒有android:前綴,也許也可以使用它。 或諸人無也。

我認為colors.xml沒有什么特別之處。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#ffff1c</color>
    <color name="colorPrimaryDark">#00c3ff</color>
    <color name="colorAccent">#3b3d3b</color>
</resources>

該文件 `animated_face.xml" 是從https://yggr.medium.com/exploring-android-12-splash-screen-21f88cc8e8f8復制並修改的,因此它會閃爍(我需要找到我可以理解和測試的東西)

<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
  <aapt:attr name="android:drawable">
    <vector
        android:name="vector"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24"
        android:viewportHeight="24">
      <path
          android:name="path"
          android:pathData="M 9 11.75 C 8.31 11.75 7.75 12.31 7.75 13 C 7.75 13.69 8.31 14.25 9 14.25 C 9.69 14.25 10.25 13.69 10.25 13 C 10.25 12.31 9.69 11.75 9 11.75 Z M 15 11.75 C 14.31 11.75 13.75 12.31 13.75 13 C 13.75 13.69 14.31 14.25 15 14.25 C 15.69 14.25 16.25 13.69 16.25 13 C 16.25 12.31 15.69 11.75 15 11.75 Z M 12 2 C 6.48 2 2 6.48 2 12 C 2 17.52 6.48 22 12 22 C 17.52 22 22 17.52 22 12 C 22 6.48 17.52 2 12 2 Z M 12 20 C 7.59 20 4 16.41 4 12 C 4 11.71 4.02 11.42 4.05 11.14 C 6.41 10.09 8.28 8.16 9.26 5.77 C 11.07 8.33 14.05 10 17.42 10 C 18.2 10 18.95 9.91 19.67 9.74 C 19.88 10.45 20 11.21 20 12 C 20 16.41 16.41 20 12 20 Z"
          android:fillColor="#000"
          android:strokeWidth="1"/>
    </vector>
  </aapt:attr>
  <target android:name="path">
    <aapt:attr name="android:animation">
      <objectAnimator
          android:propertyName="fillColor"
          android:duration="2000"
          android:valueFrom="#bb86fc"
          android:valueTo="#ffffff"
          android:valueType="colorType"
          android:repeatCount="-1"
          android:repeatMode="reverse"
          android:interpolator="@android:interpolator/fast_out_slow_in"/>
    </aapt:attr>
  </target>
</animated-vector>

最后MainActivity.cs看起來像這樣

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;


namespace SomeApp;

[Activity(Theme = "@style/AppTheme.Starting", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity: MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen(this);

        base.OnCreate(savedInstanceState);

        // Include this if using the Splash Screen API - rotate to Landscape on a device with a notch to see why - with this code commented.
        if(Build.VERSION.SdkInt >= BuildVersionCodes.P)
        {
            Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.Default;
        }

        // If you want to get a good look at the icon to check it. Don't forget remove from production code
        System.Threading.Thread.Sleep(1000);

        // Set our view from the "main" layout resource
        //SetContentView(Resource.Layout.activity_main);
    }
}

所以,幾乎就像應用程序一樣。 當我運行它時,當應用程序正在加載時,臉在閃爍(當它在調試器中停止時,那里有一個斷點),然后切換到應用程序屏幕。

我不知道AndroidManifest.xml應該是什么樣子,所以它是正確的,但可以在\obj\Debug.net7.0-android\AndroidManifest.xml上查看它。 我將在此處包含這些位信息,因為這是我在執行此操作時學到的東西。

我想就是這樣。 實際代碼位於https://github.com/gmck/XamarinBasicSplashScreen並且這里有輕微的 Maui 相關修改以使其工作。

如果您知道這可以改進或完善,請插話。似乎有一些相關的內容,例如https://github.com/do.net/maui/issues/3300

@Veksi

要了解應如何在 Xamarin.Android 中實現初始屏幕,您可以使用以下示例https://github.com/gmck/XamarinBasicSplashScreen

Android 12 及更高版本不能做的一件事是使用上面提到的兩種活動技術。 只有一行代碼被添加到標准的空白應用程序模板中,rest 是通過一個額外的主題完成的。 適用於 Android 13 到 Android 5.0

我無法幫助您使用 Maui,因為我從未使用過它,但如果您閱讀線程https://github.com/xamarin/xamarin.android/issues/7239 ,您將看到這如何幫助 developer9969 和他的 xamarin.forms應用程序,因此我假設您可以對毛伊島做類似的事情。

有關 Android 的更多詳細信息,請參閱https://developer.android.com/develop/ui/views/launch/splash-screen

如果您覺得更有幫助,我還可以將它從 xamarin.android 轉換為 .net7.0-android 應用程序。

我對毛伊島一無所知,因為我從未使用過它。 我只是快速瀏覽了一個標准 Maui 應用程序,構建了一個標准應用程序並檢查了 obj 文件夾中生成的清單。 清單文件將主題輸入為 android:theme="@style/Maui.SplashTheme",這就是您通常使用的主題。 但是,您可能不希望這樣,因此請繼續搜索 obj 文件夾。 我找到 styles.xml 的路徑是 C:\ProjectsVS2022Preview\MauiApp1\obj\Debug.net6.0-android\lp\154\jl\res\values\styles.xml

在 VS 中打開該文件並重新格式化文本以使其更易於閱讀。 似乎有兩個 styles Maui.MainTheme 和 Maui.MainTheme.NoActionBar。 您選擇哪一個取決於您的 android 應用程序類型。 我的應用程序有 @style/AppTheme 的地方將 postSplashScreenTheme 更改為那些 Maui 主題名稱之一,然后刪除 Activity 屬性中的任何主題。

請記住,這只是一個 Android 解決方案 - 不適用於其他平台。

@Veksi 我重讀了您的原始帖子,現在才意識到您已經看過我的 XamarinBasicSplashScreen 應用程序。 我可以在您的原始帖子中看到包含錯誤的樣式。

name="android:windowSplashScreenAnimatedIcon">@drawable/animated_face android:如果你使用的是 androidx.core.splashscreen 是錯誤的

如果你檢查我的風格,它是

<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_foreground</item>

使用 XamarinSplashScreenBasic 時

<item name="windowSplashScreenAnimatedIcon">@mipmap/appicon_foreground</item>

現在在 xamarinSplashScreenBasicNet7

基本上任何時候你看到 android:左邊的東西是框架,因此 Android 12+。 所以要使用小於 12 的設備,我們需要 androidX。

您的示例混合了 android 框架方式,而不是使用 androidx.core.splashscreen 的語法。 所以那永遠不會奏效。

作為 Xamarin 用戶使用 Xamarin.AndroidX.Core.SplashScreen 我們必須使用 androidX 語法。

SomeApp 命名空間示例非常正確,只是它不需要 Activity 屬性中的主題,因為它應該已經在清單中了。 當您查看評論時,它看起來像是來自我的應用程序。

如果你確實想做 animation 的圖標,那么你需要點擊資源管理器 Android 12 鏈接。

我建議讓 XamarinSplashScreenBasicNet7 在 Maui.Net7 應用程序中工作,就像基本的 Android 12 啟動畫面一樣,然后再嘗試讓 animation 工作。

但是 .NET Maui 好像沒有 InstallSplashScreen。 好吧,在 Xamarin 中,但 C# 語法完全不同

AndroidX.Core.SplashScreen.SplashScreen.InstallSplashScreen(this);

當你把毛伊島弄好后,你能發一個鏈接嗎?

暫無
暫無

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

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