android – Glide是否排队每个图像请求?滚动时,Recyclerview加载速度非常慢

前端之家收集整理的这篇文章主要介绍了android – Glide是否排队每个图像请求?滚动时,Recyclerview加载速度非常慢前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用GridLayoutManager使用Glide 3.6一个RecyclerView在我们的应用程序中看到了很长的图像加载时间.图像大约为20kb-40kb.似乎滑动将所有请求排队,导致图像在开始向下滚动列表后需要花费很长时间才能加载.我已经附加了GenericRequest类的日志.
  1. V/GenericRequest finished setup for calling load in 0.170291 this: 249721749
  2. V/GenericRequest finished onSizeReady in 0.36724999999999997 this: 249721749
  3. V/GenericRequest finished run method in 0.763083 this: 249721749
  4. V/GenericRequest Got onSizeReady in 0.041249999999999995 this: 991315424
  5. V/GenericRequest finished setup for calling load in 0.15479199999999999 this: 991315424
  6. V/GenericRequest finished onSizeReady in 0.30008399999999996 this: 991315424
  7. V/GenericRequest finished run method in 0.499959 this: 991315424
  8. V/GenericRequest Got onSizeReady in 0.060584 this: 1029510845
  9. V/GenericRequest finished setup for calling load in 0.200625 this: 1029510845
  10. V/GenericRequest finished onSizeReady in 0.39233399999999996 this: 1029510845
  11. V/GenericRequest finished run method in 0.670084 this: 1029510845
  12. V/GenericRequest Got onSizeReady in 0.048374999999999994 this: 516458536
  13. V/GenericRequest finished setup for calling load in 0.188666 this: 516458536
  14. V/GenericRequest finished onSizeReady in 0.379916 this: 516458536
  15. V/GenericRequest finished run method in 0.654083 this: 516458536
  16. V/GenericRequest Resource ready in 1012.49325 size: 2.18359375 fromCache: false this: 671303046
  17. V/GenericRequest Got onSizeReady in 0.073833 this: 356666200
  18. V/GenericRequest finished setup for calling load in 0.240667 this: 356666200
  19. V/GenericRequest finished onSizeReady in 0.460792 this: 356666200
  20. V/GenericRequest finished run method in 0.780708 this: 356666200
  21. V/GenericRequest Got onSizeReady in 0.064583 this: 347417463
  22. V/GenericRequest finished setup for calling load in 0.242583 this: 347417463
  23. V/GenericRequest finished onSizeReady in 0.45758299999999996 this: 347417463
  24. V/GenericRequest finished run method in 0.7521249999999999 this: 347417463
  25. V/GenericRequest Got onSizeReady in 0.142833 this: 671303046
  26. V/GenericRequest finished setup for calling load in 0.38966599999999996 this: 671303046
  27. V/GenericRequest finished onSizeReady in 0.703708 this: 671303046
  28. V/GenericRequest finished run method in 1.851125 this: 671303046
  29. V/GenericRequest Got onSizeReady in 0.056957999999999995 this: 634418527
  30. V/GenericRequest finished setup for calling load in 0.21787499999999999 this: 634418527
  31. V/GenericRequest finished onSizeReady in 0.443083 this: 634418527
  32. V/GenericRequest finished run method in 0.7567499999999999 this: 634418527
  33. V/GenericRequest Resource ready in 3443.041335 size: 2.18359375 fromCache: false this: 249721749
  34. V/GenericRequest Resource ready in 4110.604794 size: 3.8623809814453125 fromCache: false this: 1054166306
  35. V/GenericRequest Resource ready in 3824.033544 size: 3.8623809814453125 fromCache: false this: 991315424
  36. V/GenericRequest Resource ready in 3773.0562109999996 size: 3.8623809814453125 fromCache: false this: 1029510845
  37. V/GenericRequest Resource ready in 4542.90796 size: 2.18359375 fromCache: false this: 516458536
  38. V/GenericRequest Resource ready in 4171.866168 size: 3.8623809814453125 fromCache: false this: 634418527
  39. V/GenericRequest Resource ready in 4474.170752 size: 3.8623809814453125 fromCache: false this: 356666200
  40. V/GenericRequest Resource ready in 4496.104085 size: 2.18359375 fromCache: false this: 671303046
  41. V/GenericRequest Resource ready in 4814.625126999999 size: 2.18359375 fromCache: false this: 347417463

想知道是否有人能够提供一些有关问题的见解.也许它是调整视图大小所需的时间?我使用match_parent作为视图的宽度和固定高度约200dp.

使用加载图像

  1. Glide.with(fragmentContext)
  2. .load(imageUrl)
  3. .centerCrop()
  4. .placeholder(R.drawable.placeholder)
  5. .into(viewHolder.productImage);

XML

  1. <ImageView
  2. android:id="@+id/product_image"
  3. android:layout_width="match_parent"
  4. android:layout_height="@dimen/product_image_height" (208dp)
  5. android:layout_gravity="top"
  6. android:background="@color/placeholder_bg"
  7. />

编辑:根据@VladimirMironov评论.我已经更新了池,并在视图被回收时清除了待处理的负载.这似乎在滚动快速时有所帮助.在查看测量时,是否为视图使用“match_parent”提供覆盖是否有任何好处?有些情况下,视图几乎不会显示5秒钟.

解决方法

切换到 PercentFrameLayout极大地帮助了我的项目在RecyclerView中滚动图像的速度. ( adapted from Bumptech’s “Issues” section of their GitHub page)
  1. <android.support.percent.PercentFrameLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="@dimen/product_image_height" (208dp)
  4. android:layout_gravity="top"
  5. android:background="@color/placeholder_bg"
  6. <ImageView
  7. android:id="@+id/product_image"
  8. app:layout_widthPercent="100%"
  9. />
  10. </android.support.percent.PercentFrameLayout>

然后在onBindViewHolder()

  1. int srcHeight = getImageHeight(); // hopefully you know the image height & width
  2. int srcWidth = getImageWidth();
  3.  
  4. PercentFrameLayout.LayoutParams layoutParams =
  5. (PercentFrameLayout.LayoutParams) viewHolder.productImage.getLayoutParams();
  6. PercentLayoutHelper.PercentLayoutInfo layoutInfo = layoutParams.getPercentLayoutInfo();
  7.  
  8. if(srcHeight > 0) { // being careful about potential divide by 0 errors
  9. layoutInfo.aspectRatio = (float)srcWidth / (float)srcHeight;
  10. } else {
  11. layoutInfo.aspectRatio = 1;
  12. }
  13.  
  14. layoutParams.height = 0;
  15. viewHolder.productImage.setLayoutParams(layoutParams);
  16.  
  17. Glide.with(fragmentContext)
  18. .load(imageUrl)
  19. .centerCrop()
  20. .placeholder(R.drawable.placeholder)
  21. .into(viewHolder.productImage);

最后,您需要将PercentFrameLayout的支持添加到build.gradle中的依赖项(Module:app)

  1. compile 'com.android.support:percent:23.1.1'

猜你在找的Android相关文章