我目前有一个带有背景和前景图像的 FrameLayout。前景图像使用 alpha 具有透明度,以便看穿并观察下面的背景。
前景图像需要保持与原始位图相同的纵横比,因为它由黑色屏幕中的透明圆圈组成,很像望远镜效果。我正在寻找任何分辨率兼容性。
不幸的是,我无法做到这一点,前景总是显示它的圆圈被拉伸(stretch)。
这是 XML 代码的一部分:
<FrameLayout
android:id="@+id/layout_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:foreground="@drawable/foreground"
android:foregroundGravity="center|fill_horizontal"
>
我也在尝试解决我的问题,提供两张不同的图片,一张比例为 4:3,另一张比例为 16:9,每张图片都位于不同的可绘制文件夹中,long 和 < strong>notlong 限定词。
/drawable-long/foreground.png (16:9 aspect)
/drawable-notlong/foreground.png (4:3 aspect)
仍然没有成功。
你知道如何获得这种效果吗?
感谢您的宝贵时间。
最佳答案
在您的 FrameLayout 中更改此属性:
android:foregroundGravity="center"
这会将前景可绘制对象放置在 FrameLayout 的中心,而不改变其大小。
"center|fill_horizontal"
会将其置于中心并拉伸(stretch)其宽度以填充 FrameLayout 宽度,这显然不是您想要的。
关于java - 如何保持前景图像的纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11961265/