【问题标题】:Maintaining correct aspect ratio for windowBackground on Android splash activity在 Android 启动活动中保持 windowBackground 的正确纵横比
【发布时间】:2019-11-17 10:23:27
【问题描述】:

我正在努力在我的项目中实施启动活动。飞溅活动当前加载完美,并且在图像加载之前没有“白色闪光” - 这很好。

我唯一的问题是保持启动画面图像的正确纵横比

这是我用于 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"
/>

这是我得到的结果的屏幕截图:

我用作背景的黑色/粉色网格图像具有统一的正方形。从图像中可以看出,它没有保持适当的纵横比(被水平挤压)。

这是网格图像 (1280x1920):

我尝试了什么:

似乎控制初始窗口背景的纵横比的唯一方法是使用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>

启动画面现在只是一个黑屏。粉红色/黑色网格未显示。

【问题讨论】:

  • 我的情况和你一样。你解决了吗?如果是,请告诉我。

标签: android android-activity splash-screen launch aspect-ratio


【解决方案1】:

AFAIK,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 更改为 &lt;merge&gt; - 像这样:

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

【讨论】:

  • 我将windowBackground设置为透明​​@android:color/transparent。我不知道你说的#2是什么意思。你能发布一些 XML 吗?
  • 对不起,我在手机上,所以不能写任何代码,但你可以看看这个。 stackoverflow.com/questions/8834898/…
  • 其实更多的是优化。你可以设置一个框架布局都一样
  • 我不确定 与我的问题有什么关系。
  • @Joe,我编辑了我的答案。希望现在更容易理解了。
【解决方案2】:

使用矢量可绘制对象时,以下效果非常好:

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>

【讨论】:

    【解决方案3】:

    对于那些登陆此页面但找不到答案的人:

    Here 是一个解决方案

    基本上,您需要将 src 从原始的“png/jpg/...”更改为 xml,然后 xml 导入带有设置 android:gravity="center" 的原始图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2018-04-02
      • 2018-03-11
      • 1970-01-01
      相关资源
      最近更新 更多