Android 为什么在毕加索的作品中,毕加索要求背景?

Android 为什么在毕加索的作品中,毕加索要求背景?,android,picasso,Android,Picasso,在毕加索的with(上下文)中 public static Picasso with(Context context) { if (singleton == null) { synchronized (Picasso.class) { if (singleton == null) { singleton = new Builder(context).build(); } } } return singleton; } 构建器(

在毕加索的
with(上下文)

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

为什么毕加索总是在设置context=context.getApplicationContext(),却还要请求上下文呢

您已经发布了您的答案-

public Builder(Context context) {
  if (context == null) {
    throw new IllegalArgumentException("Context must not be null.");
  }
  this.context = context.getApplicationContext();
}
毕加索是一个图书馆,而不是一个应用程序。在创建毕加索实例时,如果您不通过
上下文
,那么您认为它将如何从中获取
应用程序上下文

要使其正常工作,它需要
上下文
,并且它肯定需要由使用此库的应用程序提供。

在生成器的帮助下创建毕加索实例后,不需要传递上下文

    // 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);
有关更多信息,您可以在此处阅读:


通过切换到应用程序上下文,它还可以防止泄漏
活动(如果您通过了该活动)。非常感谢@JakeWharton的澄清!