ImageView RecyclerView延迟加载 [英] ImageView RecyclerView Lazy Load

查看:194
本文介绍了ImageView RecyclerView延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerViewAdapter,它在onBindViewHolder中进行AsyncTask调用,以从Google Cloud Storage中下载所需的图像.

I have a RecyclerViewAdapter which in onBindViewHolder, I make an AsyncTask call to download the image required from Google Cloud Storage.

RecyclerViewAdapter中有一些项目需要相同的图像.我试图通过仅下载映像(如果本地文件存储中不存在该映像)来解决该问题.否则,它将使用已下载的图像.

There are some items in the RecyclerViewAdapter which call for the same image. I tried to deal with this by only downloading the image if it doesn't already exist in the local file storage; otherwise if it does then it uses the image that has already been downloaded.

现在发生的事情是,如果RecyclerView中有多个项目在足够近的同一时间调用同一张图像,那么ImageView中的一项将只是空白/白色,但是其余的将正确显示该图像.

Now what's happening is, if there are multiple items from the RecyclerView calling for the same image at near enough the same time, one of the items ImageView will just be blank/white but the rest of them will display the image correctly.

我希望能够捕获这种情况,只使用 Thread.sleep ,然后尝试再次获取/使用该文件,但是我没有抛出任何异常或任何异常,所以我我不知道该如何捕捉这种情况.

I was hoping to be able to capture this scenario and just use Thread.sleep and then try to get/use the file again but I'm not getting any exceptions thrown or anything so I'm not sure how to even capture that scenario to deal with it.

我在logcat中看到的唯一看起来对远程帮助不大的东西如下:

The only thing that I can see in the logcat which looks remotely helpful is the below:

D/skia:-无法创建带有消息未实现"的图像解码器

但是,它不会引发异常,而只是将其记录为消息.

However it's not throwing an exception, it's just logging it as a message.

但是,它确实指出图像解码存在问题.因此,我检查了 BitmapFactory.decodeFile 文档,它说如果无法解码将返回null.因此,我尝试在尝试解码后检查我的位图是否为null,但是它从未命中该IF语句,这意味着它从未返回null,所以我不确定该怎么做.

It does however point to there being an issue with the decoding of the image. So I checked the BitmapFactory.decodeFile documentation and it says that it returns a null if it couldn't decode it. So I tried to check if my bitmap was null after trying to decode but it never hit that IF statement meaning it was never returning null so I'm not sure what to do.

new Getlogos(holder.logoImageView.getTag().toString(), holder.itemView.getContext(), new Getlogos.AsyncResponse() {
    @Override
    public void returnBitmapURI(String URI, String tag) {
        if (holder.logoImageView != null && holder.logoImageView.getTag().toString().equals(tag)) {
            if (URI.contains("drawable://")) {
                int id = Integer.parseInt(URI.substring(11));
                Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), id);
                holder.logoImageView.setImageBitmap(myBitmap);
            } else {
                Bitmap myBitmap = BitmapFactory.decodeFile(URI);
                holder.logoImageView.setImageBitmap(myBitmap);
            }
        }
    }
}).execute();

仅在某些情况下会出现此问题,而在同一张图片上不会始终出现此问题.通常,在具有相同图像要下载/使用的某些项目之间,它会有所不同.有时根本不会发生.

The issue only occurs sometimes and it doesn't happen to the same image consistently. it normally varies between some of the items that have the same image to download/use. Sometimes it doesn't occur at all.

任何人都无法阐明该skia消息的来源以及在无法解码时如何捕获它,以便我可以处理吗?

Can anyone shed any light on where that skia message is coming from and how I can capture it when it fails to decode, so I can handle it?

推荐答案

使用Glide在recyclerView中显示图像

Use Glide to display image in recyclerView

将此依赖项添加到build.gradle

Add this dependencies to build.gradle

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

在onBindViewHolder内

请求选项以显示错误图像,占位符图像等.占位符drawable的CircularProgressDrawable(可选)

Request option to show error image, placeholder image etc. CircularProgressDrawable for placeholder drawable(optional)

val circularProgressDrawable = CircularProgressDrawable(mContext)
        circularProgressDrawable.strokeWidth = 5f
        circularProgressDrawable.centerRadius = 30f
        circularProgressDrawable.start()

        val requestOption : RequestOptions = RequestOptions()
                .placeholder(circularProgressDrawable)
                .error(R.drawable.ic_error_1)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH)
                .dontAnimate()
                .dontTransform()

如下所示初始化Glide

initialize Glide as shown below

Glide.with(mContext)
            .load(model.imageUrl)
            .apply(requestOption)
            .into(holder.imageView!!)

这篇关于ImageView RecyclerView延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆