在ReyclerView项目中拟合imageView的图像宽度

时间:2016-01-25 15:03:42

标签: android image imageview android-recyclerview picasso

我目前正在使用此代码。我正在做的是使用Picasso下载图像并将其加载到imageView中,该图像位于RecyclerView项目内。

Recycler Item XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardPreventCornerOverlap="false"
    app:cardElevation="4dp"
    app:cardCornerRadius="4dp"
    app:cardUseCompatPadding="true">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Loading View -->
        <com.wang.avi.AVLoadingIndicatorView
            android:id="@+id/avloadingitem"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:visibility="gone"
            app:indicator="BallClipRotate"
            app:indicator_color="@color/colorAccent"/>

        <!-- Imagen -->
        <com.wallakoala.wallakoala.Views.ProductImageView
            android:id="@+id/grid_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:riv_corner_radius_bottom_left="4dp"
            app:riv_corner_radius_bottom_right="4dp"
            app:riv_corner_radius_top_left="4dp"
            app:riv_corner_radius_top_right="4dp"/>

        <!-- Pie de foto -->
        <RelativeLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:alpha="0.75">

            <!-- Info extra -->
            <include android:id="@+id/extraInfo"
                layout="@layout/product_footer_extra"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="visible"/>

            <!-- Info principal -->
            <include android:id="@+id/mainFooter"
                layout="@layout/product_footer"
                android:layout_height="@dimen/footer_height"
                android:layout_width="match_parent"
                android:layout_below="@id/extraInfo"/>

        </RelativeLayout>

    </FrameLayout>

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

这是我的自定义ImageView。

public class ProductImageView extends RoundedImageView
{
    public ProductImageView(Context context)
    {
        super(context);
    }

    public ProductImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ProductImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredWidth() * 1.32f));
    }
}

我知道图像的宽高比,所以我可以用onMeasure方法来告诉Android我想要的高度:

setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredWidth() * 1.32f));

所以现在它与具有该宽高比的图像完美配合,但是,我想摆脱这个,因为我想下载具有不同宽高比的图像。那么,我怎样才能告诉Android适应宽度并调整imageView的高度以保持宽高比?

提前致谢,

4 个答案:

答案 0 :(得分:3)

认为您应该将 adjustViewBounds 属性添加到图片代码

 <ImageView
              android:id="@+id/thumbview"
              android:layout_width="120dp"
              android:layout_height="120dp"
              android:layout_gravity="center"
              android:adjustViewBounds="true"/>

答案 1 :(得分:2)

xml中的ImageView应该具有高度wrap_content,就像这样..

<ImageView
            android:id="@+id/grid_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"/>

然后,你应该使用picasso lib的post()方法从服务器加载图像。

imageView.post(new Runnable() {
                                @Override
                                public void run() {
                                    Picasso.with(mActivity).load(path).resize(imageView.getWidth(), 0).into(imageView);
                                }
                            }); break;

答案 2 :(得分:1)

有一个名为&#39;&#39; Glide&#39;的库。您只需将图像加载到imageview并调用.centercrop即可 例如:Glide.with(yourFragment)     .load(yourUrl)     .centerCrop()     .into(yourView);

字体&GT; https://github.com/bumptech/glide/wiki/Transformations

答案 3 :(得分:1)

您所看到的是:.fit().centerCrop().fit().centerInside()

Picasso
  .with(context)
  .load(imageUrl)
  .fit()
  .centerCrop()
  // or .centerInside()
  .into(myImageView)
  • 适合:等到可以测量ImageView的大小,并调整图像大小以适合ImageView
  • centerInside :图片将完全显示,但可能不会 填写整个ImageView(Mantains纵横比)
  • centerCrop :缩放图片以使其填充,裁剪额外的