android SplashScreen是中心裁剪

gcuhipw9  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(144)

对于我的应用程序,我创建一个闪屏使用drawable资源文件包括一个layerlist有一个彩色的背景和一个中心矢量drawable的标志。

splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/grey_700" />
    <item
        android:drawable="@drawable/splash_foreground"
        android:gravity="center" />
</layer-list>

style.xml

...

<style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:statusBarColor">@color/grey_900</item>
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

...

AndroidManifest.xml

...

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.SplashScreen"
            android:screenOrientation="portrait"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

...

splash_foreground.xml

问题

输出应该显示splash_foreground在屏幕中居中,而背景颜色在整个屏幕上被裁剪,但显示一个居中裁剪的启动画面。

qjp7pelc

qjp7pelc1#

我通过使用新的splash_screen API解决了这个问题。

解决方案

*build.gradle中增加闪屏API

implementation 'androidx.core:core-splashscreen:1.0.0'

*AndroidManifest.xml文件的application标签前添加此行

<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />

*theme.xmltheme.xml (night)中新建样式,父样式为Theme.SplashScreen

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="android:statusBarColor">@color/grey_900</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item>
        <item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item>
        <item name="windowSplashScreenBackground">@color/grey_700</item>
        <item name="postSplashScreenTheme">@style/Theme.LGConnect</item>
    </style>

*将应用主题和Launcher Activity主题设置为新创建的样式

...
<application
...
   android:theme="@style/Theme.App.Starting"
...
      <activity
         ...
         android:theme="@style/Theme.App.Starting"
...

*MainActivityonCreate方法中添加此行

...
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      installSplashScreen()
...

相关问题