2

我基本上将以下xml用于Android启动画面:空活动windowBackground

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/defaultBackground" />
    </item>
    <item>
        <bitmap
            android:src="@drawable/logo"
            android:tileMode="disabled"
            android:gravity="center"/>
    </item>
</layer-list>

只要logo.png小于屏幕尺寸,它就可以正常工作。如果logo.png大于屏幕,则超出屏幕。

我看到了 3 种解决方法,但都有缺点:

  1. 设置left/ rightin <item,但这需要 API 23+
  2. 因等而异@drawable/logo,但我使用的是密度拆分,这会在将 apk 重用于其他设备(apk 站点、“移动到新设备”-传输 apk 的应用程序等)时破坏它xhdpixxhdpi
  3. 使用带有 的布局ImageView,但这有明显的延迟

如何正确/没有缺点地做到这一点?

4

1 回答 1

4

我没有找到好的解决方案,所以我查看了 Google Drive 的启动画面是如何实现的。

基本上,他们使用splash.png384x384px 的单个并将其放入drawable-xhdpi并使用以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list android:opacity="opaque"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/defaultBackground" />
    <item>
        <bitmap android:gravity="center" android:src="@drawable/splash" />
    </item>
</layer-list>

这似乎在所有设备(我测试过的)上看起来都不错,并解决了我的问题(特别是 2 个)。强烈推荐!

于 2020-02-13T00:11:03.553 回答