android - 如何在 Recyclerview 交错网格布局中使用 Picasso 加载图像之前设置 ImageView 的高度和宽度

标签 android android-recyclerview imageview picasso staggeredgridlayoutmanager

我希望在使用 Picasso 加载图像之前设置 ImageView 高度。试图阻止父 View 将其高度更改为图像。我正在获取图像的高度和宽度并将其设置在 Picasso 内部,然后按比例缩小。

在使用 Picasso 加载图像之前,如何根据“webformatHeight”和“webformatWidth”缩放 ImageView 以防止调整大小。

images changing after Picasso loads

数据

{
    ...
    "hits":[
        {
            "webformatHeight":426,
            "webformatWidth":640,
            "webformatURL":"https://pixabay.com/get/eb32b5062dfd013ed95c4518b74a4291ea77ead204b0144190f8c478a2ebb3_640.jpg",
            ...
        },
        ...
    ]
}

回收商实现

RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
mPixabayAdapter = new PixabayAdapter();
recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
recycler.setAdapter(mPixabayAdapter);

ViewHolder

class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView mImage;
    private TextView mTitle;

    private ViewHolder(View view) {
        super(view);
        mImage = view.findViewById(R.id.image);
        mTitle = view.findViewById(R.id.title);
    }

    void bindView() {
        ColorDrawable[] shotLoadingPlaceholders = new ColorDrawable[]{new ColorDrawable(ContextCompat.getColor(itemView.getContext(), R.color.background_light))};
        Hit hit = mHitList.get(getAdapterPosition());
        mTitle.setText(hit.getUser());
        Picasso
                .with(mImage.getContext())
                .load(hit.getWebformatURL())
                .placeholder(shotLoadingPlaceholders[0])
                .resize(hit.getImageWidth(), hit.getImageHeight())
                .priority(Picasso.Priority.HIGH)
                .onlyScaleDown()
                .into(mImage);
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="4dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/image"
        android:background="@color/background_light"
        android:fontFamily="sans-serif-medium"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="@color/white_light"
        android:textSize="13sp" />
</RelativeLayout>

最佳答案

通过 width/height 计算图像的纵横比,并将 ImageView 包裹在这样的东西中:

public class RelativeSizeLayout extends FrameLayout {
    private float aspectRatio = 1.77f;

    public RelativeSizeLayout(Context context) {
        this(context,null);
    }

    public RelativeSizeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width / aspectRatio), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setAspectRatio(float ratio) {
        aspectRatio = ratio;
    }
}

关于android - 如何在 Recyclerview 交错网格布局中使用 Picasso 加载图像之前设置 ImageView 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309081/

相关文章:

java - RecyclerView崩溃

java - 为什么在 launchBillingFlow 之后没有调用 onPurchasesUpdated

android - 自动水平滚动RecyclerView

android - 在 fragment 内滚动时出现 RecyclerView 错误

android - 在 Android 中支持背景颜色的圆角 ImageView?

android - 如何将ImageView对齐到底部,填充它的宽度和高度,并保持它的纵横比?

java - 如何获取Firebase用户数据

android - 如何将 Android FTrace 精度提高到微秒?

android - 如何在 recyclerview 中查找每个可见项目的百分比

android - 使 ImageView 适合 CardView 的宽度