I have ListView with text and large image from internet. My image item has fit width and wrap_content
height.
I tried to display image in background with UIL
& Picasso
. Both of them can work but the image always reloads when I stop scrolling, and it makes ListView flickering
It looks like this:
你可以看到当我停止滚动时,它会重新加载下载和缓存的图像(我向下滚动,然后向上滚动).
我怎样才能防止这种情况发生?
<ImageView android:id="@+id/imgFeed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
// UIL
options = new DisplayImageOptions.Builder()
.showImageOnLoading(defaultImage)
.showImageOnFail(defaultImage)
.showImageForEmptyUri(defaultImage)
.resetViewBeforeLoading(false)
.cacheOnDisk(true).delayBeforeLoading(0)
.displayer(new FadeInBitmapDisplayer(200)).cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();
ImageAware imageAware = new ImageViewAware(viewHolder.imgFeed, false);
ImageLoader.getInstance().displayImage(item.getPhotoUrl(), imageAware, options);
// Picasso
Picasso.with(getContext())
.load(item.getPhotoUrl())
.placeholder(R.drawable.place_holder_big)
.resize(screenWidth, 0) //set max width
.into(viewHolder.imgFeed);
对于UIL,我尝试了很多方法这个问题 https://github.com/nostra13/Android-Universal-Image-Loader/issues/376但它们根本不适合我。
Update:我似乎遇到了内存缓存问题这个问题 https://stackoverflow.com/questions/27771646/picasso-keeps-reloading-images-while-scrolling-upwards-in-listview-loads-slowly。但我该如何解决这个问题呢?看看Facebook应用程序,他们做得很好。所有图像都有不同的尺寸和宽度,并且滚动非常平滑,无需重新加载图像。他们怎么能这么做呢?
如果你想知道 Facebook 是如何做到的,他们实际上发布了他们的图像加载库(https://github.com/facebook/fresco https://github.com/facebook/fresco)
是否有可能您当时实际上在底层 ListView 上调用了 notificationDataSetChanged ?您还使用 hasStableIds() 吗?
对于 UIL,您可以尝试使用 Weak MemoryCache(请参阅https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Useful-Info https://github.com/nostra13/Android-Universal-Image-Loader/wiki/Useful-Info)因为理论上这将允许您利用所有可用内存,尽管它可能会导致大量额外的 GC 调用。
毕加索·塔哈的方法看起来是您最好的选择!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)