2015-01-08 8 views
5

Czy mogę utworzyć nowy instace Picassa do ładowania wszelkich image.For Np coś takiego:Czy to w porządku, aby utworzyć nową instancję Picassa za każdym

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

w getView() Urządzony listAdaptor .Does nie tworzą nowy LruCache za każdym razem, co ostatecznie doprowadzi do OOM.

Również Czy Context przekazane Picasso może być Activity 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(); 
} 

Odpowiedz

11

Picasso został zaprojektowany jako singleton, więc za każdym razem nie powstaje nowa instancja.

Jest to metoda with():

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

Należy pamiętać, że jest to metoda statyczna, więc nie zadzwonić with() w konkretnym przypadku, Picasso zarządza własną instancję, która tworzona jest tylko wtedy, gdy jest singletonnull.

Nie ma problemu z przepuszczanie Activity jako kontekst, ponieważ Builder użyje ApplicationContext który jest single, global Application object of the current process:

public Builder(Context context) { 
     if (context == null) { 
     throw new IllegalArgumentException("Context must not be null."); 
     } 
     this.context = context.getApplicationContext(); 
} 

Co do pamięci podręcznej, tak samo jeden jest używać za każdym razem, ponieważ jest ona zachowana przez singleton:

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

Chcę podkreślić "pojedynczy, globalny obiekt aplikacji bieżącego procesu". Oznacza to, że musisz mieć jedno wystąpienie Picassa dla każdego z procesów, na których działa twoja aplikacja. – Sebastiano

1
Its not problem..You are not creating Object 

Picasso jest już SingleTon Object

Oto kod sosie Picasso Klasa

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

Więcej Kod źródłowy na Picasso Source code

1

Kalyan ma rację! Picasso jest już singletonem, więc używa tej samej instancji dla wszystkich ładowanych obrazów. Oznacza to również, że nie będziesz potrzebować tego budowniczego. po prostu zadzwoń: "Picasso.with (context) .load (url) .into (właściciel.image);" z ustawieniami, które chcesz i to wszystko.

Powiązane problemy