优先级:高优先级、中优先级、低优先级
你可能正在想一个符合这个的情景,但是如果你想需要按照优先级来加载图片,你可以使用priority()
这个方法。这需要使用三个常量:HIGH
,MEDIUM
,LOW
.默认的,所有的请求都使用MEDIUM
。赋予图片不同的加载优先级将会影响到Picasso的加载行为。
例子
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/activity_request_priority_hero"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Sweets"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Lorem Ipsum is simply dummy text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/activity_request_priority_low_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ImageView
android:id="@+id/activity_request_priority_low_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
这个xml的代码并不是很重要。你只需要知道他是下面的布局即可。
例子:activity的代码
在我们的activity中,我们只需要加载需要展示在三个ImageView上面的图片。你应该知道如何使用Picasso发出正确的请求。下面这张图片将得到HIGH
的优先级。
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.fit()
.priority(Picasso.Priority.HIGH)
.into(imageViewHero);
下面的两张小图片将是LOW
的优先级。
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[1])
.fit()
.priority(Picasso.Priority.LOW)
.into(imageViewLowPrioLeft);
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[2])
.fit()
.priority(Picasso.Priority.LOW)
.into(imageViewLowPrioRight);
很重要的一点是,你自己指定的Picasso的请求并不是有效的。你应该使用Picasso Priorities
来改变Picasso的请求而不是改变自己请求的顺序。