我有GridView英镑.GridView的数据是来自服务器的请求.

以下是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

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 position, View convertView, ViewGroup parent)方法的代码

Bitmap bitmap = BitmapUtil.loadBitmapFromHttpUrl(product.getHttpUrl());
prodImg.setImageBitmap(bitmap);

图像大小为210*210.我在Nexus4上运行我的应用程序.图像的宽度为ImageView,但高度为ImageView时不zoom .ImageView没有显示整个图像.

我怎么解决这个问题呢?

推荐答案

不使用任何自定义类或库:

<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的固有宽度大于父宽度,则
    将使其尽可能宽,并向下zoom 保持纵横比.

不管你使用android:srcImageView.setImage*种方法,关键可能是adjustViewBounds.

Android相关问答推荐

以正确的方式从房间收集流量

滑动以更改合成中的活动

Jetpack Compose Scaffold—content不在TopAppBar下面开始'

有没有办法知道每台安卓设备上的通知限制?

list 合并失败,AGP 8.3.0

约束布局:垂直链中的视图应将内容包裹到空间的1/3

使用Kotlin/Compose与Java/XML指南的比较

如何更新Kotlin中的显示?

在以XML格式设置完整屏幕视图时可见App Compat按钮

Jetpack Compose 使用 SavedStateHandle 发送返回结果不适用于 ViewModel 中注入的 SavedStateHandle

Android 访问任意公共目录

我可以从 Android 中的选定文本中获取周围的文本吗?

Jetpack Compose 绘制范围内的动画

任务 ':app:kaptGenerateStubsDebugKotlin' 执行失败

如何在 React Native 下载文件之前打开文件管理器并 Select 一个目录

如何使用 Jetpack Compose 在图像上叠加文本

如何只允许拖动 BottomSheetScaffold 中 BottomContent 的 SheetPeek 的一部分?

如何使用底页,启用和展开父交互

Kotlin:如何在另一个变量的名称中插入一个变量的值

在 Compose 中使用 DeepLink 会导致无法向后导航