【发布时间】: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