简体   繁体   English

Android Glide - 如果我的原始图片加载时间太长,我该如何加载另一张图片?

[英]Android Glide - How do I load another image if my original image takes too long to load?

I'm loading an image from a url like this.我正在像这样从 url 加载图像。 If the image is taking a really long time to load, I want to load another image in its place so I used.error() to load an error image if the load fails, but this fail never gets called.如果图像加载时间很长,我想在它的位置加载另一个图像,所以我使用 .error() 在加载失败时加载错误图像,但永远不会调用此失败。 Instead, the original image will eventually load after a long time, but I don't want to wait this long for the image to load.相反,原始图像最终会在很长一段时间后加载,但我不想等这么久才能加载图像。 I tried using.timeout() but that doesn't do anything.我试过 using.timeout() 但这没有做任何事情。

                Glide.with(this)
                .load(url)
                .error(Glide.with(errorUrl).load(imageView))
                .into(imageView);

You can use placeholder attribute of Glide.您可以使用 Glide 的占位符属性。 Inser your placeholder image inside drawable folder.将占位符图像插入可绘制文件夹中。 And use it like this.并像这样使用它。

Glide.with(this)
        .load(url)
        .placeholder(R.id.your drawable name)
        .error(errorUrl)
        .into(imageView)
Glide.with(context)
    .load(url)
    .placeholder(R.id.your drawable name) //this will be shown until actul image load
    .apply(new RequestOptions().override(150, 150)) //for resizing orgininal image 
    .into(imageView);

Note : Lower resolution in override(150, 150) would result in faster loading so choose accordingly.注意override(150, 150)中的较低分辨率会导致更快的加载,因此请相应地选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM