在Android启动画面中维护正确的windowBackground纵横比

3

我正在为我的项目实现闪屏活动。目前,闪屏活动已经可以完美加载,并且在图像加载之前没有“白色闪光”-这很好。

我的唯一问题是如何保持闪屏屏幕图像的正确 纵横比

这是我使用的SplashActivity主题:

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

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/pink_grid"
        android:gravity="fill_vertical|clip_horizontal"
/>

以下是我所得到的结果截屏:

Improper Aspect Ratio

我使用作为背景的黑色/粉色网格图像具有均匀正方形。如您从图像中看到的,它没有保持适当的纵横比(水平被挤压)。

这是网格图像(1280x1920):

Pink and black grid image

我尝试过:

似乎唯一控制启动窗口背景纵横比的方法是使用gravity。我尝试将图像垂直填充并水平裁剪。但这无法保持纵横比。

如何调整启动图像的重力以保持纵横比并适合任何设备的屏幕?

编辑:根据 Raz 的答案取得进展:

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/pink_grid" android:scaleType="centerCrop"/>
</merge>

SplashActivity.kt

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

//        val intent = Intent(this, MainActivity::class.java)
//        startActivity(intent)
//        finish()
    }
}

AndroidManifest.xml

<application>
    <!-- ... -->
    <activity
            android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

styles.xml

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

启动画面现在只是一个黑屏。粉色/黑色网格没有显示出来。

我和你当时处于同样的情况,你解决了吗?如果是的话,请告诉我。 - Tara
3个回答

0
对于那些来到这个页面但找不到答案的人:

这里有一个解决方案

基本上,你需要将src从原始的“png/jpg/…”更改为xml,并且xml使用android:gravity="center"设置导入原始图像。


0
据我所知,windowBackground将始终填充整个屏幕。 你需要做的是:
将活动的布局设置为:
<FrameLayout 
        android:width="match_parent" 
        android:height="match_parent">
    <ImageView  
android:width="match_parent"  
android:height="match_parent"  
android:ScaleType="CENTER_CROP"  android:src="your_image"/>
        </FrameLayout>

imageView属性的scaletype将保留纵横比。

作为优化:

  1. 将windowBackground设置为透明(以减少过度绘制)
  2. 将根FrameLayout更改为<merge> - 如下所示:

<merge> <ImageView
android:width="match_parent"
android:height="match_parent"
android:ScaleType="CENTER_CROP" android:src="your_image"/> </merge>


我将窗口背景设置为透明 <item name="android:windowBackground">@android:color/transparent</item>。我不知道你所说的#2是什么意思。你能发一些XML吗? - Joe
我不确定 <merge> 如何与我的问题相关。 - Joe
你正在oncreate中立即切换到下一个活动。这不会显示启动屏幕。请删除这些行。此外,您需要setContentView(your_layout_id)。 - Raz
我添加了setContentView,并注释掉了intent。现在启动应用程序时,闪屏正确地以正确的纵横比加载...但它从未转换到主活动。它只停留在闪屏界面上。 - Joe
通常在启动屏幕中,您需要添加一个定时器或者基于某个事件来调用下一个活动(您已注释的代码)。 - Raz
显示剩余6条评论

0

当使用矢量图时,以下内容非常好用:

drawable/splash.xml

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

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