5

我有一个ImageView我以编程方式创建的,并使用毕加索加载图像。ImageView 的高度和宽度设置为WRAP_CONTENT. 图像很大,所以我想使用毕加索的.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

我能找到的唯一信息是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()

4

2 回答 2

7

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

            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"
                />

希望这可以帮助!:)

于 2016-09-22T02:23:06.127 回答
0

利用

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

或者

Picasso.with(getContext()).load(path).fit().centerCrop().into(iv);
于 2017-01-16T19:40:56.667 回答