为什么在Picasso.with(上下文)中,毕加索要求上下文?

时间:2016-04-14 17:29:42

标签: android picasso

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

和Builder(Context context)一样

setting context = context.getApplicationContext( )

为什么毕加索总是要求上下文 <android.support.design.widget.TextInputLayout android:id="@+id/ti_keyword_error" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/defaultLayoutPadding" android:layout_marginRight="@dimen/defaultLayoutPadding" android:layout_marginTop="@dimen/adv_top_padding"> <android.support.v7.widget.AppCompatEditText android:id="@+id/et_keyword_search" style="@style/match_wrap.white_hint.no_focusable" android:textColorHint="@color/color_white" android:hint="@string/m_adv_keyskills" /> </android.support.design.widget.TextInputLayout>

2 个答案:

答案 0 :(得分:3)

您已经发布了答案 -

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

Picasso是一个图书馆,而不是一个应用程序。在创建Picasso实例的同时,如果您没有通过context,那么您认为它将如何从application context获得?  要使它工作,它需要context,它肯定需要由使用此库的应用程序提供。

答案 1 :(得分:3)

在构建器

的帮助下创建picasso实例后,您无需传递上下文
    // 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