【发布时间】: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() 的测量值的任何想法?
【问题讨论】:
-
不要使用毕加索的fit()方法stackoverflow.com/questions/25114045/…
标签: android android-imageview picasso android-image android-layoutparams