【问题标题】:Drawable as background of splash theme doesn't keep ratio可绘制为启动主题的背景不保持比例
【发布时间】:2018-01-09 10:39:20
【问题描述】:

在我的应用程序中,我尝试将splash theme 设置为drawable background。如果我使用小图像,它可以正常工作,但是当图像大于屏幕尺寸时,它就不再工作了。

图像水平缩放正确,但height 不是,如下所示:

我尝试了所有比例类型和重力,我还能做什么?

这是我的主题:

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

这是我的 background_splash.xml

<item android:drawable="@android:color/white" />

<item
    android:left="50dp"
    android:right="50dp"
    >
    <bitmap android:src="@drawable/logo_splash"
        android:gravity="center|clip_horizontal"
        android:scaleType="centerInside"
        />
</item>

这是我的清单(相关代码):

    <activity
        android:name=".activities.SplashScreenActivity"
        android:configChanges="orientation|screenSize"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

有什么想法吗?

【问题讨论】:

  • 你为什么不直接使用ImageView 和正确的ScaleType 而不是设置背景。
  • @ADM 感谢您的回复。这是因为如果您设置了启动布局而不是启动主题,当应用程序启动时会显示片刻白屏,然后您将看到布局。通过这种方式,您立即看到没有白屏的资源
  • @drawable/logo_splash 的像素大小是多少?
  • @ThomasMary 相当大:1050 x 1050。我稍后会减小它,但无论如何我都希望有一种方法来做到这一点
  • 如果在 background_splash.xml 中删除 android:gravity="center|clip_horizontal" 会怎样?

标签: android xml splash-screen scaletype


【解决方案1】:

您可以在 background_splash.xml 中使用这样的层列表:

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

    <item
        android:drawable="@drawable/logo_splash"
        android:gravity="center" />
</layer-list>

这将使您的图像在屏幕上居中,左侧空间将被白色背景颜色填充。

也许您必须添加不同尺寸的图像,如您在此处阅读的: https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp

如果这样做,您可以访问具有相同名称的图像。您的项目结构将如下所示:

如果您想删除启动屏幕中的操作和状态栏,可以在 SplashTheme 中添加以下行:

<item name="android:windowFullscreen">true</item>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2012-08-25
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多