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 को बढ़ावा मिलेगा।

इसके अलावा Context पिकासो को दिया गया Activity Context हो सकता है कर सकते हैं: Souce कोड

/** 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

पिकासो एक सिंगलटन डिज़ाइन किया गया है, इसलिए वहाँ एक नया हर बार बनाया उदाहरण है नहीं है।

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

नोट तो वह एक स्थिर तरीका है कि ताकि आप एक विशेष उदाहरण पर with() कॉल नहीं करते, पिकासो अपने स्वयं के उदाहरण है, जो केवल बनाई गई है प्रबंध कर रहा है, तो singleton है:

यह with() विधि है null

एक Activity संदर्भ के रूप में पारित करने के साथ कोई समस्या नहीं है, क्योंकि Builder ApplicationContext का उपयोग करेगा जो एक 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(); 
} 

कैश के रूप में, एक ही एक है, हर का उपयोग है, क्योंकि यह बनाए रखा है सिंगलटन द्वारा:

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

मैं 'एकल, वैश्विक आवेदन वर्तमान process' की वस्तु तनाव करना चाहते हैं। इसका मतलब है कि आपको लगता है कि अपने app पर चल रहा है प्रक्रियाओं में से हर एक के लिए पिकासो का एक उदाहरण है की आवश्यकता होगी। – 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

कल्याण सही है! पिकासो पहले से ही एक सिंगलटन है, इसलिए यह आपके द्वारा लोड की जाने वाली सभी छवियों के लिए एक ही उदाहरण का उपयोग करता है। इसके अलावा इसका मतलब है कि आपको उस निर्माता की आवश्यकता नहीं होगी। आप बस सिर्फ फोन: "Picasso.with (संदर्भ) .load (यूआरएल) .into (holder.image);" सेटिंग्स जो आप चाहते हैं और वह सब कुछ है।

संबंधित मुद्दे