安卓启动画面将Logo拉伸至全屏幕大小

8

我有一个简单的启动画面,白色背景和标志位于屏幕中央。在现代版本的Android上,它运行良好,但在“旧”版本的Android(例如API 22)中,我的标志被拉伸到整个屏幕。如何使标志启动画面在所有Android版本(API 19之前)上看起来正常?

drawable/background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

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

  <item
    android:width="150dp"
    android:height="50dp"
    android:drawable="@mipmap/my_logo"
    android:gravity="center" />

</layer-list>

values/styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:statusBarColor">@color/white</item>
    </style>

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

</resources>

需要闪屏的xml布局文件,请将其添加到您的帖子中。 - Barns
1
尝试使用此属性 android:scaleType="fitXY" - Syed Danish Haider
5个回答

14

使用 <item> 标记内部的 <bitmap> 标记,而不是简单的 <item/> 标记。

来自图层列表可绘制资源文档:

所有可绘制项默认缩放以适应包含视图的大小。为了避免对列表中的项进行缩放,请使用一个 <bitmap> 元素在 <item> 元素中指定可绘制项并将重力定义为不进行缩放的值,如 "center"

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">

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

    <item>
        <bitmap
            android:src="@mipmap/my_logo"
            android:gravity="center"/>
    </item>

</layer-list>

我们如何将图像适应屏幕的宽度和高度?使用这种方法,它会将图像缩放到屏幕中心。 - Mahesh Jamdade

1
这是因为属性android:widthandroid:height是在API级别23中引入的。如果您想显示相同大小与屏幕尺寸相关的图像,我能想到的唯一解决方案是提供替代位图资源。

这里有一个链接到Android开发者指南,进一步解释了这个问题。


1
当然这是问题,但不是解决方案。尽管如此,因为认识到了这个问题,给你点赞。 - Barns

1

首先,不要使用png图片,而是使用矢量可绘制图形。这样您就不需要为不同的屏幕尺寸准备单独的图像。使用此工具将图像转换为矢量可绘制图形 http://a-student.github.io/SvgToVectorDrawableConverter.Web/

现在,在XML文件中的图像视图中,将比例类型设置为中心裁剪或适合XY android:scaleType="centerCrop" 或使用 android:scaleType="fitxy"


-1

问题与Android版本无关,应该与设备的屏幕大小有关。

为了声明不同的布局和位图以用于不同的屏幕,您必须将这些备选资源放置在单独的目录/文件夹中。

这意味着,如果您为xhdpi设备生成了一个200x200的图像,则应为hdpi设备生成相同的资源,大小为150x150,mdpi设备为100x100,ldpi设备为75x75。

总之,Android应用程序应提供6种图像尺寸:ldpi(0.75x),mdpi(1.0x),hdpi(1.5x),xhdpi(2.0x),xxhdpi(3.0x)和xxxhdpi(4.0x)。


1
你正在用加农炮打苍蝇。这里有一个简单得多的解决方案,但这个答案没有解决实际问题。 - Barns

-4
通常我所做的是确保我的布局根节点是一个RelativeLayout,并且第一个子节点是一个ImageView,其scaleTypecenterCrop
以下是一个示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:scaleType="centerCrop"
        android:background="@drawable/img_background"/>

    ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接