简体   繁体   English

从URL Android加载大量图像的最有效方法

[英]Most efficient way to load a lot of images from URL Android

I'm making this netflix style app in which images are loaded into different categories. 我正在制作这个netflix风格的应用程序,其中图像被加载到不同的类别。 Let's say Dog videos (has 15 images), Cat videos (15 images), etc... All the images are loaded from a URL, it kind of takes a while for all to load. 假设狗视频(有15个图像),猫视频(15个图像)等等......所有图像都是从URL加载的,所有这些都需要一段时间才能加载。 I was wondering if there was anything I could do to speed up the process? 我想知道我能做些什么来加快这个过程吗? Or maybe show an empty container then fill it as the images load (that would be cool). 或者可能会显示一个空容器,然后在图像加载时填充它(这将很酷)。

This is what I have done: 这就是我所做的:

I have multiple async calls in one Activity, (1 async call per category) 我在一个Activity中有多个异步调用,(每个类别1个异步调用)

JSONTask1 dogTask = new JSONTask1();
JSONTask2 catTask = new JSONTask2();
JSONTask3 pigTask = new JSONTask3();
JSONTask4 horseTask = new JSONTask4();

dogTask.execute("");
catTask.execute("");
pigTask.execute("");
horseTask.execute("");

I have all of those in a row in my actual code. 我的实际代码中包含了所有这些内容。 Thanks. 谢谢。

I would use the " proxy pattern ". 我会使用“ 代理模式 ”。 Basically, you need to create a class that contains the minimal informations required for the display . 基本上,您需要创建一个包含显示所需的最小信息的类。 In which, you have a preview image . 其中,您有一个预览图像 When ever you load everything you start by showing the preview content, ie : a loading gif for everypicture with the title of the movie or whatever. 当您通过显示预览内容加载所有内容时, 即:每个图片的加载gif以及电影的标题或其他内容。 and basically the proxy would have a "loadImage" method that would make an ajax call or async call and the photos would load one by one. 基本上代理将有一个“loadImage”方法 ,它将进行ajax调用或异步调用 ,并且照片将逐个加载。 Plus, to make the loading easier, make sure the photos are not oversized. 另外,为了使装载更容易,请确保照片不会过大。

You can see Picasso answers , in picasso i suggest you this way : 你可以看到Picasso答案,毕加索我建议你这样:

Picasso.with(getApplicationContext()).load("your url").placeholder(R.drawable.your_place_holder).error(R.drawable.showing_when_error_occured)
            .into(imageView, new Callback() {
        @Override
        public void onSuccess() {

        }


        @Override
        public void onError() {

        }
    });

Also another suggestion from me : convert your thumb images to base64 format in backend, then firstly retrieve your thumbs and show them. 另外一个建议是:在后端将拇指图像转换为base64格式,然后首先检索你的拇指并显示它们。 Then start an async task and change images when successfull. 然后启动异步任务并在成功时更改图像。 Like whatsapp. 喜欢whatsapp。 In whatsapp you have thumb images they have so low resolution and super fast. 在whatsapp你有拇指图像,他们有如此低的分辨率和超快。 When you click image if you have internet connection they load actual thumb images, and click again they load larger image. 当您点击图像时,如果您有互联网连接,他们加载实际的拇指图像,然后再次单击它们加载更大的图像。

Picasso website : http://square.github.io/picasso/ 毕加索网站: http//square.github.io/picasso/

与Picasso异步加载它们,您甚至可以显示占位符图像,直到加载真实的图像

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

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