2015-01-08 5 views
5

それは大丈夫のようなすべてのimage.For例えば何かロードするためのピカソの新しいinstaceを作成することです:それは作成されませlistAdaptor .DoesのgetView()はピカソ毎回の新しいインスタンスを作成し、それは大丈夫です

Picasso.with(context) 
    .load(url) 
    .placeholder(R.drawable.placeholder) 
    .error(R.drawable.error) 
    .centerInside(
    .tag(context) 
    .into(holder.image); 

を新しいLruCacheが毎回OOMにつながります。また

ContextActivity Contextすることができピカソに渡すことができます。

/** Start building a new {@link Picasso} instance. */ 
public Builder(Context context) { 
    if (context == null) { 
    throw new IllegalArgumentException("Context must not be null."); 
    } 
    this.context = context.getApplicationContext(); 
} 

答えて

11

ピカソはシングルトンになるように設計されているため、毎回新しいインスタンスが作成されることはありません。

これはwith()方法である:singletonがある場合にのみ作成されますが、ピカソは独自のインスタンスを管理している特定のインスタンスにwith()を呼び出すことはありませんので、それは静的メソッドであることを

public static Picasso with(Context context) { 
    if (singleton == null) { 
     synchronized (Picasso.class) { 
     if (singleton == null) { 
      singleton = new Builder(context).build(); 
     } 
     } 
    } 
    return singleton; 
} 

注意、 null

Buildersingle, global Application object of the current processあるのApplicationContextを使用するためのコンテキストとしてActivityを渡すことに問題はありません:

キャッシュ用として
public Builder(Context context) { 
     if (context == null) { 
     throw new IllegalArgumentException("Context must not be null."); 
     } 
     this.context = context.getApplicationContext(); 
} 

、それが保持されているので、同じものは、毎回の使用でありますシングルトン:

public Picasso build() { 
     // code removed for clarity 

     if (cache == null) { 
     cache = new LruCache(context); 
     } 
     // code removed for clarity 

     return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats, 
      defaultBitmapConfig, indicatorsEnabled, loggingEnabled); 
} 
+0

「現在のプロセスのグローバルな単一のアプリケーションオブジェクト」を強調したいと思います。これは、あなたのアプリケーションが実行されているプロセスごとに1つのPicassoのインスタンスを持つ必要があることを意味します。 – Sebastiano

1
Its not problem..You are not creating Object 

PicassoはすでにSingleTonオブジェクトここ

です源泉コードをピカソクラス

public static Picasso with(Context context) { 
    if (singleton == null) { 
     synchronized (Picasso.class) { 
      if (singleton == null) { 
       singleton = new Builder(context).build(); 
      } 
     } 
    } 
    return singleton; 
} 

詳細のソースコードPicasso Source code

1

Kalyanが正しいです! Picassoはすでにシングルトンなので、読み込んだすべての画像に同じインスタンスが使用されます。また、それはあなたがその建築業者を必要としないことを意味します。 "Picasso.with(context).load(url).into(holder.image);"あなたが望む設定で、それはすべてです。

関連する問題