将图像缩放到最大可用宽度,保持宽高比并避免 ImageView 额外空间
我有这个简单的布局 XML 和一个 30X30 像素的 green_square.png 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:background="@color/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="@drawable/green_square"/>
</LinearLayout>
我想要实现的是缩放图像以填充父宽度,保持其纵横比,而没有 ImageView 的额外空间。
我在 ImageView 中添加了红色背景,只是为了参考我想要避免的额外空间。
I have this simple layout XML, and a green_square.png file which is 30X30 pixels
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:background="@color/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="@drawable/green_square"/>
</LinearLayout>
What I'm trying to achieve is scaling the image to fill the parent width, keeping it's aspect ratio with no extra space of the ImageView.
I've added a red background to the ImageView just for reference of the extra space I want to avoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这对我有用。母体和中心作物的全宽以保持纵横比
this works for me. Full width of parent and center crop to keep aspect ratio
就是这么简单。只需将宽度设为 fill_parent ,将高度设为 wrap_content 即可。这是我想出来的。
It's so simple. Just make the width fill_parent and height wrap_content. Here is the I figured it out.
在我看来,这个
ImageView
额外布局是一个错误。我也发生过同样的事情。结果,我想要遵循green_square
的文本遵循了额外的布局。所以我所做的就是指定相对于屏幕底部的文本。这样,文本就会覆盖额外的布局。我承认,这不是最好的解决方案,但它看起来比green_square
和文本之间有巨大的空间要好得多。In my view, this
ImageView
extra layout is a bug. I had the same thing happen. As a result, text that I wanted to follow thegreen_square
followed the extra layout. So what I did was specify the text relative to the bottom of the screen. That way, the text would go over the extra layout. I admit, it wasn't the best solution, but it looks much better than having a huge space between thegreen_square
and the text.这里有很多很好的答案,而且确实有效。我只是想分享一下我现在使用更现代的 XML 语法(即约束视图)执行此操作的方式。相信我约束视图车!
当然
工具:context =“com.collect savings.collect.collectpro.LoginAndRegisterPrompt”
和
android:src="@drawable/green_square"
都应该分别匹配您的应用程序活动上下文和drawabl 资源。
There lots of great answers here which actually works. I just what to share the way I do this now with a more modern XML syntax, the constraint view. Trust me constraint view rooks!
of course the
tools:context="com.collectsavings.collect.collectpro.LoginAndRegisterPrompt"
and
android:src="@drawable/green_square"
Should all match your app activity context and drawabl resources respectively.
使用比例类型“fitCenter”或“fitxy”
Use scale type "fitCenter" or "fitxy"
试试这个,
Try this,