2016-05-02 11 views
0

私はピカソを使用してイメージを取得し、トランスフォーメーションでそれらを実行しています。 奇妙なことは、変換オブジェクトは各画像に対して作成されますが、メソッド変換は一度だけ呼び出されることです。ピカソは一度だけトランスフォームを呼び出します

次のオブジェクト(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()"; 
} 

そして「ターゲット」

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); 
    } 
} 

が、変換メソッドは1度だけ呼び出されます。

答えて

1

Picassoは、そのメモリ内のキャッシュからイメージをロードしています。 これはすでにキャッシュされた結果ですぐにTargetに戻ります。Transformationtransformに電話する必要はありません。

Tranformationでキャッシュキーを変更する場合は、実装をkey()に変更するだけです。

また、すべてのTargetにequalsおよびhashcodeコントラクトを実装する必要があります。

関連する問題