适合 ReyclerView 项目中 imageView 的图像宽度

cuo*_*oka 0 android image imageview picasso android-recyclerview

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

回收者项目 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>
Run Code Online (Sandbox Code Playgroud)

这是我的自定义 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));
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道图像的长宽比,所以我可以在 onMeasure 方法中执行此操作来告诉 Android 我想要的高度:

setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredWidth() * 1.32f));
Run Code Online (Sandbox Code Playgroud)

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

提前致谢,

小智 6

认为您应该将adjustmentViewBounds属性添加到图像标签中

 <ImageView
              android:id="@+id/thumbview"
              android:layout_width="120dp"
              android:layout_height="120dp"
              android:layout_gravity="center"
              android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)