Android 初始屏幕:缩放位图

Tea*_*lls 3 android splash-screen android-layout pixel-density screen-density

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

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Tea*_*lls 7

我没有找到好的解决方案,所以我研究了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>
Run Code Online (Sandbox Code Playgroud)

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