Glide 是不是对每个图像请求进行排队?滚动时Recyclerview加载非常慢
Posted
技术标签:
【中文标题】Glide 是不是对每个图像请求进行排队?滚动时Recyclerview加载非常慢【英文标题】:Does Glide queue up every image request? Recyclerview loads are very slow when scrollingGlide 是否对每个图像请求进行排队?滚动时Recyclerview加载非常慢 【发布时间】:2015-08-11 06:37:27 【问题描述】:我发现我们的应用程序中使用 Glide 3.6 + 使用 GridLayoutManager 的 RecyclerView 加载图像的时间很长。图像都在 20kb-40kb 左右。似乎 glide 将所有请求排队,一旦您开始向下滚动列表,就会导致图像需要很长时间才能加载。我已经附加了 GenericRequest 类的日志。
V/GenericRequest﹕ finished setup for calling load in 0.170291 this: 249721749
V/GenericRequest﹕ finished onSizeReady in 0.36724999999999997 this: 249721749
V/GenericRequest﹕ finished run method in 0.763083 this: 249721749
V/GenericRequest﹕ Got onSizeReady in 0.041249999999999995 this: 991315424
V/GenericRequest﹕ finished setup for calling load in 0.15479199999999999 this: 991315424
V/GenericRequest﹕ finished onSizeReady in 0.30008399999999996 this: 991315424
V/GenericRequest﹕ finished run method in 0.499959 this: 991315424
V/GenericRequest﹕ Got onSizeReady in 0.060584 this: 1029510845
V/GenericRequest﹕ finished setup for calling load in 0.200625 this: 1029510845
V/GenericRequest﹕ finished onSizeReady in 0.39233399999999996 this: 1029510845
V/GenericRequest﹕ finished run method in 0.670084 this: 1029510845
V/GenericRequest﹕ Got onSizeReady in 0.048374999999999994 this: 516458536
V/GenericRequest﹕ finished setup for calling load in 0.188666 this: 516458536
V/GenericRequest﹕ finished onSizeReady in 0.379916 this: 516458536
V/GenericRequest﹕ finished run method in 0.654083 this: 516458536
V/GenericRequest﹕ Resource ready in 1012.49325 size: 2.18359375 fromCache: false this: 671303046
V/GenericRequest﹕ Got onSizeReady in 0.073833 this: 356666200
V/GenericRequest﹕ finished setup for calling load in 0.240667 this: 356666200
V/GenericRequest﹕ finished onSizeReady in 0.460792 this: 356666200
V/GenericRequest﹕ finished run method in 0.780708 this: 356666200
V/GenericRequest﹕ Got onSizeReady in 0.064583 this: 347417463
V/GenericRequest﹕ finished setup for calling load in 0.242583 this: 347417463
V/GenericRequest﹕ finished onSizeReady in 0.45758299999999996 this: 347417463
V/GenericRequest﹕ finished run method in 0.7521249999999999 this: 347417463
V/GenericRequest﹕ Got onSizeReady in 0.142833 this: 671303046
V/GenericRequest﹕ finished setup for calling load in 0.38966599999999996 this: 671303046
V/GenericRequest﹕ finished onSizeReady in 0.703708 this: 671303046
V/GenericRequest﹕ finished run method in 1.851125 this: 671303046
V/GenericRequest﹕ Got onSizeReady in 0.056957999999999995 this: 634418527
V/GenericRequest﹕ finished setup for calling load in 0.21787499999999999 this: 634418527
V/GenericRequest﹕ finished onSizeReady in 0.443083 this: 634418527
V/GenericRequest﹕ finished run method in 0.7567499999999999 this: 634418527
V/GenericRequest﹕ Resource ready in 3443.041335 size: 2.18359375 fromCache: false this: 249721749
V/GenericRequest﹕ Resource ready in 4110.604794 size: 3.8623809814453125 fromCache: false this: 1054166306
V/GenericRequest﹕ Resource ready in 3824.033544 size: 3.8623809814453125 fromCache: false this: 991315424
V/GenericRequest﹕ Resource ready in 3773.0562109999996 size: 3.8623809814453125 fromCache: false this: 1029510845
V/GenericRequest﹕ Resource ready in 4542.90796 size: 2.18359375 fromCache: false this: 516458536
V/GenericRequest﹕ Resource ready in 4171.866168 size: 3.8623809814453125 fromCache: false this: 634418527
V/GenericRequest﹕ Resource ready in 4474.170752 size: 3.8623809814453125 fromCache: false this: 356666200
V/GenericRequest﹕ Resource ready in 4496.104085 size: 2.18359375 fromCache: false this: 671303046
V/GenericRequest﹕ Resource ready in 4814.625126999999 size: 2.18359375 fromCache: false this: 347417463
想知道是否有人可以提供一些关于问题可能是什么的见解。也许是调整视图大小所需的时间?我使用match_parent
作为视图的宽度和大约200dp
的固定高度。
图片加载使用
Glide.with(fragmentContext)
.load(imageUrl)
.centerCrop()
.placeholder(R.drawable.placeholder)
.into(viewHolder.productImage);
XML
<ImageView
android:id="@+id/product_image"
android:layout_
android:layout_ (208dp)
android:layout_gravity="top"
android:background="@color/placeholder_bg"
/>
编辑:根据@VladimirMironov 的评论。我已经更新了增加的池并在回收视图时清除了挂起的负载。这似乎在快速滚动时有所帮助。在进行视图测量时,为视图提供覆盖或不使用“match_parent”是否会带来任何好处?仍然存在视图不会显示近 5 秒的情况。
【问题讨论】:
您可以尝试两件事来提高性能:1. 取消RecyclerView.Adapter.onViewRecycled
中的下载请求(通过Glide.clear
调用) 2. 通过recycler.getRecycledViewPool().setMaxRecycledViews(0, 2 * numColumns)
增加ViewHolder
s 池大小
@VladimirMironov 很有趣,不知道这些我会试一试
@VladimirMironov 我用一些额外的信息编辑了我的帖子
【参考方案1】:
添加以下行,您将看到改进
recyclerView.setHasFixedSize(true);
yourAdapter.setHasStableIds(true);//recycler adapter
覆盖 onViewRecycled() 并清除之前的 Glide 请求
Glide.with(imageHolder.preview.getContext()).clear(imageHolder.preview);
【讨论】:
以上是关于Glide 是不是对每个图像请求进行排队?滚动时Recyclerview加载非常慢的主要内容,如果未能解决你的问题,请参考以下文章
使用 Glide,是不是可以在实际解码并保存到位图之前获得输出图像分辨率?