你可能还没遇到过这种特殊场景,但是如果你需要为图像的加载分配优先级,则可以使用.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中,我们只需要加载适当的图像到这些ImageView
上即可。你现在应该知道的是如何做出正确的Picasso请求。被加载到imageViewHero
上的图像拥有最高的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的.priority()
,而不是试图通过排列Picasso的调用来影响图像加载的优先级。