2016-04-14 46 views
3

Picasso.with(context) ..爲什麼在畢加索(背景)中,畢加索要求上下文?

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

和建造(上下文的背景下),這樣

/** 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(); 
} 

爲什麼畢加索甚至要求的上下文時,它總是setting context = context.getApplicationContext()

回答

3

您已經張貼你的答案 -

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

畢加索是一個庫,而不是一個應用程序。在創建畢加索實例時,如果您不通過context,那麼您如何看待application context? 對於它的工作需要context,它肯定需要由使用這個庫的應用程序提供。

+6

它還可以防止通過切換到應用程序上下文來泄漏「活動」(如果這就是你所傳遞的)。 –

+0

非常感謝@JakeWharton! –

2

你不需要與建設者

// create Picasso.Builder object 
    Picasso.Builder picassoBuilder = new Picasso.Builder(context); 

    // Picasso.Builder creates the Picasso object to do the actual requests 
    Picasso picasso = picassoBuilder.build(); 

    // instead of Picasso.with(Context context) you directly use this new custom Picasso object 

picasso 
    .load(UsageExampleListViewAdapter.eatFoodyImages[0]) 
    .into(imageView1); 

的幫助欲瞭解更多信息,你可以閱讀更多關於它在這裏創建畢加索實例後,通過上下文: -

https://futurestud.io/blog/picasso-customizing-picasso-with-picasso-builder