前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【译】根据图像优先级排序请求队列

【译】根据图像优先级排序请求队列

作者头像
小鄧子
发布2018-08-20 15:28:58
4430
发布2018-08-20 15:28:58
举报

优先级:高,中,低

你可能还没遇到过这种特殊场景,但是如果你需要为图像的加载分配优先级,则可以使用.priority()。这个方法涉及到三个代表优先级的常量,HIGHMEDIUMLOW。默认情况下,所有的请求等级为MEDIUM。分配不同的优先级将直接影响到Picasso的加载行为。

示例:XML布局

为了更清楚演示,让我们看一个实际例子:

代码语言:javascript
复制
<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请求。被加载到imageViewHero上的图像拥有最高的HIGH优先级:

代码语言:javascript
复制
Picasso
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .fit()
    .priority(Picasso.Priority.HIGH)
    .into(imageViewHero);

其余两张小图,被分配了最低的LOW优先级:

代码语言:javascript
复制
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的调用来影响图像加载的优先级。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.06.20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 优先级:高,中,低
    • 示例:XML布局
      • 示例:Activity代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档