You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui中Android动画启动页的实现问题与解决方法

在.NET Maui中实现Android动画启动页的完整方案

问题背景

此前参考Android原生教程在.NET Maui中实现Android动画启动页时,卡在OnCreate方法的Intent创建环节,无法正确跳转至MainActivity。后续通过Xamarin相关示例找到解决方案,现将完整实现步骤整理如下:

配置修改

1. 项目文件.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" />

2. 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>

3. 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>

4. 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>

5. animated_face.xml动画资源

将该文件放置在Resources/drawable目录下:

<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>

6. 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);

        // 适配刘海屏设备横屏显示
        if(Build.VERSION.SdkInt >= BuildVersionCodes.P)
        {
            Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.Default;
        }

        // 测试时保留以查看动画效果,生产环境必须移除
        System.Threading.Thread.Sleep(1000);
    }
}

效果说明

运行应用后,启动页会显示颜色渐变循环的动画图标,加载完成后自动跳转至应用主界面。可通过\obj\Debug\net7.0-android\AndroidManifest.xml路径查看自动生成的清单文件,验证配置是否生效。

内容的提问来源于stack exchange,提问作者Veksi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:40:30