我正在为我的项目实现闪屏活动。目前,闪屏活动已经可以完美加载,并且在图像加载之前没有“白色闪光”-这很好。
我的唯一问题是如何保持闪屏屏幕图像的正确 纵横比。
这是我使用的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>
启动画面现在只是一个黑屏。粉色/黑色网格没有显示出来。