gpt4 book ai didi

Android 在 ListView 中使用 Picasso 和 TextDrawable

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:57 24 4
gpt4 key购买 nike

我有一个用户图像的ListView。有些用户有头像,我用Picasso加载图像。
如果用户没有图像,使用 TextDrawable我在我的 ListView 适配器中设置了他的图像,如下所示。

if (picture!=null) {
Uri imageUri = Uri.parse(picture);
Picasso.with(holder.pic.getContext()).load(imageUri.toString()).fit().centerCrop().into(holder.pic);

} else {
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(userName);
String userChar = userName.substring(0, 1);
TextDrawable drawable = TextDrawable.builder()
.buildRound(userChar.toUpperCase(), color);
holder.pic.setImageDrawable(drawable);

}

问题是当我滚动列表时,有时图像会乱七八糟,那些没有图像并且应该用 TextDrawable 显示的图像会与另一个用户的个人资料图片一起显示。
如果我使用 Picasso 加载所有图像,则不会发生这种情况。无论如何将 TextDrawable 对象传递给 picasso 来解决这个问题?或者还有其他解决办法吗?

最佳答案

您可以使用 TextDrawable 作为 Picasso 的可绘制占位符。那么我想你的问题就解决了。

ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
int color = generator.getColor(userName);
String userChar = userName.substring(0, 1);
TextDrawable drawable = TextDrawable.builder()
.buildRound(userChar.toUpperCase(), color);
Picasso.with(holder.pic.getContext()).
load(imageUri.toString()).
placeholder(drawable).
error(drawable).
centerCrop().
into(holder.pic);

关于Android 在 ListView 中使用 Picasso 和 TextDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com