我使用毕加索:
Picasso.with(mContext)
.load(url)
.placeholder(fallback)
.error(fallback)
.transform(new CircleTransform(userStatus))
.into(this);
转换以将笔画和圆度应用于图像:
public class CircleTransform implements Transformation {
private int userStatus = UserProfile.STATUS_TYPE_NONE;
public CircleTransform(int userStatus) {
this.userStatus = userStatus;
}
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
if (UserProfile.STATUS_TYPE_NONE != userStatus) {
Paint paintStroke = new Paint();
paintStroke.setAntiAlias(true);
if (UserProfile.STATUS_TYPE_BRONZE == userStatus)
paintStroke.setColor(Colors.bronzeColor);
else if (UserProfile.STATUS_TYPE_SILVER == userStatus)
paintStroke.setColor(Colors.silverColor);
else if (UserProfile.STATUS_TYPE_GOLD == userStatus)
paintStroke.setColor(Colors.goldColor);
else if (UserProfile.STATUS_TYPE_PLATINUM == userStatus)
paintStroke.setColor(Colors.platinumColor);
else if (UserProfile.STATUS_TYPE_DIAMOND == userStatus)
paintStroke.setColor(Colors.diamondColor);
paintStroke.setStyle(Paint.Style.STROKE);
float stroke = size * CIRCLE_SIZE_IN_PERCENT;
paintStroke.setStrokeWidth(stroke);
canvas.drawCircle(r, r, r - (stroke / 2), paintStroke);
}
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle";
}
}
问题是每次加载图像时都不会调用公共Bitmap transform(Bitmap source)
。
答案 0 :(得分:1)
userStatus
变量会更改转换的行为,因此必须包含在缓存键中。毕竟,毕加索认为任何圆形变换都可以被另一个变换。
@Override
public String key() {
return "circle" + userStatus;
}
答案 1 :(得分:-2)
好的我找到了解决方案,转换键保持不变,并且它不会应用具有相同键的新键,所以我改变了
@Override
public String key() {
return "circle";
}
到:
@Override
public String key() {
return "circle" + value;
}
value - 应该是定义ImageView实例的唯一参数。我希望它可以帮助别人:)