缩放图像以填充 ImageView 宽度并保持纵横比 [英] Scale Image to fill ImageView width and keep aspect ratio

查看:17
本文介绍了缩放图像以填充 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:

<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 并将图像加载到 Bitmap

I request data from server, get image url and load image to Bitmap

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;
    }
}

adapter中有getView(int position, View convertView, ViewGroup parent)方法的代码

and there is the code of getView(int position, View convertView, ViewGroup parent) method in adapter

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="match_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.

使用 android:srcImageView.setImage* 方法没关系,关键可能是 adjustViewBounds.

It doesn't matter if you use android:src or ImageView.setImage* methods and the key is probably the adjustViewBounds.

这篇关于缩放图像以填充 ImageView 宽度并保持纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆