我有一个带有适配器的 listView,其中包含可变大小(宽度和高度)的 ImageView
。我需要将 Picasso 加载的图片调整为布局的最大宽度和图片纵横比给出的可变高度。
我已经检查了这个问题: Resize image to full width and fixed height with Picasso
fit()
有效,但我没有找到任何东西可以保持图片的纵横比。
如果我固定适配器布局中的高度,此代码部分有效:
Picasso.with(this.context).load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.fit().centerInside()
.into(holder.message_picture);
但它会在 listView 的图片之间产生空白,因为图片可能没有那个高度。
提前致谢。
最佳答案
从 picasso 2.4.0 开始,this operation is now directly supported .只需添加一个 .resize()
请求,其中一个维度为 0
。例如,要具有可变宽度,您的调用将变为:
Picasso.with(this.context)
.load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.resize(0, holder.message_picture.getHeight()),
.into(holder.message_picture);
请注意,此调用使用 .getHeight()
,因此假定 message_picture
已被测量。如果不是这种情况,例如当您在 ListAdapter
中扩展了新 View 时,您可以通过向 View 添加 OnGlobalLayoutListener
来延迟此调用直到测量之后:
holder.message_picture.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
// Wait until layout to call Picasso
@Override
public void onGlobalLayout() {
// Ensure we call this only once
imageView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
Picasso.with(this.context)
.load(message_pic_url)
.placeholder(R.drawable.profile_wall_picture)
.resize(0, holder.message_picture.getHeight())
.into(holder.message_picture);
}
});
https://stackoverflow.com/questions/21889735/
相关文章:
android - Android PopupWindow Activity 时背景模糊或变暗
android - Kotlin 错误 : Could not find org. jetbrain
android - 如何使用 Android Studio 查看共享首选项文件?
android - 通过代码将 TextView 的宽度设置为 wrap_content
android - 什么是 CSS 的 dp(密度独立像素)单位?
android - 何时清除 Android 中的缓存目录?
android - NoSuchFieldError : No static field listV