【问题标题】:Picasso .fit().centerCrop() not working with WRAP_CONTENTPicasso .fit().centerCrop() 不适用于 WRAP_CONTENT
【发布时间】:2023-03-30 15:37:01
【问题描述】:

我有一个ImageView,我以编程方式创建,并使用毕加索加载图像。 ImageView 的高度和宽度设置为WRAP_CONTENT。图片很大,所以我想使用 Picasso 的.fit().centerCrop() 方法来节省内存。但是,当我添加方法时,图像没有显示出来,我认为这一定是因为WRAP_CONTENT。这是我的代码:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(centerParams);
Picasso.with(context)load(path).fit().centerCrop().into(iv); //if I remove .fit().centerCrop(), the images load just fine

我能找到的唯一信息是this issue on GitHub 关于它——虽然这个问题说它在 v2.4 中已关闭,但有些人评论说他们在 v2.5 中遇到了它。 (我也在用v2.5)

编辑:根据安吉拉的建议,这是我现在使用的:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(350));
centerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
iv.setLayoutParams(centerParams);
iv.setAdjustViewBounds(true);                                    
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getContext()).load(path).fit().into(iv);

图像现在显示出来了,但它们没有正确缩放,纵横比以某种方式发生了变化。关于如何保持纵横比同时仍允许毕加索获得.fit() 的测量值的任何想法?

【问题讨论】:

标签: android android-imageview picasso android-image android-layoutparams


【解决方案1】:

我也遇到过这个问题。这就是我所做的......

            Picasso
                    .with(context)
                    .load(path)
                    .fit()
                    .into(iv);

在 xml 文件中尝试包含 scaleType、adjustViewBounds 和 layout_gravity 例如:

<ImageView
                android:id="@+id/img_movie"
                android:layout_width="match_parent"
                android:layout_height="350dp"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

希望这会有所帮助! :)

【讨论】:

  • 谢谢! :) 图像现在加载,但我无法正确缩放它们,纵横比不断变化——更新了上面的代码。
【解决方案2】:

使用

Picasso.with(getContext()).load(path).fit().centerInside().into(iv);

Picasso.with(getContext()).load(path).fit().centerCrop().into(iv);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2018-06-13
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    相关资源
    最近更新 更多