缩放图像以填充 ImageView 宽度并保持纵横比
问题描述
我有一个 GridView
.GridView
的数据是来自服务器的请求.
I have a GridView
. The data of GridView
is request from a server.
这是GridView
中的item布局:
Here is the item layout in GridView
:
我从服务器请求数据,获取图像 url 并将图像加载到 Bitmap
I request data from server, get image url and load image to Bitmap
adapter中有getView(int position, View convertView, ViewGroup parent)
方法的代码
and there is the code of getView(int position, View convertView, ViewGroup parent)
method in adapter
图片尺寸为210*210
.我在 Nexus 4 上运行我的应用程序.图像确实填充了 ImageView
宽度,但 ImageView
高度没有缩放.ImageView
不显示整个图像.
The image size is 210*210
. I run my application on my Nexus 4. The image does fill ImageView
width, but the ImageView
height does not scale. ImageView
does not show the whole image.
我该如何解决这个问题?
How do I solve this problem?
推荐答案
不使用任何自定义类或库:
Without using any custom classes or libraries:
scaleType="fitCenter"
(省略时默认)
- 将使其尽可能宽,并根据需要保持纵横比.
scaleType="centerInside"
- 如果
src
的固有宽度小于父宽度
将图像水平居中 - 如果
src
的固有宽度大于父级宽度
将使其与父级允许的宽度一样宽,并按比例缩小以保持纵横比.
- if the intrinsic width of
src
is smaller than parent width
will center the image horizontally - if the intrinsic width of
src
is larger than parent width
will make it as wide as the parent allows and down-scale keeping aspect ratio.
使用 android:src
或 ImageView.setImage*
方法没关系,关键可能是 adjustViewBounds
.
It doesn't matter if you use android:src
or ImageView.setImage*
methods and the key is probably the adjustViewBounds
.
这篇关于缩放图像以填充 ImageView 宽度并保持纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!