将图像适合 ImageView,保持宽高比,然后将 ImageView 调整为图像尺寸?
如何使随机大小的图像适合 ImageView
?
何时:
- 最初
ImageView
尺寸为 250dp * 250dp - 图像的较大尺寸应向上/向下缩放至 250dp
- 图像应保持其纵横比
ImageView
尺寸应与缩放后的图像尺寸相匹配 后
缩放 对于 100*150 的图像,图像和 ImageView
应为 166*250。
例如,对于 150*100 的图像,图像和 ImageView
应为 250*166。
如果我将边界设置为
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />
图像适合 ImageView
,但 ImageView
始终为 250dp * 250dp。
How to fit an image of random size to an ImageView
?
When:
- Initially
ImageView
dimensions are 250dp * 250dp - The image's larger dimension should be scaled up/down to 250dp
- The image should keep its aspect ratio
- The
ImageView
dimensions should match scaled image's dimensions after scaling
E.g. for an image of 100*150, the image and the ImageView
should be 166*250.
E.g. for an image of 150*100, the image and the ImageView
should be 250*166.
If I set the bounds as
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />
images fit properly in the ImageView
, but the ImageView
is always 250dp * 250dp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
可能不是这个特定问题的答案,但如果有人像我一样正在寻找答案,如何在 ImageView 中以有限的尺寸(例如,maxWidth)调整图像,同时保留宽高比,然后摆脱如果 ImageView 占用的空间过多,那么最简单的解决方案是在 XML 中使用以下属性:
May not be answer for this specific question, but if someone is, like me, searching for answer how to fit image in ImageView with bounded size (for example,
maxWidth
) while preserving Aspect Ratio and then get rid of excessive space occupied by ImageView, then the simplest solution is to use the following properties in XML:(在澄清原始问题后对答案进行了大量修改)
澄清后:
这不能仅在 xml 中完成。无法同时缩放图像和
ImageView
,以便图像的一维始终为 250dp,并且ImageView
与图像具有相同的尺寸。此代码缩放
ImageView
的Drawable
以保持在 250dp x 250dp 等正方形中,一维恰好为 250dp 并保持纵横比。然后调整ImageView
的大小以匹配缩放图像的尺寸。该代码在活动中使用。我通过按钮单击处理程序测试了它。享受吧。 :)
ImageView
的 xml 代码:感谢有关缩放代码的讨论:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
2012 年 11 月 7 日更新:
按照评论中的建议添加了空指针检查
(The answer was heavily modified after clarifications to the original question)
After clarifications:
This cannot be done in xml only. It is not possible to scale both the image and the
ImageView
so that image's one dimension would always be 250dp and theImageView
would have the same dimensions as the image.This code scales
Drawable
of anImageView
to stay in a square like 250dp x 250dp with one dimension exactly 250dp and keeping the aspect ratio. Then theImageView
is resized to match the dimensions of the scaled image. The code is used in an activity. I tested it via button click handler.Enjoy. :)
The xml code for the
ImageView
:Thanks to this discussion for the scaling code:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
UPDATE 7th, November 2012:
Added null pointer check as suggested in comments
下面的代码使位图与图像视图的大小完美一致。获取位图图像的高度和宽度,然后借助 imageview 的参数计算新的高度和宽度。这为您提供了具有最佳纵横比的所需图像。
享受。
The Below code make the bitmap perfectly with same size of the imageview. Get the bitmap image height and width and then calculate the new height and width with the help of imageview's parameters. That give you required image with best aspect ratio.
enjoy.
尝试将
android:scaleType="fitXY"
添加到您的ImageView
中。try adding
android:scaleType="fitXY"
to yourImageView
.这一切都可以使用 XML 来完成...其他方法看起来相当复杂。
不管怎样,你只需将高度设置为你想要的 dp 值,然后设置宽度以包裹内容,反之亦然。使用scaleType fitCenter调整图像的大小。
this can all be done using XML... the other methods seem pretty complicated.
Anyway, you just set the height to what ever you want in dp, then set the width to wrap content or visa versa. Use scaleType fitCenter to adjust the size of the image.
在大多数情况下有效的最佳解决方案是
以下示例:
The Best solution that works in most cases is
Here is an example:
经过一天的搜索,我认为这是最简单的解决方案:
After searching for a day, I think this is the easiest solution:
编辑Jarno Argillanders答案:
如何使图像适合您的宽度和高度:
1) 初始化 ImageView 并设置图像:
2) 现在调整大小:
编辑
scaleImage
方法:(您可以替换预期的边界值)和 . xml:
Edited Jarno Argillanders answer:
How to fit Image with your Width and Height:
1) Initialize ImageView and set Image:
2) Now resize:
Edited
scaleImage
method: (you can replace EXPECTED bounding values)And .xml:
如果它不适合你,那么用 android:src 替换 android:background
android:src 将发挥主要作用,
它像魅力一样工作正常
if it's not working for you then replace android:background with android:src
android:src will play the major trick
it's working fine like a charm
使用此代码:
Use this code:
这对我的案例来说是这样的。
This did it for my case.
我需要一个 ImageView 和一个 Bitmap,因此 Bitmap 会缩放为 ImageView 大小,并且 ImageView 的大小与缩放后的 Bitmap 相同:)。
我正在浏览这篇文章以了解如何做到这一点,最后做了我想要的,但不是这里描述的方式。
然后在 onCreateView 方法中
,然后在layoutImageView() 代码中
,结果是图像完美地适合内部,保持宽高比,
当位图位于内部时,ImageView 中不会有额外的剩余像素。
结果
ImageView 拥有这一点很重要
wrap_content 和 adjustmentViewBounds 为 true,
然后setMaxWidth和setMaxHeight就可以了,这是ImageView源码中写的,
I needed to have an ImageView and an Bitmap, so the Bitmap is scaled to ImageView size, and size of the ImageView is the same of the scaled Bitmap :).
I was looking through this post for how to do it, and finally did what I want, not the way described here though.
and then in onCreateView method
and then layoutImageView() code
And the result is that image fits inside perfectly, keeping aspect ratio,
and doesn't have extra leftover pixels from ImageView when the Bitmap is inside.
Result
It's important ImageView to have
wrap_content and adjustViewBounds to true,
then setMaxWidth and setMaxHeight will work, this is written in the source code of ImageView,
我需要在毕加索的约束布局中完成此操作,因此我将上面的一些答案放在一起并提出了这个解决方案(我已经知道我正在加载的图像的长宽比,所以这有帮助):
在 setContentView(...) 之后在我的活动代码中调用
在我的 XML 中
请注意缺少constraintBottom_toBottomOf 属性。 ImageLoader 是我自己的静态类,用于图像加载 util 方法和常量。
I needed to get this done in a constraint layout with Picasso, so I munged together some of the above answers and came up with this solution (I already know the aspect ratio of the image I'm loading, so that helps):
Called in my activity code somewhere after setContentView(...)
In my XML
Note the lack of a constraintBottom_toBottomOf attribute. ImageLoader is my own static class for image loading util methods and constants.
我正在使用一个非常简单的解决方案。这是我的代码:
我的图片是动态添加到网格视图中的。当你对imageview进行这些设置时,图片可以自动以1:1的比例显示。
I am using a very simple solution. Here my code:
My pictures are added dynamically in a gridview. When you make these settings to the imageview, the picture can be automatically displayed in 1:1 ratio.
使用简单的数学来调整图像的大小。您可以调整
ImageView
的大小,也可以调整可绘制图像的大小,而不是ImageView
上设置的大小。找到您想要在 ImageView 上设置的位图的宽度和高度并调用所需的方法。假设您的宽度 500 大于调用方法的高度您使用此类来调整位图大小。
xml 代码是
Use Simple math to resize the image . either you can resize
ImageView
or you can resize drawable image than set onImageView
. find the width and height of your bitmap which you want to set onImageView
and call the desired method. suppose your width 500 is greater than height than call methodYou use this class for resize bitmap.
xml code is
快速回答:
Quick answer:
只需将其写在 xml 中
对我有用
Just write it in xml
Worked for me
就我而言,我发现答案隐藏在这个问题的评论中(归功于@vida)。
In my case, I found the answer buried in a comment on this question (credit to @vida).
Jarno Argillander 的答案的 kotlin 版本用于我的应用程序
kotlin version of Jarno Argillander's answer used for my app
我只是在 ConstraintLayout 中使用 ImageView 并将 ImageView 中的 adjustmentviewbound 设置为 true 。
I just use ImageView inside ConstraintLayout and set adjustviewbound in ImageView to true.