【发布时间】:2019-03-18 02:11:16
【问题描述】:
我已为应用的加载主题分配了一个可绘制资源。但是,可绘制资源中的位图被剪裁了,我不知道为什么。使用 android:gravity="fill_horizontal" 会停止水平裁剪,但也会改变图像的纵横比。
如何在不剪裁边缘并保持其原始纵横比的情况下使用图像?
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Launcher/splash theme. -->
<style name="AppTheme.Launcher">
<item name="android:windowBackground">@drawable/launch_screen</item>
<item name="android:adjustViewBounds">true</item>
</style>
</resources>
launch_screen.xml
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<!-- Background color-->
<item android:drawable="@android:color/white"/>
<!-- Splash Logo -->
<item>
<bitmap
android:src="@drawable/splash_screen"
android:gravity="center"/>
</item>
</layer-list>
Bitmap using---android:gravity="center"
Bitmap using---android:gravity="fill_horizontal"
临时解决方案: 我找到了一个不完美的解决方案。通过调整项目的高度和宽度,我设法确保图像保持在容器的边界内。图像在不同屏幕尺寸上的缩放比例不同。目前,此设置是我遇到的最佳解决方案。
修改了launch_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Use android:opacity=”opaque” to prevent black flash during theme
transition. -->
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<!-- Background color-->
<item android:drawable="@android:color/white"/>
<!-- Splash Logo -->
<item
android:height="400dp"
android:width="400dp"
android:gravity="center">
<bitmap
android:src="@drawable/splash_screen"
/>
</item>
</layer-list>
【问题讨论】:
-
@Vince 我不确定你想让我做什么。我尝试了一些建议的答案,但都没有奏效。
-
已经尝试过使用gravity="fill" ?
-
@Vince 是的。它解决了裁剪问题,但它会拉伸图像以适应其容器的约束,从而导致纵横比发生变化。