我正在使用Picasso获取图像并通过转换运行它们。 奇怪的是,虽然为每个图像创建了Transformation对象,但方法转换只被调用一次。
我创建了许多以下对象(使用Gson解析),然后调用" postDeserialise"在他们身上。
@Hide
transient public ObservableField<Drawable> badgeImage;
@Hide
transient private ImageUtils.ImageTarget bindableFieldTarget;
public void postDeserialise(Context context, String src){
if (imageURL != null) {
badgeImage = new ObservableField<Drawable>();
bindableFieldTarget = new ImageUtils.ImageTarget(badgeImage, context.getResources());
if (progress.compareTo(BigDecimal.ONE)==0) {
Picasso.with(context)
.load(imageURL)
.into(bindableFieldTarget);
}
else {
GrayscaleTransformation transform = new GrayscaleTransformation(Picasso.with(context), progress);
Picasso.with(context)
.load(imageURL)
.transform(transform)
.into(bindableFieldTarget);
}
}
}
以下是GrayscaleTransformation类:
private static final String TAG = "GrayscaleTransformation";
private final Picasso picasso;
private BigDecimal percentageToTransform;
public GrayscaleTransformation(Picasso picasso, BigDecimal percentageToTransform) {
this.picasso = picasso;
this.percentageToTransform = percentageToTransform;
Log.d(TAG, "New GrayscaleTransformation percentageToTransform = " + percentageToTransform);
}
@Override public Bitmap transform(Bitmap source) {
Log.d(TAG, "transform ....");
Bitmap result = createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
Bitmap noise;
try {
noise = picasso.load(R.drawable.noise).get();
} catch (IOException e) {
throw new RuntimeException("Failed to apply transformation! Missing resource.");
}
BitmapShader shader = new BitmapShader(noise, REPEAT, REPEAT);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(result);
int width = canvas.getWidth();
int clipPoint = percentageToTransform.multiply(new BigDecimal(width)).intValue();
Log.d(TAG, "width/clipPoint = " + width + "/" + clipPoint);
canvas.clipRect(clipPoint, 0, canvas.getWidth(), canvas.getHeight(), Region.Op.REPLACE);
canvas.drawBitmap(source, 0, 0, paint);
paint.setColorFilter(null);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
canvas.drawRect(0, 0, width, canvas.getHeight(), paint);
canvas.clipRect(0, 0, clipPoint, canvas.getHeight(), Region.Op.REPLACE);
canvas.drawBitmap(source, 0, 0, new Paint());
source.recycle();
noise.recycle();
return result;
}
@Override public String key() {
return "grayscaleTransformation()";
}
&#34;目标&#34;
public static class ImageTarget implements Target {
private ObservableField<Drawable> observableField;
private Resources resources;
public ImageTarget(ObservableField<Drawable> observableField, Resources resources) {
this.observableField = observableField;
this.resources = resources;
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
observableField.set(new BitmapDrawable(resources, bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
observableField.set(errorDrawable);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
observableField.set(placeHolderDrawable);
}
}
每次我在日志中看到转换类时都会创建转换类,但转换方法只会被调用一次。
答案 0 :(得分:1)
Picasso
正在从其内存缓存中加载图像。
这将立即使用已缓存的结果返回Target
,无需在transform
上调用Transformation
。
如果您希望Tranformation
更改缓存密钥,只需更改key()
的实施。
另请注意,您应该在所有Target
上实现equals和hashcode合约。