【问题标题】:android listview with image item resize image height proportionaly带有图像项的android listview按比例调整图像高度
【发布时间】:2017-09-06 17:10:18
【问题描述】:

我有一个包含此图像项的列表视图:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/llItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/white"

android:layoutDirection="rtl"
android:orientation="horizontal">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="5dp"
    android:background="@color/white"
    app:cardElevation="4dp"
    app:cardUseCompatPadding="true">

    <ImageView
        android:id="@+id/pollimg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"


        />
</android.support.v7.widget.CardView>

我需要将 getview 方法中的图像视图加载到数组适配器中,如下所示:

                 @Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
              .... 
                etc
              .....
        try {
            Picasso.with(context)
                    .load(allpolls.get(position).getMediaUrl()).fit()
                    .placeholder(R.mipmap.icon).into(holder.img);
        } catch (Exception e) {
            holder.img.setVisibility(View.GONE);
        }
         }

我的问题是所有行的图像视图高度都是固定的,我需要将图像视图高度设置为与在线图像相同的图像高度。所以它可以取它的确切高度

【问题讨论】:

  • 发布您的完整布局...代码
  • 问题已更新,请检查

标签: android listview picasso


【解决方案1】:

来自this教程

fit() 正在测量目标 ImageView 的尺寸,并在内部使用 resize() 将图像尺寸减小到 ImageView 的尺寸。关于 fit() 有两件事需要了解。首先,调用 fit() 会延迟图像请求,因为 Picasso 需要等到可以测量 ImageView 的大小。其次,您只能将 fit() 与 ImageView 作为目标。

这意味着,如果您使用fit(),图像将被调整大小。删除它。

更多信息,如果你想保持图像的原始纵横比,你应该考虑将高度和宽度都设置为wrap_content

【讨论】:

  • 就是这样!我也将此添加到 imageview 以正常工作android:adjustViewBounds="true"
  • 很高兴为您提供帮助。这也保留了图像的宽高比
猜你喜欢
  • 2011-09-20
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 2013-03-17
  • 2017-09-04
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多