缩放图像,以填补ImageView的宽度,并保持宽高比 [英] Scale Image to fill ImageView width and keep aspect ratio
问题描述
我有一个的GridView
。的数据的GridView
是从服务器请求。
I have a GridView
. The data of GridView
is request from a server.
下面是的GridView
的项目布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/analysis_micon_bg"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/half_activity_vertical_margin"
android:paddingLeft="@dimen/half_activity_horizontal_margin"
android:paddingRight="@dimen/half_activity_horizontal_margin"
android:paddingTop="@dimen/half_activity_vertical_margin" >
<ImageView
android:id="@+id/ranking_prod_pic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/ranking_rank_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/ranking_prod_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/ranking_prod_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我请求数据从服务器获取图像的URL和加载图像位图
public static Bitmap loadBitmapFromInputStream(InputStream is) {
return BitmapFactory.decodeStream(is);
}
public static Bitmap loadBitmapFromHttpUrl(String url) {
try {
return loadBitmapFromInputStream((InputStream) (new URL(url).getContent()));
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return null;
}
}
和有 getView(INT位置,查看convertView,ViewGroup中父)
适配器方法
Bitmap bitmap = BitmapUtil.loadBitmapFromHttpUrl(product.getHttpUrl());
prodImg.setImageBitmap(bitmap);
图片大小 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:
<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
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.
如果您使用不要紧机器人: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的宽度,并保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!