【Android】画面卡顿优化列表流畅度四之Glide几个常用参数设置

本文讲述了在Android项目中,如何通过Glide库优化图片加载,特别是使用thumbnail功能和调整RequestOptions参数来提高RecyclerView的流畅度,解决因网络图片过重导致的渲染卡顿问题。

上一篇:
【Android】画面卡顿优化列表流畅度三之RecyclerView刷新机制notifyItemRangeInserted

好像是一年前快两年了,笔者解析过glide的源码,也是因为觉得自己熟悉一些,也就没太关注过项目里glide的具体使用对当前业务的影响;主要是自负,还有就是真没有碰到过这样的数据加载情况。暴露了经验还是不太足够
有兴趣的可以去瞅瞅,就是对源码的解释而已比较枯燥乏味。也是因为有了这个积累才能找到比较合适的参数比解决当前的问题:
传送门:Glide源码解析
在这里插入图片描述
优化之前的用法如下:

 					Glide.with(context)
 						.load(imgUrl)
                        .into(holder.imageview);

因为不是笔者自己写的这段加载逻辑,所以笔者也没改动,呃!搞开发的都知道,程序能运行就别动;再说笔者就是一个小虾米,又在一个还算那啥的体系里。多一事不如少一事,搞开发也要讲讲人情世故蛤!不过一年前负责这块的开发跳槽了,据说工资涨了一大截;也算是happy跳槽。

优化之后的图片加载渲染就顺畅很多了,再没有上下滑动过程中出现的那种一顿一顿的画面顿感了,解决了在滑动过程中因为网络图片过重导致的渲染卡顿问题

下面针对几个优化参数设置进行细化解析:

thumbnail(0~1.0f)

原文介绍如下:

/**
   * Loads a resource in an identical manner to this request except with the dimensions of the
   * target multiplied by the given size multiplier. If the thumbnail load completes before the full
   * size load, the thumbnail will be shown. If the thumbnail load completes after the full size
   * load, the thumbnail will not be shown.
   *
   * <p>Note - The thumbnail resource will be smaller than the size requested so the target (or
   * {@link ImageView}) must be able to scale the thumbnail appropriately. See
   * {@link android.widget.ImageView.ScaleType}.
   *
   * <p>Almost all options will be copied from the original load, including the {@link
   * com.bumptech.glide.load.model.ModelLoader}, {@link com.bumptech.glide.load.ResourceDecoder},
   * and {@link com.bumptech.glide.load.Transformation}s. However,
   * {@link com.bumptech.glide.request.RequestOptions#placeholder(int)} and
   * {@link com.bumptech.glide.request.RequestOptions#error(int)}, and
   * {@link #listener(RequestListener)} will only be used on the full size load and will not be
   * copied for the thumbnail load.
   *
   * <p>Recursive calls to thumbnail are supported.
   *
   * <p>Overrides any previous calls to this method, {@link #thumbnail(RequestBuilder[])},
   *  and {@link #thumbnail(RequestBuilder)}.
   *
   * @see #thumbnail(RequestBuilder)
   * @see #thumbnail(RequestBuilder[])
   *
   * @param sizeMultiplier The multiplier to apply to the {@link Target}'s dimensions when loading
   *                       the thumbnail.
   * @return This request builder.
   */
  @NonNull
  @CheckResult
  @SuppressWarnings("unchecked")
  public RequestBuilder<TranscodeType> thumbnail(float sizeMultiplier) {
   
   
    if (sizeMultiplier < 0f || sizeMultiplier > 1f) {
   
   
      throw new IllegalArgumentException("sizeMultiplier must be between 0 and 1");
    }
    this.thumbSizeMultiplier = sizeMultiplier;

    return this;
  }

GPT翻译如下:

/**

以与此请求相同的方式加载资源,但是目标的尺寸乘以给定的尺寸倍数。如果缩略图加载在完整尺寸加载之前完成,将显示缩略图。如果缩略图加载在完整尺寸加载之后完成,将不会显示缩略图。

<p>注意 - 缩略图资源将小于请求的尺寸,因此目标(或{@link ImageView})必须能够适当地缩放缩略图。请参阅{@link android.widget.ImageView.ScaleType}。
<p>几乎所有选项都将从原始加载复制,包括{@link com.bumptech.glide.load.model.ModelLoader}、{@link com.bumptech.glide.load.ResourceDecoder}和{@link com.bumptech.glide.load.Transformation}。但是,{@link com.bumptech.glide.request.RequestOptions#placeholder(int)}和{@link com.bumptech.glide.request.RequestOptions#error(int)},以及{@link #listener(RequestListener)} 仅在完整尺寸加载时使用,并且不会被复制到缩略图加载。
<p>支持对缩略图的递归调用。
<p>覆盖对此方法的先前调用,{@link #thumbnail(RequestBuilder[])}和{@link #thumbnail(RequestBuilder)}。
@see #thumbnail(RequestBuilder)

@see #thumbnail(RequestBuilder[])

@param sizeMultiplier 加载缩略图时要应用于{@link Target}尺寸的倍数。

@return 此请求构建器。 */ 

优化之前的,这个空白和缩略图没有关系蛤!
优化之前的效果没有图片加载就是空白的

优化之后的实际效果展示:
在这里插入图片描述
也是由于图片太重了,当这个方法加上的时候,上下滑动立马就顺畅了很多,笔者实际使用的参数值是:
在这里插入图片描述
0.0625f 这个参数值我自己都惊讶了!不要问这个参数怎么来的,问就是二分法
好,接着设置RequestOptions

RequestOptions

下面是优化后的使用

public static RequestOptions buildRequestOptions(Context context, int resId) 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lichong951

你的鼓励决定更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值